Jump to content

Getting Out of a Function


Recommended Posts

I hope my example tells you what I want to know. and how/can it be done. Thanks.

 

 

;this function called from within another function

Function DoWeHaveTheThing()

If Game.GetPlayer().GetItemCount(ThingThatWeneed) < 1; player does not have the thing that we need

; show a message saying get the thing

EndIf

;I want to exit this function NOW and return to previous function

If Game.GetPlayer().GetItemCount(ThingThatWeneed) > 0; player has the thing that we need

; do all the stuff with the thing

EndIf

EndFunction

 

Link to comment
Share on other sites

Functions end when everything in their block has finished running.

 

Oh man!.Even in the 1980s Atari BASIC programming had a 'get out of sub-routine early' call. Would go something like this...

 

10 Doing Stuff

20 GoSub 100 go to line 100 and run sub-routine from there

30 Hi. Back from sub-routine are you

 

100 Do Stuff1

110 Return If stuff 1 condition is correct/incorrect/etc go back to line after sub-routine was called (line 30) if not...

120 … do more stuff

130 Return All stuff is done go back to the line after sub-routine was called (line 30)

 

I am gonna have to run a lot of functions if I can't exit a block early.

Edited by antstubell
Link to comment
Share on other sites

Use Return to exit early.

 

 

;this function called from within another function
Function DoWeHaveTheThing()
  If Game.GetPlayer().GetItemCount(ThingThatWeneed) < 1; player does not have the thing that we need
    ; show a message saying get the thing
    ;I want to exit this function NOW and return to previous function
    Return
  ElseIf Game.GetPlayer().GetItemCount(ThingThatWeneed) > 0; player has the thing that we need
    ; do all the stuff with the thing
  EndIf
EndFunction

 

 

 

You can also use Return to send a specific value if you design your function to do so.

Example

 

Bool Function DoWeHaveTheThing(Form NeededThing)
  If Game.GetPlayer().GetItemCount(NeededThing) < 1; player does not have the thing that we need
    ; show a message saying get the thing
    Return False
  Else ; no need to check again, either there is enough or there is not
    Return True
  EndIf
EndFunction

Function OtherFunction()
  If DoWeHaveTheThing(ThingThatWeNeed) == True
    ;do stuff with the thing
  EndIf
EndFunction

Note by setting up parameters for your functions you can reuse them with different stuff by passing in different values as needed. In this example DoWeHaveTheThing is reusable since any form can be passed into it.

 

Link to comment
Share on other sites

Note that the really critical part is where you would choose to return. In your original code you indicated you wanted to return after the first if-endif statement. That would have meant that the second one would never even be checked.

 

IsharaMeradin's code uses the if-elseif-endif structure and corrects the potential problem by including the return inside that first if section. In that example the return isn't actually needed since the elseif will also effectively tell the game that the function has nothing more to do when the first if condition is met.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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