Introducing Dragonfly Cloud! Learn More

Question: How do you determine the length of an array in GameMaker?


In GameMaker, you can determine the length of a one-dimensional array using the built-in array_length_1d function. This function returns the size of the array in terms of the number of elements it contains.

Here is a simple code example illustrating how to use array_length_1d:

// Create a one-dimensional array var myArray = [10, 20, 30, 40, 50]; // Get the length of the array var length = array_length_1d(myArray); // Display the length show_debug_message("The length of the array is: " + string(length));

For two-dimensional arrays, GameMaker also offers the functions array_height_2d and array_width_2d, which return the height (number of rows) and the width (number of columns) respectively. Below is an example of how to find the size of a two-dimensional array:

// Create a two-dimensional array var my2DArray; my2DArray[0,0] = 11; my2DArray[0,1] = 12; my2DArray[1,0] = 21; my2DArray[1,1] = 22; // Get the height and width of the array var height = array_height_2d(my2DArray); var width = array_width_2d(my2DArray); // Display the dimensions show_debug_message("The array has " + string(height) + " rows and " + string(width) + " columns.");

Note that in GameMaker Studio 2.3 and above, arrays are more dynamic and can have varying lengths for each index when dealing with two-dimensional arrays. Therefore, you might need to loop through each "row" to get the length of each "column" if they are not consistent.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.