Using Arrays in Scripts

Script variables can be configured as arrays by starting the variable name with a hash (#) or an at symbol (@). Variables starting with a hash are global arrays, while variables starting with an at symbol are local to individual characters. Once a variable is created, you cannot change whether it is an array variable (but its locality can be changed). Array variables can only store integer values, not text.

An array variable is effectively a set of index-value pairs, where each index has a value (defaulting to 0). Therefore, an arbitary number of values can be stored under a single variable name. In character scripts, all array indexes are integers, however unlike many programming languages, arrays allow negative indexes to be used.

Setting array values

Values can be set in an array variable using the Set array var command. The value at a specified index can be set either to an integer value, or an expression that will resolve to such a value.

Additionally, multiple array values can be set by providing multiple values or expressions, separated by a semi-colon (;). The first value will be assigned at the specified index, and each subsequent value will be assigned to the next index. For example, if the specified index is 3, the first value is assigned to the at position 3, the second value to position 4, and so on.

The Set array var at command works in the same way, but allows for remote variable assignment, as with Set var at. Both commands support overriding the specified index using _MyScriptF.

Values cannot be set for an index that is less than -50,000 or above 50,000.

Querying array values

Like any other script variable, an array value can be used anywhere an integer value is expected. The one difference is that the variable name must be followed by a pair of square brackets ([]) containing an index value. This can be a numeric value or an expression that resolves to such a value. If an index that has not been set is accessed, a default value of 0 is returned.

Querying full arrays

Two commands operate over an entire array, checking all set values to provide a concise way to determine its state:

Examples of valid array expressions

Clearing arrays

The Clear array var command can be used to reset all values in an array variable to zero.


Command list
Character Scripting