The three virtues of a programmer: Laziness, Impatience, and Hubris. – Larry Wall

Static arrays

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

A static array is a composite data type in UnrealScript that groups multiple values of the same type. Static arrays, as opposed to dynamic arrays, have a fixed length that is specified as a constant value at compile time.

Declaration[edit]

Class properties, struct members, function parameters and local variables can be declared as static arrays by simply appending the array length in square brackets after the variable name:

var type varname[length];

The type can be any other UnrealScript type, with the exception of type bool and dynamic array types. Note that the compiler will not complain about dynamic array types and just silently use the dynamic array's base type instead!

Because the array length is specified as part of the variable name, it is possible to mix static arrays and non-array variables in a single declaration. The length can be any integer constant, including the ArrayCount pseudo function.

This kind of declaration also has an important implication: It is not possible to use static arrays as the return type of a function. You can get around that by using an out parameter instead.

Usage[edit]

To access an element of a static array, simply specify the name of the static array variable and append the element's index in square brackets:

varname[index]

Valid index values are positive integer values smaller than the array's length, i.e. 0 to ArrayCount(varname)-1. Array elements referenced this way are lvalues, i.e. they can be used on the left side of an assignment or as out parameter in a function call.

ArrayCount[edit]

The ArrayCount pseudo-function can be used to get the length of a static array. Note that it is actually evaluated at compile time and replaced by an integer value in the UnrealScript bytecode. As such it can be used even with a complex argument and is guaranteed to never throw any runtime warnings. Also, because it's not really a function call, ArrayCount can be used as the value of constants and as the length value of other static array declarations.

ArrayCount(staticarrayreference)

The static array reference could be as simple as the name of a static array variable in the same class or function or a more complex expression, such as class'TeamAI'.default.OrderList.