"invalid return; we've already returned from the last gosub"

Hi guys. This error has me stumped, so I’m posting here. Basically this is the scene I keep just to store my ‘level-up’ subroutine. I only encounter this in randomtest btw … The error in question says:

Error in line 5: skills line 5: invalid return; we’ve already returned from the last gosub

To go through the code: in the main game, whenever a quest is done, I’ll say

*gosub_scene skills quest_complete

(To keep my code clean, I have different labelled subroutines for all my experience point checks.)
So the game skips to *label quest_complete and adds 500 experience points to the experience point count. Then it goes to *label level_up_check, which checks if the player has levelled up. If so, the player can add points to a stat. This then goes to *label exceed check which ensures that the player’s stats are capped at 100. Then all these subroutines return, and the player goes back to the game once all the stuff behind the scenes is taken care of.

*label quest_complete
- Quest complete: 500 EXP -
*set exp +500
*gosub level_up_check
*return ==== this is line 5 which throws up the error

*label level_up_check

*if ((exp % 1000) = 0)
 Your skill level has increased! 
 *if (((charisma < 100) or (knowledge < 100)) or ((engineering < 100) or (intuition < 100)))
  *line_break
  Add a skill point to ... 
  *fake_choice
   *selectable_if (charisma < 100) #Charisma
    *set charisma +10
   *selectable_if (knowledge < 100) #Knowledge
    *set knowledge +10
   *selectable_if (engineering < 100) #Engineering
    *set engineering +10
   *selectable_if (intuition < 100) #Intuition
    *set intuition +10
   #Forfeit the skill point
  *gosub exceed_check
  *return
 *else
  All your skills are maxed. 
  *gosub exceed_check
  *return
*else
 *return

*label exceed_check
*if (charisma > 100) 
 *set charisma 100
 *goto check2
*else
 *goto check2
*label check2
*if (knowledge > 100)
 *set knowledge 100
 *goto check3
*else
 *goto check3
*label check3
*if (engineering > 100)
 *set engineering 100
 *goto check4
*else
 *goto check4
*label check4
*if (intuition > 100)
     *set intuition 100
     *return
    *else
     *return

So I have no idea why randomtest is throwing this up …

Is this the only place you use *gosub level_up_check?

When I get this error, it’s usually because I’ve used a *goto instead of a *gosub by mistake, for a subroutine I use in multiple places.

Wait, here’s a thought. If this is exactly how your code looks, then you need a *goto or *finish after your *gosub level_up_check to take the code somewhere other than your subroutine. Otherwise, when the subroutine finishes, it *returns to line 5 and starts reading from *label level_up_check… which means it’ll hit a *return again and choke on it.

2 Likes

So I changed the code and it still doesn’t work! I replaced all the specific *gosubs to just *goto. So now there’s only one *return, like so:

*label quest_complete
- Quest complete: 500 EXP -
*set exp +500
*goto level_up_check
*label level_up_check
*if ((exp % 1000) = 0)
 Your skill level has increased! 
 *if (((charisma < 100) or (knowledge < 100)) or ((engineering < 100) or (intuition < 100)))
  *line_break
  Add a skill point to ... 
  *fake_choice
   *selectable_if (charisma < 100) #Charisma
    *set charisma +10
   *selectable_if (knowledge < 100) #Knowledge
    *set knowledge +10
   *selectable_if (engineering < 100) #Engineering
    *set engineering +10
   *selectable_if (intuition < 100) #Intuition
    *set intuition +10
   #Forfeit the skill point
  *goto exceed_check
 *else
  All your skills are maxed. 
  *goto exceed_check
*else
 *goto exceed_check
*label exceed_check
*if (charisma > 100) 
 *set charisma 100
 *goto check2
*else
 *goto check2
*label check2
*if (knowledge > 100)
 *set knowledge 100
 *goto check3
*else
 *goto check3
*label check3
*if (engineering > 100)
 *set engineering 100
 *goto check4
*else
 *goto check4
*label check4
*if (intuition > 100)
 *set intuition 100
 *goto end
*else
 *label end
 *return

And yes, I haven’t used any *gotos by accident …

As Havenstone said this is probably due to you “running into” a *return somewhere you don’t mean to.

1 Like

Don’t end with *return. Only *gosubs can end with *return. End with a *goto or a *finish after *label end. (Or you could revert to your original code and put the same *goto or *finish after *gosub level_up_check – it would do the same thing.)

1 Like

FWIW, it looks to me like you are just over-complicating things for yourself by using *return commands where they are not necessary (e.g. on every line immediately following a *gosub in your original code, which was the direct cause of that error). It may help if you take a closer look at a basic use of *gosub / *return and carefully read the explanation accompanying this example, then you’ll see what I mean:

2 Likes

You guys, your solutions are all welcome, but I fixed this whole thing by myself :stuck_out_tongue:
I believe I may have confused randomtest by placing the skills scene last on the list, and as a result randomtest went from the latest scene I was working on to the skills scene which caused the error. I changed the order of scenes in startup by putting skills right after startup and using a *goto_scene in startup to skip past the skills scene. That seems to have fixed the error completely, without requiring me to change any code.

Well, at the end of the day, don’t fix what’s working. :slight_smile: Glad it worked out.

1 Like