Multidimensional Arrays

Extended only. In Original mode, fixed-size square-bracket arrays such as Local slots[7] take a single dimension. Select Extended mode to give them two or more. Dim arrays can have several dimensions in both modes; see Arrays.

Declaration

Local name:ElementType[upperBound, upperBound [, upperBound ...]]

Every upper bound is inclusive, zero-based, and must be a non-negative compile-time Int constant. A bound of 7 therefore provides indices 0 through 7. The total element count is the product of the size of every dimension:

Local grid:Int[7, 5]  ; (7 + 1) * (5 + 1) = 48 elements

Fixed arrays may contain Int, Float, String, concrete-Type, or Interface values. A single bound declares a one-dimensional fixed array; comma-separated bounds declare a multidimensional fixed array.

Reading and writing elements

Supply exactly one square-bracket index for every declared dimension. Each index is checked against its dimension's inclusive upper bound in debug builds.

Local terrainHeight:Float[127, 127]

terrainHeight[10, 20] = 4.5
Print terrainHeight[10, 20]

Additional bounds create additional dimensions:

Local lightLevel:Float[31, 23, 7]

lightLevel[4, 12, 2] = 0.75

The first dimension varies fastest in memory. Bounds and total allocation size are checked by the compiler so an invalid or overflowing declaration is rejected.

Storage contexts

Multidimensional fixed arrays may be locals, globals, custom-Type fields, and Function parameters. A parameter includes the exact bounds expected by the Function:

Global board:Int[7, 7]

Type Level
    Field tiles:Int[31, 31]
End Type

Function LastCell:Int(values:Int[1, 2])
    Return values[1, 2]
End Function

All storage is allocated with its variable or containing object and cannot be resized. Fixed arrays cannot be assigned or returned as whole values, and collection Types cannot be nested inside them.

Dim arrays

Code that must also compile in Original mode can use a Dim array instead. It takes several dimensions in both modes, can be resized by running Dim again, and is shared by the whole program:

Dim heights#(127, 127)
heights(10, 20) = 4.5

See Arrays for Dim's declaration, resizing, element types, and custom-Type array rules.