In this programming, we will learn how to work with arrays in Game Maker.

Arrays can contain many elements and then can be one-dimensional or multidimensional. Unlike other programming languages, an array in Game Maker does not need to be initialized. You can add an element to the array without array initialization. The name of the array comes first and its index is indicated in square brackets. Numbering starts from zero

inventory[0] = "bow"
inventory[1] = "arrow"

As a result, it will look like an array of two elements [“bow”, “arrow”]. Two-dimensional arrays are also often used. To do this, you need to add one more square brackets which will indicate the row

inventory[0][0] = "bow"
inventory[0][1] = "arrow"
inventory[1][0] = "sword"
inventory[1][0] = "shield"

In this case, it will look like a table with two rows and two columns.

For loop

A for loop is often used to fill an array with data or read from it. This significantly reduces the amount of code that needs to be written. For example, if you need to add 100 elements to the array, you can copy the assignment line of the element to the array 100 times, but it is not very convenient to work with it. Let’s create an empty array of 100 elements.

inventory[0] = "empty"
inventory[1] = "empty"
inventory[98] = "empty"
inventory[99] = "empty"

If you use for loop, it will be much more compact. The for loop looks like this

for (i = 0; i < 99, i++) {
    # CODE

i indicates where we start the countdown. Next comes the condition that i must be less than 99, as soon as it becomes 99, the cycle will end immediately. And at the end, we have i++, which means that I will increase by 1. Thus, when the loop starts, we have i = 0. After the first pass of the loop, i increased by 1 and becomes equal to i = 1. And so on until i becomes 99. Then the cycle ends.

Let’s try to create an empty one-dimensional array using a loop.

for (i = 0; i < 99, i++) {
    inventory[i] = "empty"

Thus, at each iteration of the loop, i will increase and we will create an array of 100 elements with the value “empty”. This can be added to the Draw event to display the array. But before that, it must be created. Its creation is defined in the Create event. In draw_text, I will add 70*i to the x coordinate, this is to create an indentation between the elements of the array. When i = 0, 70*i will also be 0. Then it will be 70, 140, and so on.

for (i = 0; i < 99, i++) {
    draw_text(10+70*i, 10, inventory[i])


A two-dimensional array can be created in the same way, only we will need two loops

for (i = 0; i < 99, i++) {
    for (j = 0; j < 99, i++) {
        inventory[i][j] = "empty"

What will happen in this case? First, we start the first loop where i = 0, then we immediately get into the second loop where j = 0. And we make the first entry in the array at index [0][0]. Further, i remains 0, and j increases by 1. Therefore, we make an entry in the array at index [0][1] and so on until j becomes 99. Then the nested loop ends and we return to the loop with i. Here i will increase by 1 and again go into a nested loop where j is equal to 0. And we will write to the index [1][0] and again until j becomes 99 and i will increase by 1 until will reach 99 and then the loop will end.

To display a two-dimensional array, two cycles also be required. And everything else is the same as with one-dimensional

for (i = 0; i < 99, i++) {
    for (j = 0; j < 99, i++) {
        draw_text(10+70*i, 10+70*j, inventory[i][j])