ChoiceScript Wiki

Choicescript supports an array syntax to access a specific entry out of a sequence of related data points. If you use square brackets [ ] after a variable name, the value of a second variable will be concatenated using underscore. See this example.

Suppose you have multiple characters in your game. You can store their strength as follows:

*set strength_0 100

*set strength_1 90

*set strength_2 80

You can then access any of the values using the syntax strength[index]. This will print "current strength: 80

*set index 2

Current strength ${strength[index]}

The square brackets [ ] are simply replaced with underscore and the value of the variable index, so this is equivalent to either:

Current strength ${strength_2}

Current strength ${strength[2]}

This is very useful in a loop or when your output depends on a variable.

Note that all entries (in this example, strength_0, strength_1, strength_2) must be created (with *create or *temp) before their use. Reading or writing strength[3] will give an error.

Additional usages[]

The variable in brackets can also be a string. This will allow developers to keep related features together (similar to fields in a structure or class in other languages). See example:

*set character_hair "brown"
*set character_eyes "hazel"
*set feature "eyes"
Your character has ${character["hair"]} hair and ${character[feature]} eyes.

Known limitations[]

At creation, the variable name must be fully spelled out - you can't use the array syntax at creation. These will all fail.

*create strength[0] 100     // will fail
*set index 0
*create strength[index] 100 // will fail
*temp strength[index] 100    // will fail

This means that the maximum size of an array must be chosen at the moment of writing your scene and cannot be changed. For instance, if you plan to have a maximum of ten characters, you will write:

*set strength_0
*set strength_1
 *set strength_9

You won't be able to add characters later.