ChoiceScript Wiki

As of a September 2022 update, Choicescript supports a new set of commands relating to arrays: *create_array, *temp_array, and *delete_array.

With *create_array, you provide a variable name, number of variables to create, and value for each variable, and it will automatically create a list of variables, each with an underscore and index number at the end to distinguish them. You can think of it as a quick way to *create a bunch of variables with similar names and value without typing each one out individually.

For example, if you have multiple characters with the same attribute, you can use the below command:

*create_array intelligence 5 50

Doing this would be like doing the following, but without having to type everything out:

*create intelligence_1 50

*create intelligence_2 50

*create intelligence_3 50

*create intelligence_4 50

*create intelligence_5 50


You can also view or change the variables using the syntax arrayname[index] or arrayname_index. The "index" portion can itself be another variable, so long as that variable is a number.

*set intelligence[1] 80

*set intelligence_2 30

*temp current_character 3


Character 1's strength is ${intelligence_1}.

Character 2's strength is ${intelligence[2]}.

The current character's intelligence is ${intelligence[current_character]}.

etc.

Although the above example shows numbers, arrays can store any type of variables: numerical, boolean, or string.

*Temp_Array and *Delete_Array[]

*temp_array works the same as *create_array, just the *temp version. I.e., the array will only apply to the current scene file and not elsewhere.

*delete_array, meanwhile, is like the *delete command for regular variables, and will delete a given array.

Pre-2022 Array Method[]

(Note: the below still applies and could be used in newer versions. It's simply not as convenient as the new array commands.)

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.