Variables

Parent Previous Next

Variables

Variables are items that store values.  These values can be updated during the game, and can be compared to in restrictions.


ADRIFT Developer must not be in Simple Mode for the Variables button to be visible on the main toolbar.


There are two types of variables in ADRIFT.  These are Number (more technically, Integers) and Text.  Both variable types can also be created as arrays (a set of variable values which can be accessed by a unique index starting from 1).


Variables can be compared with other values (variables, constants or expressions) in Restrictions and Expressions, and altered using Actions.


Clicking on the Variables button in the ribbon toolbar opens the following dialog:



The Variables dialog box defaults to Number variables.  If you wish to create a Text variable, change the radio button selection.  This will then change the window so that the Initial Value becomes a multi-line text area.  (Note, this is one of the few places where a text box in ADRIFT doesn't allow Alternative Descriptions).


The Number or Text variable should be given an initial value.  However, this is not mandatory as you may wish to change the value in a Task Action.

Displaying Variables

When referring to a variable, you must enclose it's name in % symbols.  In text boxes, ADRIFT will highlight any correctly identified variables by highlighting them in purple (see Syntax Highlighting).  


For example, you might describe a wallet like so:

It is a leather wallet containing %coins% gold coins.

This would simply output coins as a number, for example, "It is a leather wallet containing 7 gold coins."


You can also specify numeric values to be displayed in words using the NumberAsText function.  For example, the following text:

It is a leather wallet containing %NumberAsText[%coins%]% gold coins.

would create the equivalent output: "It is a leather wallet containing seven gold coins."

Arrays

An Array is a collection of values, each of which can be referenced using an index.  They allow a convenient way of accessing and writing to variable values, without referencing each variable individually.


For example, say you want to create three predefined values, and select one of these at random.  The conventional way might be to create three Text variables:

%value1% = "one"
%value2% = "two"
%value3% = "three"

To select one of these values at random, you would need to create another variable %index%, set this to a random value between 1 and 3, then create three tasks, each with restrictions such as "%index% must equal 1" which has action "set %finalvalue% = %value1%".


A more efficient way of doing this is to use an array.


When defining your variable, if you select the Variable is an Array checkbox, the Initial Values box will become multi-line (if it wasn't already), and you can enter any number of values.  Each unique value must be on a separate line.  The Length box will automatically update as you create entries in the Initial Values box.


So for the above example, you might create a variable %my_text_array% like so:



To access the variable value of an array, you need say which index within the array you wish to access.  Arrays in ADRIFT are 1 based (i.e. the first entry in the array is 1, the second 2 ... up until the last entry).  Array indexes must be supplied within square brackets.  So, to access the second entry in the above array, you would reference the value %my_text_array[2]%.


Array arguments are evaluated as Expressions, and so we can also supply the arguments using expression functions, without having to embed them.


So for example, to access a random entry within the array, you can simply use an expression such as:

set %finalvalue% = %my_text_array[RAND(1,3)]%


If you need to insert a carriage return within an array value when defining the initial values, you can use the HTML <br> markup code.


Further examples of accessing array variables might be:

%my_text_array[RAND(1,10)]%

Randomly outputs one of 10 different strings

%my_text_array[3]%

Outputs the third text string in the array

%my_text_array[%my_variable%]%

Uses an integer variable to select which text string to display

System Variables

Certain variable names have been reserved for System variables. These can be used in any text box to display the indicated value.


%Score% - This variable is where you keep track of the players score in the game.  It can be altered using Actions or used in Restrictions.  It has a hidden 'run once' functionality built in to it which prevents a particular task from incrementing its value more than once.


%MaxScore% - If this is not zero, then when the game finishes ADRIFT will display the message "In that game you scored %Score% out of a possible %MaxScore%, in %Turns% turns."