Question: How do you determine the length of an array in GameMaker?
Answer
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?
Other Common Game Engines Questions (and Answers)
- Can You Use C# in Unreal Engine?
- Is Unreal Engine Open Source?
- Can Unreal Engine make 2D games?
- Does Unreal Engine Use Python?
- What Language Does Unreal Engine Use?
- Does Unreal Engine Use JavaScript?
- Does Unreal Engine work on Linux?
- How Do I Uninstall Unreal Engine 5?
- Is Blender or Unreal Engine Better?
- Does Unreal Engine Work on Mac?
- Why Is Unreal Engine So Laggy?
- Can I Run Unreal Engine 5 Without a Graphics Card?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost