Jump to content

What does RETURN actually do in a condition?


Recommended Posts

I'm having issues with the use of 'return'. From what I have previously understood is that, for example, you have 5 IF conditions in a script to check and only 1 of them has to be met for a result. Each time this script is run all 5 conditions are checked like the example below.

 

An Event Called

 

Int iButton = MyMENU.Show()

 

If iButton == 0

debug.notification("You pressed 1")

EndIf

 

ElseIf iButton == 1

debug.notification("You pressed 2")

EndIf

 

ElseIf iButton == 2

debug.notification("You pressed 3")

EndIf

 

… and so on…

 

Even if the required result is met at the second IF condition, the script will still check the remaining conditions. The next example below is what I beloved would 'get around' this.

 

An Event Called

 

Int iButton = MyMENU.Show()

 

If iButton == 0

debug.notification("You pressed 1")

Return

 

ElseIf iButton == 1

debug.notification("You pressed 2")

Return

 

ElseIf iButton == 2

debug.notification("You pressed 3")

Return

 

… and so on…

Questions: Does 'Return' at the end of a condition bypass any remaining condition checks?

Does 'return' send the script back to it's event beginning (waiting for something to happen)?

Can somebody show me an example of a multi-condition script that uses 'return' of any other command to 'get out' of checking any remaining conditions?

Thank you.

 

 

 

Link to comment
Share on other sites

Return does two things.

 

For functions with return values, return will return that value(no pun intended).

 

Int Function GetSum(Int aiNum01, Int aiNum02)

Return aiNum01 + aiNum02

EndFunction

 

This also serves to exit/finish the function, the other thing Return does.

 

For menus, return will exit the menu, all I understand with this one is like if you want to make a cancel button which closes out the menu all together.

 

For multiple conditions, including nested conditions, it may not be necessary to use Return unless you need it as a failsafe. The function/event normally exits when everything in a given condition has been met, however if you believe there could be some reason it may break(debugging is basically trying to break it and then add a prevention method), you could use Return to force an early exit.

 

For functions with return values, you can't just use return by itself, you have to return the value that the function expects.

 

Some examples:

 

 

 

 

if condition 1 == x
   ; something happens
else
   ; something else happens
   ; function/event is exited here
endif
 
------
 
if condition 1 == x
  ; something happens..
elseif condition 2 == x
  ; nothing happened in condition 1, something happens in condition 2
  ; function/event exits
else
  ; just in case nothing happened in condition 1 or condition 2, something happens here
  ; function/event exits
endif
 
----
 
if condition 1 == x
    ; ...
elseif condition 2  == x
    ; ...
elseif condition 3 == x
   ; ...
else
   ; ...
endif
 
return
; let's assume something was missed with entire condition tree and the function gets stuck
; return will exit the funciton

 

 

 

I wish I could think of an actual "broken" scenario but this the best I could come up with.

Edited by Rasikko
Link to comment
Share on other sites

I apologise if I have upset anybody, cumbrianlad tells me that I've upset Rasikko and has PMed me telling me his dislike of my attitude towards Rasikko. Rasikko I'm sorry if I have caused you any offence. Yes, I have multiple threads going as I have multiple problems. If I don't reply in what is considered an appropriate time then again I am sorry. I have an elderly mother who I have to watch out for, not an excuse - a fact I have to deal with.

So again if I have offended anybody and it seems especially Rasikko - I am sorry.

 

EDIT: My internet was down for a day as well which contributed to my late acknowledgment of your replies.

Edited by antstubell
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...