Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Sunday, November 27, 2011

GameMaker - multi-dimensional arrays

As you may know, GameMaker natively supports only 2-dimensional arrays with size up to 32000x32000.
However, in some cases, you may want to use arrays with 3 (or more) dimensions.
Small comparison table with descriptions below:

Stat\Method array:offset array:instance ds_grid's ds_map
Init (fill-up) Slow Slow Fast Slowest
Access (i/o) Fast Fast Average Average
Size limit ~109 cells 32k/dimension Resizable No
Index Integer Integer Integer String
Existance check No No Not needed Yes
Region operations No No Yes Only clearing
References No Partial (layer) Yes Yes

Wednesday, November 16, 2011

GameMaker - shuffling an array

In some cases you may want to shuffle contents of array.
Out of those, it may also happen that it can not be easily replaced by ds_list.
The following code can be used to sort an array relatively fast and finely:
var i, j, k;
for (i = 0; i < size; i += 1)
{
    j = irandom_range(i, size - 1)
    if (i != j)
    {
        k = data[i]
        data[i] = data[j]
        data[j] = k
    }
}
Where data is name of array variable, and size is array length (with elements being stored in indexes from 0 to (size - 1)).