SC2Mapster.com Wiki

Arrays are a way to store and use multiple values of the same type.

int[10] list;
int i = 0;

while (i < 10)
{
    list[i] = i * i;
    i += 1;
}

print(list[5]); // 25
print(list[10]); // error, index overflow

The list then has 10 values in it, indexed from 0 to 9, filled with the values { 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }.

Arrays must have their length pre-declared. As far as Galaxy is concerned, int[10] is a completely different type from int[12].

Arrays can be of any type.

struct MyType
{
    int alpha;
    fixed bravo;
};

MyType[5] list;

Dynamic collections

There is no current way to make a dynamically-sized array or array-like structure, but you can simulate it by making a large array and keeping the current length around.

const int c_maxListLength = 100;

struct List {
    fixed[c_maxListLength] data;
    int length;
};

void List_Add(List* self, fixed value) {
    self.data[self.length] = value;
    self.length += 1;
}

fixed List_Pop(List* self) {
    self.length -= 1;
}

void List_Clear(List* self) {
    self.length = 0;
}

List list;
List_Add(&list, 5.0);
List_Add(&list, 10.0);
print(list.length); // 2
print(list.data[0]); // 5.0
print(list.data[1]); // 10.0

Multidimensional Arrays

Multidimensional arrays are available in Galaxy.

int[4][2] matrix;
int i = 0;
int j;

while (i < 4) {
    j = 0;
    while (j < 2) {
        matrix[i][j] = i * j;
        j += 1;
    }
    i += 1;
}

print(matrix[3][1]); // 3
print(matrix[2][0]); // 0

You must login to post a comment. Don't have an account? Register to get one!

Facts

Date created
Aug 17, 2010
Last updated
Jan 14, 2011

Authors