ChoiceScript Wiki

    The *set command is used for assigning a new value to a variable, or modifying the value of a variable to something else. It can be used for both Temporary variables (created as & when needed in your story scripting using the *temp command) and Permanent variables (defined using the *create command in startup.txt). There is no actual difference in the way *set is used for either variable type.

Setting Numeric Variables

Let's say we have a numeric variable named "var", to which we assigned the starting value of 10.

We can change its current value anywhere in our story scripting, by giving it a new value with the *set command:

*set var 20

This will simply overwrite the current value of the "var" variable, changing its value from 10 (or whatever) to 20.

We could instead just modify its current value, like this:

*set var + 15

Which would add 15 to the value of the "var" variable, increasing it from 10 to 25.

Other arithmetic operators are also possible.

We can also add (or subtract, or multiply, or divide) the value of one numeric variable with another, as follows:

*set var + var1

Setting String Variables

Variables are not always numeric in type. We can also assign other types of values, like strings (text):

*set var "Hello!"

Strings should always be enclosed within "quotation marks" as shown above.

Note that there is no real limit to the number of alphanumeric characters (meaning letters and numbers, but also including common symbols such as exclamation and question marks) able to be included in a single string value, but very long text strings may be better handled as conditional text using the *if / *elseif / *else commands -- unless there is a very good reason for saving the data as string values.

Note that you can also concatenate (combine) two or more strings, with the *set command.

Setting Boolean Variables

The third data type for variables is boolean, which may hold a value of only either true or false, as follows:

*set var true

*set var false

Note that even though boolean values look vaguely the same as string (text) variables, they are not; "Quotation marks" are not needed around the values true or false, and no other values can ever be assigned to a boolean.

Changing a Data Type

Although it is now possible to effectively change a variable's data type using the *set command (e.g. a string or boolean variable can be *set to a numeric value - or vice-versa - simply by overwriting its existing value with a new value of a different data type), we strongly advise against doing so if you are still relatively new to ChoiceScript. It is easier to keep the game design logic clear in your own mind if you always use a specific set of variables for a specific purpose, and create new variables as needed for any additional purpose.

Choicescript_stats.txt and *set

It should be noted that the *set command works differently in choicescript_stats.txt than it does in an ordinary scene file. Unlike for an ordinary scene, on the Stats page it has only a temporary effect, adjusting data or other information for display purposes only. It does not actually save any changes. For example, if you

*set var + 15

on the Stats page, and assuming var previously had a value of 10, it will now display var with a value of 25. However, when the player exits that screen and returns to the game, var will still have the original value of 10.

TIP: If your game should require a range of data updating or recalculating on a regular basis - and with the new values actually being saved using *set - we recommend using a *gosub routine for this purpose.

Useful links

To properly understand variables and correct use of the *set command, we highly recommend studying the articles on Variable Types and Data Types respectively.

More commands / functions
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, Multireplace
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, Implicit Control Flow