Home > SoulHow > SoulHow to use arrays

SoulHow to use arrays

September 3, 2008

NOTE: Comments are locked. I no longer answer questions about the Game Maker tutorials on this blog; I suggest you take any questions to the Game Maker Community. For more info, view the FAQ page.

Skill Level: Intermediate User (5)

Ever heard of those weird things called arrays?  Me neither.

Just kidding of course.  Anyways, arrays are much easier to understand than they sound.  Due to the fact that I realized I had actually never explained arrays in any blog entry (!!!), I’ll explain them here (as suggested by the article title…) If you don’t know what a variable is, either read the SoulHow to code in GML article, read the GM manual, or else you’re on your own.

Essentially, arrays are just a bunch of variables all strung together under one name.  Imagine if you and 9 of your buddies stood in a room.  Now imagine for some strange reason that you were all, in actuality, variables.  When standing randomly around the room, you are just 10 random variables.  However, imagine now you all stand in a line, close together.  Now you are all an array; a connection of all ten of you in a single line.

So instead of writing:

For arrays, you would write

Note that array indexes start at 0; so friend #1 is actually friend #0.

Now what is the benefit of this? It’s the same, even more typing for arrays. Never fear, I’ve got a bit of wizardry already prepared for you to check out.

When specifying the “friend numbers”, instead of having the number be part of the actual variable name, you can use an actual integer inside the brackets to specify the wanted index of the array. The index means, in this example, which friend you want to use.

Now let’s see what happens if we want to set every friend to 2. It seems okay to type Friend1=2 to Friend4=2. But now imagine you became immensely popular and got 96 more friends. You now have 100 friends standing in that room. It’s completely possible to type Friend1 to Friend100, if your life is really that pathetic (no offense).

But even if you did have the superhuman patience for that task, what if now you wanted to change them all to 3 instead of 2? You would have to go all the way back through each of the 100 “Friend”s and change the 2 to 3. Let’s think of a better way. Ah yes, the point of this article. The array.

Because of the magical integer-powered index super-system in the array, we can use the all powerful (okay, the somewhat-powerful) for loop. We start at the index 0, and loop until just before we reach 100. Note that we keep going until just before 100 because since the indexes begin at 0, they end at 99. Okay, on to the code.

var c;
for (c=0; c<100; c+=1) Friend[c]=2;

Remember to always declare temporary variables with var. That way they don’t conflict with any other variable in your object (var causes the variable to only exist in that script or piece of code; after the script/code ends, the variable disappears).

Okay, as you can see, using this one line of code, we have instantly set the 100 indices of our Friend array to equal two. Oops, you want it to equal 3? No problem. We only have to change them in one place.

So hopefully now you understand why arrays are very helpful in programming. Experiment to find even more ways to make arrays useful.

Note: Soon, I will write about arrays’ pro-only big brothers: data structures. Until then, though, leave comments, feedback, etc., etc… The usual.

If you enjoyed this article, please consider checking out the rest of the blog.

If you have a technical question, such as “How do I do this” or “this is not working right” (relative to something with your Game Maker game after reading this tutorial), please head over to the Game Maker Community and ask there.  I can’t answer any such questions in this blog and the members at the GMC will be more than capable to help you.  For more info, go to the FAQ page of this blog.

%d bloggers like this: