Choices within gosub

I’ve been toying with a hunger/thirst system in my game and I’m having trouble with something. Looked in the wiki and the forum but couldn’t really find an answer to my question.
What I’m trying to do is this:

  • I want the hunger and thirst stats to decrease every now and then.
  • I want the game to check if these stats are below 30.
  • If they are, I want the game to prompt the reader to eat and/or drink something.

Now, at first, I created a gosub to automatically decrease the stats by 2. I was thinking it would be cool to place a button on the stats page next to these stats so it would be easier for the reader to eat and drink but apparently you can’t do that. So then I tried to expand my gosub and wrote this:

*label needs
*set hunger -2
*set thirst -2
*if hunger <30
    You're getting hungry. Would you like to eat some food?
    *choice
        #Yes
            *if rations >0
                *set rations -1
                You feel better now.
                *return
            *if rations =0
                You have no more food!
                *return
        #No
            *return
*if thirst <30
    You're getting thirsty. Would you like to drink some water?
    *choice
        #Yes
            *if water >0
                *set water -1
                You feel better now.
                *return
            *if water =0
                You have no more water!
                *return
        #No
            *return
*return

  • First of all, I’m not sure whether or not I can do it like this.
  • Second, not sure if I need all those returns.
  • Third, and most important: when I tested this code, everything was fine until I tried to eat some food. It gave me the “it is illegal to fall out of a *choice statement” error and I’m not sure how to fix it or if what I’m trying to do is even possible.

Any input you can give me is very much appreciated. I’m very new to this and still trying to learn everything!

1 Like
  1. You can.
  2. You do, unless you consolidate them.
  3. Rather than just having two *ifs, turn the second one into an *else (your stat is probably negative).
1 Like

What do you mean by “consolidate them”?

And I tried your suggestion, but it didn’t work. However, I noticed that instead of presenting the text after the choice, it’s going straight for the part where I was before and the error appears on an option that does have a *goto included.

What do you want to do?
*line_break
*choice
    #Explore one of the corridors
        *set body_temp -2
        *set hunger 15
        *gosub needs
        some text goes here
    #Try to make a fire and rest for a while.
        *if matches =0
            You have no matches.
            *page_break
            *goto room1
        *if matches >0
            *set matches -1
            *set body_temp -2
            You pick up two logs and set them on the ground, in the middle of the room. From one of the inner pockets of your backpack, you retrieve a box of matches. You light one carefully and then place it under the logs, hoping they're dry enough to catch on fire.
            *page_break
            Success! You manage to make a small campfire. 
            *line_break
            You lay down next to it, place your backpack under your head for a bit of comfort and rest for a bit while your clothes dry and your body temperature rises pleasantly.
            *set body_temp +20
            *goto room1
    #Use some of the rope to make a bundle of firewood to carry with you and use later.
        *set eq1 "Bundle of wood"
        *goto room1

I used *set hunger 15 on the first choice to be able to test the gosub.
The error is on line 21, “#Try to make a fire and rest for a while.”.

1 Like

The error is actually line 20. You don’t have a *goto, *return, or *finish in your choice statement like you do for the others. So you’re falling out of the choice statement as soon as you come back from your subroutine. The issue isn’t the subroutine, it’s that you’re missing a *goto.

*choice
    #Explore one of the corridors
        *set body_temp -2
        *set hunger 15
        *gosub needs
        some text goes here
        *goto room1

That should fix it.

Also, what he meant by consolidate your returns is this:

*fake_choice
  # Yes.
    *if rations > 0
      *set rations -1
      *set hunger +50
      You feel better now.
    *if rations <= 0
      You don't have any food.
  # No.
*return

You’ll notice I’ve also fixed the if statement by including the option for rations to be negative, and I’ve made sure that eating rations actually has an effect on your hunger rather than doing nothing, which you might not have noticed.

(oh and an else statement won’t work with a fake_choice which is why I did it that way)

I left out the water part because it’ll be good for you to fix that one yourself so you can work through it.

3 Likes

I totally knew that. flips hair
Seriously though, you’re a lifesaver. I learned so much with your comment!

I did notice that the rations wouldn’t have an effect, but I still haven’t decided how much of an effect I want them to have, which is why I hadn’t written it yet. However, I did not consider the consequences of not writing any random value there. Had it worked, it would’ve just looped over and over again because the hunger stat wouldn’t be changed, right? So thank you for pointing that out.

Also thank you for figuring out the problem. Can’t believe it was that simple. I guess what threw me off what the fact that the game just skipped over the text on the subroutine so I figured there was something wrong with it and didn’t bother checking everything else. Especially not the *choice that I was still working on…

And on that consolidation thing, does it have to be a fake_choice? I mean, I understand why you wrote it like that, but does it have to be like that? Would it still work with a regular choice and the else statement? (thank you so much for pointing out that else statements don’t work with fake choice, I didn’t know that!)

EDIT: it works!

1 Like

It wouldn’t work (fake_choice and else) because *fake_choice can’t have *goto statements in it or anything else like that; all of the choices must lead to the same place. And including an *else statement requires the use of *gotos as it is illegal to fall into an else statement.

And yes, it would have to be *fake_choice to consolidate the return into just one line. The way with *choice would actually increase the number of lines, because you’d have to put a *label above your *return and then everywhere where you used to have *return you’d need a *goto to the label anyway. Fake_choice skips around all of that and just shunts every choice to the same place.

Implicit control flow (search it up in the forum) would solve all of these problems but I’m sticking with the regular way for now just to make sure I fully understand what I’m doing and how the code works.

4 Likes