PDA

View Full Version : Stop Code execution!!!


pissupoosa
07-19-2011, 08:58 AM
Hey.

what is the syntax to halt the code execution and give focus back to the UI..

for instance, something like

if ErrCode != undefined do
(
messagebox ErrMsg as string
)

i want the code to stop here and exit the subroutine without halting the code. pls excuse my noob'ness.. im very new to maxscript.

thankx again.

Pacermike
07-19-2011, 09:49 AM
Would this be a good place to use try()catch()?


try
(
print selection[1].name as string
)
catch
(
messagebox "Yut oh! T'aint nothing selected!"
)

JHN
07-19-2011, 09:51 AM
Depends on what you're in, if it's a function that doesn't get called in a big loop you can quit a function by calling "return".
If you're in a for loop, you can quit the loop by calling "break". A more elegant (and preferred) solution to quitting a for loop is with the use of a while condition:


loop = true
for i = 1 to 100000 while loop do
(
if i > 100 do
(
loop = false
)
)


There's variations to that with "do...while" or "while...do". Look it up in the manual.

So it depends a bit on what you want to break, for non repetitive loops you can use return and break, if you care about speed and memory use conditional operators like while.

-Johan

j-man
07-19-2011, 11:38 AM
Hi,

You can exit a sub-routine early by using the exit command.


(
format "Starting code.\n"
x=0
for i=1 to 1000 do
(
x+=1
r=(random 0 10)
if r==1 then exit
)
format "% random numbers generated before we got the number 1.\n" x
)


Or did I miss something?

J.

denisT
07-19-2011, 12:03 PM
Hi,

You can exit a sub-routine early by using the exit command.


(
format "Starting code.\n"
x=0
for i=1 to 1000 do
(
x+=1
r=(random 0 10)
if r==1 then exit
)
format "% random numbers generated before we got the number 1.\n" x
)


Or did I miss something?

J.

we all missed something and are answering an another question :)
but i can't leave a code not optimized:

format "the successful attempt is #%\n" (for i=1 to 1000 where (random 0 10) == 7 do exit with i)


if I'm so unlucky to miss the SEVEN, the code returns OK

j-man
07-19-2011, 01:00 PM
LOL thanks Denis (I think).

I chose to keep it simple (and un-optomised) but history tells me that no matter how far I may
optomise something you will probably find another level < :


J.

CGTalk Moderation
07-19-2011, 01:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.