What is the difference between "exit" and "return"?

MrChoke

Member
I know both of them leave the currently executing function. With return you can give a value back to the caller, exit you can't. But with exit, I read there is something about stopping further execution of events maybe?? I didn't quite know what it meant.

So does exit do anything different than return?
 

TsukaYuriko

☄️
Forum Staff
Moderator
return is usable in scripts. It ends execution and returns a value.

exit is usable in scripts, events and action blocks and ends their execution, respectively. Call a script in an event, call exit in said script - only the script is ended. Call it in the event - the whole event is ended.
 

MrChoke

Member
return is usable in scripts. It ends execution and returns a value.

exit is usable in scripts, events and action blocks and ends their execution, respectively. Call a script in an event, call exit in said script - only the script is ended. Call it in the event - the whole event is ended.
Sorry, I am still confused. When you say "end the event" what do you mean? Let's say I exit out of the "Step" event. I know the rest of execution for "Step" will end. But does that also mean "End step" won't execute?
 

MrChoke

Member
Ok, so its the exact same as return. It exits the current function. An event handler is just a function like anything else. I can probably do return in an event handler and it leaves just like exit.
 

Yal

🐧 *penguin noises*
GMC Elder
Ok, so its the exact same as return.
No, which you would've seen if you'd read TsukaYuriko's first reply.
return is usable in scripts. It ends execution and returns a value.

exit is usable in scripts, events and action blocks and ends their execution, respectively. Call a script in an event, call exit in said script - only the script is ended. Call it in the event - the whole event is ended.
There's one more thing you need to think about: return is meant to return a value, exit isn't. If you write myvar = myfunction(foo,bar) and you use exit instead of return, you have basically no idea what value you'll get if you try using myvar later.
 

Yal

🐧 *penguin noises*
GMC Elder
There was a time when I used globalvar to avoid having to write "global" for all my global variables, used the way you could address an array without []s to access its zeroeth element, and enjoyed how objects created later were on top of earlier objects meant there was no need to depth-sort bullets. Not to mention using execute_string to avoid having to write scripts unless absolutely necessary. And all of those things meant my GM8 projects were a nightmare to port over to Studio (which is why most of them weren't ported over). I learned my lesson, and these days I only do stuff that's properly defined by the APIs.
 

TheouAegis

Member
You can use return in events, I'm pretty sure. It doesn't actually return anything though outside of a script.

Isn't exit supposed to end the event even if it's called inside a script in GMS2?
 
Top