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

Tuesday, November 22, 2011

GameMaker - INI without an *.ini

So, say, you would like to load data from INI without creating an *.ini file somewhere.
There can be many reasons for doing so, be that security, performance, or simple fact of INI contents being defined inside of game's code.

There are two things to do about it - deciding on presentation of data, and creating a algorithm to load it. Presentation of data is relatively simple with INI - file is split into named sections, which contain named values. Since both use string index, using ds_map (sections) with other ds_map's inside (values) seems like an approriate choice. Below is my implementation.

Thursday, November 17, 2011

GameMaker - "sliding" views

This example illustrates setup of 'sliding' views, similar to ones seen in older console games (like Legend of Zelda).
In this case game is not paused while view is scrolling, making it slightly different from origins.
Download: (link)

GameMaker - get sprite's alpha channel

In some cases you may want to retrieve alpha channel of chosen sprite, to either apply it to other sprite, use for drawing, or in some other way.

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)).