Question regarding the use of if/else statements


#1

I’m trying to make the code print out a certain word based on a previous choice using if/else statements. Error tells me that I have to use *finish or *goto after every if or else statement. The problem is if I do that the “scene” will go to the next label or finish the game. I just wish to print out the text with the choice and continue printing text afterwards without getting to the next label.

Code:

*label briefing
You chose
*if race = 0
	Bananas.
*elseif race = 1
	Lollipops.
*else
	Berries.


Because of this you get stat points based on your choice :
*if race = 0
	*set strength +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*elseif race = 1
	*set intelligence +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*else
	*set strength +1
	*set agility +3
	*set perception +1

Strength: ${Strength}


Agility: ${Agility}


Intelligence: ${intelligence}


Persuasion: ${Persuasion}


Perception: ${Perception}


*goto getStats

#2

If… *if commands paired with *elseif or *else, a *goto or *finish is mandatory

BUT!

Try replacing those *elseif and *else with another *if,
and behold the magic :sparkles: :wink:


#3

Try
Code:

*label briefing
You chose
*if race = 0
	Bananas.
*if race = 1
	Lollipops.
*if race > 1
	Berries.


Because of this you get stat points based on your choice :
*if race = 0
	*set strength +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*if race = 1
	*set intelligence +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*if race > 1
	*set strength +1
	*set agility +3
	*set perception +1

Strength: ${Strength}


Agility: ${Agility}


Intelligence: ${intelligence}


Persuasion: ${Persuasion}


Perception: ${Perception}


*goto getStats

#4

And maybe in near future you won’t have to do this and can do things like @kgold has done.

He has mentioned them here


#5

I see two possible ways to handle this: First, adding an extra label after each chain of *if and *else; or second, using a chain of all *if without any *elseif or *else statements, as *if can function without a redirect command such as *goto or *finish but those two require redirects.

With the first method, you may have code something like this...
*label briefing
You chose
*if race = 0
	Bananas.
	*goto stat_points
*elseif race = 1
	Lollipops.
	*goto stat_points
*else
	Berries.
	*goto stat_points

*label stat_points
Because of this you get stat points based on your choice :
*if race = 0
	*set strength +2
	*set agility +1
	*set persuasion +1
	*set perception +1
	*goto view_stats
*elseif race = 1
	*set intelligence +2
	*set agility +1
	*set persuasion +1
	*set perception +1
	*goto view_stats
*else
	*set strength +1
	*set agility +3
	*set perception +1
	*goto view_stats

*label view_stats
Strength: ${Strength}


Agility: ${Agility}


Intelligence: ${intelligence}


Persuasion: ${Persuasion}


Perception: ${Perception}


*goto getStats

Fairly simple, though perhaps a little longer than you’d like, due to the extra *label statements and *goto commands.

The second method may use code something like this...
*label briefing
You chose
*if race = 0
	Bananas.
*if race = 1
	Lollipops.
*if (race != 0) and (race != 1)
	Berries.

Because of this you get stat points based on your choice :
*if race = 0
	*set strength +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*if race = 1
	*set intelligence +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*if (race != 0) and (race != 1)
	*set strength +1
	*set agility +3
	*set perception +1

Strength: ${Strength}


Agility: ${Agility}


Intelligence: ${intelligence}


Persuasion: ${Persuasion}


Perception: ${Perception}


*goto getStats

In the second example, I used *if (race != 0) and (race != 1) to duplicate the effect of an *else without actually using an *else. If you’re not going to use *else, you’ll need something to make sure every possible value for the variable in question can be handled by the code the way you want, and nothing simply “falls out” by accident. It isn’t wrong to write code that “falls out” on purpose, but it should only happen because you’ve chosen to do that.

You could absolutely use *if race > 1 to achieve the same effect… if you’re absolutely certain the race variable will never be both less than one and not equal to one or zero at the same time. If you use *if race > 1 and some function of math causes the race variable to be something ridiculous like -1 or 0.5, the code won’t be able to handle it (-1 is not greater than 1, but is simultaneously not equal to 0 or equal to 1.) If your variable will only be whole integers, and only 0 or greater, then *if race > 1 will work perfectly!

I did notice one final possibility, at least for this specific sample of code...
*label briefing
You chose
*if race = 0
	Bananas.
	*set strength +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*if race = 1
	Lollipops.
	*set intelligence +2
	*set agility +1
	*set persuasion +1
	*set perception +1
*if (race != 0) and (race != 1)
	Berries.
	*set strength +1
	*set agility +3
	*set perception +1

Because of this you get stat points based on your choice :

Strength: ${Strength}


Agility: ${Agility}


Intelligence: ${intelligence}


Persuasion: ${Persuasion}


Perception: ${Perception}


*goto getStats

Because both of your *if chains are looking for the exact same combination of values for the race variable, you can handle setting stats with the same *if statements you used to display the Bananas, Lollipops, and Berries text.


#6

As far as current implementation goes, think of it this way: *if, *elseif, *else chains are used for redirecting (hence the *goto/*finish requirement); *if, *if, *if chains are used for displaying text/setting variables without redirecting.

As already mentioned, you can just use *if statements to set your variables, though you’ll need to make sure to account for all possibilities.

You can also use *if chains for text in your story:

You are
*if persuasion = 2
    very
*if persuasion = 1
    a little bit
*if persuasion = 0
    not at all
persuasive.

or

*if (persuasion > strength)
    You should really try to talk to them first.
*if (strength > persuasion)
    Forget talking, just hit them with your sword.

Note that if you want to use variables to alter display text, you can also use the new multireplace feature:
You are @{persuasion+1 not at all|a little bit|very} persuasive.
or
@{(persuasion > strength) You should really try to talk to them first.|Forget talking, just hit them with your sword.}


#7

I’m surprised you responded so quickly and in such numbers. As a newbie I am happy to see such a nice community. Never knew that you can modify the code yourself like kgold did, I’ll give it a shot once I feel like I master more of the code. Minnows third option is just perfect and I will certainly use the newly aquired power of multireplace feature™. Thank you all !:kissing_heart:

Also, if I happen to have another problem with code that comes right after the code I showed but the problem is different from the topic, should I make a different thread ? (No worries I’m ok now. Just wanted to know.)


#8

You should make a new thread - that would gain larger attention and your problem would be solved quicker than posting in this thread though both will be solved in 12 - 48 hrs