Search code examples
arraysgame-maker

How can i set all items of an array to 0 in GameMaker studio?


Simple. I Need Help Setting All The Values Of An array to 0 in GameMaker: Studio. This is because i need to test if i have not modified the array by using

if array[id] != 0 {
//Enter code
}

Solution

  • Of course, there are several ways, choose the best depending on the circumstances.

    If you haven't filled anything in the array yet, adding a new item to a certain index initializes all previous values with "0":

    var array;
    array[length-1] = 0; //everything upto length-1 is filled
    

    If you did already create the array and wish to reset it you should loop over it:

    for (var i = array_get_length_1d(array) - 1; i >= 0; --i) {
        array[i] = 0;
    }
    

    If you do not care about the original memory locations, and you can create a complete new array, creating a new one in place of the old one can be slightly faster:

    array = 0; //destroys the old array
    array[length - 1] = 0; //recreates like in the first option