*if statement in stats screen

Hey ya’ll!

So i want to display variables for hunger in my stats screen, but *if statements seem to be getting in my way. I managed to make it work out and display what I wanted it to [image attached], but surely there’s a more elegant way to do this than using a *goto leading to an empty label to get it to work, right?

I also cannot use *finish in place of *goto here, as that makes it only read the first part of the hunger sequence (The MC’s) and not the siblings as well.

Here’s my example code (in choicescriptstats_txt):

*stat_chart
percent Health

HUNGER
*line_break
*if (hunger=1)
$!{h1}
*goto h

*elseif (hunger=2)
$!{h2}
*goto h

*elseif (hunger=3)
$!{h3}
*goto h

*else (hunger =4)
$!{h4}
*goto h

*label h

SIBLING HUNGER
*line_break
*if (shunger=1)
$!{sh1}
*goto s

*elseif (shunger=2)
$!{sh2}
*goto s

*elseif (shunger=3)
$!{sh3}
*goto s

*elseif (shunger =4)
$!{sh4}
*goto s

*label s

*finish

Well first of all, you don’t actually need to use *elseif here at all. There’s no possible situation where “hunger” equals more than one value at once, so you can just replace every single one of them with a normal *if and there will be no need for *goto anymore.

Alternatively, if you already feel comfortable with ChoiceScript, you can just put *create implicit_control_flow true at the start of your story and eliminate the need for all these pesky *goto commands altogether. I know it’s not recommended for new writers, but I personally consider the default setting a bit of a pain, so maybe consider that eventually.

2 Likes

Annnnd that worked! :melting_face: silly me, i originally had them all as simple *if statements but decided to change them all to *elseif…just because? This is what happens when you code all day kids :sob:

Ill look into the implicit control flow thing, ive always hated having to create extra labels because of *if statements

Thanks for the speedy answer man!

Not necessarily. CS is built to lean on labels. Any if/elseif/else block requires a label at the end (unless you switch on Implicit Control Flow, which I wouldn’t recommend for anyone still getting their heads around the coding). Thinking of your *label h as an “empty label,” reasonable as that may be, isn’t a perspective that fits with CS’s design as a language.

But if hunger only has four possible values-- from when you create it through the whole game, not just when you want to check it – you can use multireplace like this:

@{hunger $!{h1}|$!{h2}|$!{h3}|$!{h4}}

to yield the same outcome as your *if hunger block.

Note that when you’re using if/elseif/else, the last option should just be

*else

not

*else (variable = "value")

and also not

*elseif (shunger =4)

If you want to remind yourself of what that *else means, you can do it with a comment:

*else 
  *comment (hunger =4)
  $!{h4}
  *goto h

Hope that’s helpful…

4 Likes

Just tried out the multireplace and i really like that as well (it also lessens this big ole chunk of code i originally had, which is great)

Ill stick to using the multireplace, thank you so much as well! This simplifies things a lot haha

1 Like

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.