The *if conditional command serves three main purposes:
- comparing variables (including textual string variables),
- checking if a variable is true or not,
- checking if the value of a numeric variable is equal to, not equal to, greater than, or less than, a specific value.
Let's say we create var1 with the value 10, and var2 with the value 20. We can then use the *if command to compare them:
*temp var1 = 10 *temp var2 = 20 *if (var1 = var2) 10 is equal to 20! *finish 10 is not equal to 20! *finish
Obviously that is not correct (10 is not equal to 20!), the conditional check will fail, and the program will skip the two indented lines and proceed straight to the next equally-indented line, in this case the line that says that 10 is not equal to 20. If however, that condition had been true, the program would have followed the indented lines and displayed the text that 10 is equal to 20.
The parentheses (brackets) around (var1 = var2) are not strictly required when a single conditional check sits on a line of its own, as in the above example, but as these are needed if you have more than one condition or anything else on the same line (such as a *choice #option), so you may prefer to get into the habit of using them regardless. It doesn't hurt to use them even though not strictly required, so it's a matter of personal choice.
Equality and inequality
With the basic *if command, variables can be checked in a variety of ways; some common examples follow.
Equal to: *if var = 40 (Is var equal to 40?) Not equal to: *if var != 40 (Is var different from 40?) Greater than: *if var > 40 (Is var greater than 40?) Less than: *if var < 40 (Is var less than 40?) Greater than or equal to: *if var >= 40 (Is var greater than or equal to 40?) Less than or equal to: *if var <= 40 (Is var less than or equal to 40?)
Note: for greater/less than or not equal to, the greater/less than or not sign must come before the equals sign.
And / Or / Not
In addition, it's possible to assign two or more conditions to a single *if statement, using and / or. Note that when checking two or more conditions simultaneously, parentheses are required to separate them, as follows:
And: *if (var1 > 10) and (var2 > 20)
Is var1 greater than 10 and var2 greater than 20? (Proceed only if both conditions are true.)
Or: *if (var1 > 10) or (var2 > 20)
Is var1 greater than 10 or var2 greater than 20? (Proceed if either condition is true.)
Not: *if not (var > 10)
Is var not greater than 10? (Proceed only if var is less than or equal to 10).
Multiple conditions can also be checked, which require more parentheses to separate them:
Three equations: *if (((var1 > 10) and (var2 > 20)) and (var3 > 30))
Is var1 greater than 10 and var2 greater than 20 and var3 greater than 30? Note that each and/or can only connect two equations, and each equation can only be connected to one and/or. For example:
*if ((var1 > 10) and (var2 > 20) and (var3 > 30))
would not work, as the second and tries to connect (var2 > 20) which is already connected to (var1 > 10).
Complex parentheses: *if ((var1 > 10) and (var2 > 20)) or (var1 > 70)
Is var1 greater than 10 and var2 greater than 20, OR is var1 greater than 70? (proceed only if var1 is greater than 10 AND var2 is greater than 20, but if this combined condition is not true, it will still proceed if var1 is greater than 70).
Boolean (simple true / false) variables have a special kind of *if check, which just checks if they are true.
This will check if a variable is set to true. Parentheses are required enclosing the (var) name, as shown.
N.B. If a variable is not a boolean and this type of check is used, it will cause a game stopping error, e.g. a string variable with a text value set as "true" is not the same as a boolean variable set to true where this particular type of simple true / false check is concerned.
You can also check if a boolean variable is currently false (i.e. not true) as follows:
*if not (var)
Again, the (var) name must be enclosed within parentheses.
N.B: *If not (var) will work correctly only if this check occupies a line of its own. For example, in the following code the 'not' will be completely ignored and would actually display that #Option text if (var) is in fact true:
*choice *if not (var) #This option to be displayed *goto next
In order to work correctly, that code should actually be written as:
*choice *if not (var) #This option to be displayed *goto next
Another solution is to enclose the 'not' in another set of parentheses, like so:
*choice *if (not (var)) #This option to be displayed *goto next
Let's say we have a string variable called name that contains some text. You can use *if to check if the text it contains is currently exactly equal to a specific word or phrase, for example:
*if (name = "alex")
Note that you must always use "quotation marks" when checking a string variable.
You can also check if a string is not equal to a particular word or phrase:
*if (name != "alex")
The versatile *if command serves many purposes in ChoiceScript. Some common examples of use follow:
Setting other boolean variables to true or false
*if (exam_passed) *set happy true You've passed the exam! *finish
If the boolean variable "exam_passed" is true, the variable "happy" will be set to true, and the text "You've passed the exam!" will be displayed.
Modifying numeric & string variables
*if (exam_passed) *set happiness + 30 *set graduated "With Honors" You've passed the exam! *finish
If the boolean variable "exam_passed" is true, the numeric variable "happiness" is increased, the string variable "graduated" is changed to "With Honors" and the text "You've passed the exam!" will be displayed.
*if (happiness < 10) You're very sad today. *finish *elseif (happiness < 30) You're sad today. *finish *elseif (happiness < 60) You're not very happy today. *finish *elseif (happiness < 80) You're moderately happy today. *finish *else You are very happy today! *finish
In the example above, the text displayed to the player -- e.g. You're not very happy today. -- is determined by the character's current "happiness" value. It will check each *if/*elseif condition in turn until it finds one that is true, at which point it will display the appropriate message and *finish. If none of those conditions apply (i.e. if "happiness" is actually 80 or more) it will default to the "catch-all" *else at the end. *else should never be given a specific condition of its own as it essentially means "If none of the above conditions apply, then do this".
It is a basic requirement of ChoiceScript's logic that any use of *elseif or *else commands in combination with *if must always end with either a *finish, *goto_scene, *goto, *goto_random_scene, *ending or *gotoref command, as in the example above. The following code will cause an error:
*if (happiness < 30) You're very sad today. *elseif (happiness < 60) You're moderately happy today. *else You are very happy today!
because ChoiceScript hasn't been told what to do once it has found a valid condition and displayed the appropriate text.
|Choice||*choice, *fake_choice, *disable_reuse, *hide_reuse, *allow_reuse, *selectable_if|
|Variable||*create, *temp, *set, Arithmetic operators, *delete, *input_number, *input_text, *print, *rand|
|Conditional||*if, *elseif, *else|
|Goto||*label, *goto, *goto_scene, *goto_random_scene, *gosub, *gosub_scene, *finish|
|Formatting||Bold text, Italic text, *image, *line_break, *page_break, *link, *stat_chart|
|Miscellaneous||*comment, *scene_list, *title, *author, *achieve, *achievement, *check_achievements, *bug, *ending, *more_games, *share_this_game, *show_password, *script|