Notifications
Clear all

[Closed] How to exit a script early if condition is true..?

I’m sure this is simple but I have been searching on the net and max help files with no luck.

For example:


if (classOf($) == ObjectSet) then
(
messageBox "Please select just 1 object"
end script -- How??
)

(Other code here)

I know its possible with if and else statements but then the code gets ugly when theres so many conditions and your main code is indented 10 times

4 Replies
1 Reply
(@raytracer05)
Joined: 11 months ago

Posts: 0

If you have a lot of conditions you want to test have a look for case in the help. It works a bit like else if but is a bit neater.

case classOf obj of
(
	box:(some code here)
	cone:(some code here)
	sphere:(some code here)
	default:(some code here) --if non of the above are true
)

This is how I tend to do that kind of thing.

fn validateSelection =
    (
    	if selection.count == 0 then
    	(
    		messageBox "Nothing selected"
    		return false
    	)
    	else if selection.count >= 2 then
    	(
    		messageBox "Please select just one object"
    		return false
    	)
    	
    	true
    )
    
    button start "Start"
    
    on start pressed do
    (
    	if (validateSelection()) then
    	(
    		doStuffWithSelection()
    	)
    )

Cheers,

Drea

what do you actually want to exit: loop, function, or entire script?

you can’t exit , only error generate a “script interrupt” and “[size=2][font=Courier New]escapeEnable true” detect [/size][/font]the Esc[size=3][font=Times New Roman] key when your “while loop” is infinite and you want interrupt it.
You can use the “[size=2][font=Courier New]return” code like C++:[/font][/size][/size][/font][size=3]
[/size]
[left]
[/left]

  fn main =
  (
	  <your code>
	  if condition do return 0
	  <your code>
	  return 1
  )
  main()

because the scope of your code is inside a function, not in global scope as listener