This article is aimed at beginners to ChoiceScript. However, if you have not already done so then the best place to start would be with the very first article in this Introduction To ChoiceScript Game Development series of linked articles: A Basic Tutorial.
This article discusses ChoiceScript Variables, their general purpose from a beginner's point of view, and the main differences between the two types of variables -- Temporary variables and Permanent ones. In an effort to cover the topic thoroughly for the beginner, it will doubtless repeat information given on different pages elsewhere in the ChoiceScriptDev Wiki.
'What are ChoiceScript Variables?
Variables are the means by which ChoiceScript stores game information, otherwise known as data, whether for a short-term, temporary purpose or a long-term, permanent one. They are also the means by which we actually make use of this stored data within our story scripting.
For a simple example of the difference between the two types of variables, consider that the player-character's name would be useful information to store permanently, for long-term, repeated use, whereas recording the fact that the protagonist drank twelve glasses of beer in the opening scene will likely only be of importance until shortly after -- when you need to know whether or not to describe his or her terrible hangover on the following morning . . . You may decide to use only a temporary variable for the latter purpose, and possibly call that variable drunk_as_a_skunk.
Naming ChoiceScript Variables
As that silly example demonstrates, variables of both types can be named almost anything you like, provided that the names given to each are unique and also do not contain any spaces. Under_scores_are_fine, however.
Variable names also should never contain any Upper Case (capital) letters, unlike (for instance) *label names. In addition, a variable name must always begin with an ordinary letter (a-z), but may thereafter contain numbers: for instance, it is common for similar variables to be named identically except for a number on the end (var1, var2, var3, etc.) which effectively makes each name unique.
The names of Permanent variables must always be unique, whereas any Temporary variables need only be unique within the currently-running scene file. Temporary variable names can therefore be reused in a different scene file, may in fact refer to something entirely different than previously, and may even contain a different Data Type than before (although this is perhaps inadvisable as it may serve only to confuse yourself).
Temporary variables are created within your story scripting as & when needed, using the *temp command.
Temporary variables are so named because as soon as your game loads a new scene file as a result of either a *finish or *goto_scene command, all of your temporary variables -- and their values -- are lost from memory. In essence, temporary variables can only be used within the current scene file in which they are created.
More information on when and how to use Temporary variables can be found on the page for *temp command.
Permanent variables are created by "declaring" them in advance as a series of *create commands in the startup.txt file. This process also sets their data type (also either numeric, string or boolean) and starting value -- even if only zero (if numeric), or blank (if a textual string type).
Once defined in this fashion, Permanent variables remain in existence for the duration of the game, carrying essential data forward from scene to scene, unless you purposely at some point choose to *delete one, although that will rarely, if ever, prove necessary.
Using ChoiceScript Variables
While step-by-step details of how precisely to make use of variables within your scripting is covered in the various pages for many of the individual *commands, there are some general points worth noting.
Both types of variables are always referenced in your scripting using simply their given name, which is why all variable names must always be unique when used within a particular scene file. Permanent variables, being essentially "global" (as they apply to all scene files), must therefore be uniquely named throughout -- i.e. no Temporary variable can ever be given the same name as a Permanent one.
Regardless of its starting value, the current value of any variable can also be changed -- either overwritten or simply modified -- using the *set command for both purposes. By overwritten we mean being *set to a completely new value regardless of its current value; by modified we mean its current value is actually taken into account when applying the change. As an example of the latter, if we use *set to add +15 to a numeric variable currently containing the value of 10, it will thereafter contain a value of 25: it has only been modified by the new change, not overwritten with a completely new value. This can be an important distinction to bear in mind.
And finally; of the three data types variables can contain, the stored values of two of these types (numeric and string variables respectively) can serve a very useful purpose when displayed for the player within the game. Both are subjects worth looking into further to fully appreciate the key role Variables play in ChoiceScript, and both are covered in more detail within this Tutorial.