Home Button News Button About & FAQ Button Members Button In.Dev Button Play Button Forum Button Resources Button Links Button Contact Button
Main Logo
Resources
 

ARTICLE ARTICLE
Written by Chronic Chronic
Posted on 2/May/2007
Arrays

What’s an array? Basically an array is a variable that can hold more than one value each kept in its own index.

To put it in simple terms, if you think of an array as a spread sheet... the variable is the spread sheets name, the index(s) are the columns/rows.

Arrays (in Game Maker) come in two kinds, a normal 1D array... and a 2D array.

1D Arrays

Ok, back that spread sheet metaphor, imaging a spread sheet with only one column, and a lot of rows (there is a max limit of 32000 rows), but instead of the common ABC123 labelling for the each cell, they are all numbers starting from 0... A bit like the picture below.



Lets call this table, "player_info", and we'll use it hold information like, name, lives, and health.

Ok, I hope your following this so far, because now I’m going to explain this in array terms, instead of spread sheets.

In Game Maker... 1D arrays are written like this...

:
name[index]


The "name" is the variable name; the "index" is the number,

So for us it could read...

:
player_info[0]


Now back to the spread sheet... Below I have filled in the table with some info about the player, on the right is how it would look in GML.



It’s a little simpler to understand with a visual aid such as a spread sheet... right?

That should cover enough of 1D arrays for you, now that you know 1D, 2D arrays should be simple for you to understand... well that’s only if you've followed me so far.

2D Arrays

Just like in 1D arrays, think of the spread sheet, except this time it has more than one column, but for this tutorial we'll only use 3 (0,1,2).

We'll use this array this time for 3 players, again called player_info:



In Game Maker... 2D arrays are written like this...

:
name[index,index]


The "name" is the variable name; the "index" is the number

So for us it could read...

:
player_info[0,0]


Here's the table filled in with some example info, like in the 1D example, we'll use name, lives and health.



In GML, it would look like this...

:
player_info[0,0] = "Chronic"
player_info[0,1] = 4
player_info[0,2] = 100

player_info[1,0] = "Bluntman"
player_info[1,1] = 5
player_info[1,2] = 47

player_info[2,0] = "Zod"
player_info[2,1] = 2
player_info[2,2] = 106


Reading from Arrays

Reading from an array is like reading from a normal variable, except you need to give the index(es) for the value your after..

:
draw_text(x,y,player_info[0]);


You could also use a for loop.. but don't forget to use string() if the array has "real" values as well as "string" values.

:
for (j=0; j<=2; j+=1) {
  draw_text(x,y+(16*j),string(player_info[j]))
}


The for loop would be a little different for a 2D array though.

:
for (j=0; j<=2; j+=1) {
  for (t=0; t<=2; t+=1) {
    draw_text(x+(60*j),y+(16*t),string(player_info[j,t]))
  }
}


I hope this tutorial has helped you understand arrays a little better than you did before reading.
 

Eo Logo   Link to Us | Feeds
2004 - 2018 © Eo
Website by House of Ninzha & Simon Donkers