Jump to content


Photo
* * * * * 3 votes

Array Tutorial


  • Please log in to reply
71 replies to this topic

#1 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 05 October 2003 - 01:20 AM

  • Title: Array Tutorial
  • Description: An introduction to the use of arrays.
  • GM Version: GM5 to Present.
  • Registered: No
  • File Type: N/A
  • File Size: N/A
  • File Link: N/A
This tutorial is also available on my site in a text document, i've posted it here for those who have no yet seen it. I've had quite a few comments from people saying that its helped them, I hope it helps more people.

Array Tutorial by Chronic

What is 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.

0
  .---------.
0 |         |
  |---------|
1 |         |
  |---------|
2 |         |
  '---------'
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 would 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.

0
  .---------.
0 | Chronic |     player_info[0] = "Chronic"
  |---------|
1 |    4    |     player_info[1] = 4
  |---------|
2 |   100   |     player_info[2] = 100
  '---------'
Its 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 thats 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

0         1         2
  .-----------------------------.
0 |         |         |         |
  |---------|---------|---------|
1 |         |         |         |
  |---------|---------|---------|
2 |         |         |         |
  '-----------------------------'
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 would read...

player_info[0,0]
Here is the table filled in with some example info, like in the 1D example, we'll use name, lives and health.

0         1         2
  .------------------------------.
0 | Chronic | Bluntman |   Zod   |
  |---------|----------|---------|
1 |    4    |     5    |    2    |
  |---------|----------|---------|
2 |   100   |    47    |   106   |
  '------------------------------'
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 varible, 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 (i=0; i<=2; i+=1) { draw_text(x,y+(16*i), string(player_info[i])) }
The for loop would be a little different for a 2D array though.

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

You can find this tutorial along with many others, including a downloadable help file containing them all at my website: http://www.Chronic667.com.


Search Tags
  • GMCTUTORIAL
  • GMCGML
  • GMCARRAY

  • 12

#2 Ablach Blackrat

Ablach Blackrat

    GMC Member

  • Retired Staff
  • 1215 posts
  • Version:GM8

Posted 05 October 2003 - 03:17 AM

This is the tutorial that finally helped me to understand arrays a while back. Thanks :lol:

But there's is still one thing I've never been able to figure out : how to initialize arrays before actually using them. Up until now, I've been setting all uninitialized variables to be treated as zero, but I would like to learn how.

Here's an example...

array(1,50)=0;
for (a=0; a<51; a+=1;) array(0,a)=50;
for (a=0; a<51; a+=1;) array(1,a)=100;

I had assumed that the first line would set the upper limit of the array and intialize it at the same time. It doesn't. It says unknown script or variable. So how do you get GM to recognize an array variable so you can use it?

Edited by Ablach Blackrat, 05 October 2003 - 03:19 AM.

  • 0

#3 Chris

Chris

    Banned From Oxygen

  • New Member
  • 1 posts

Posted 05 October 2003 - 03:29 AM

You shouldn't have to initialize an array in gml. This is done for you dynamically. It seems the issue you are having is the use of parenthesis instead of brackets. Use "array[]" rather than "array()." In GML, and any programming language pretty much, using () will cause the interpreter/compiler to assume a function.
  • 0

#4 Ablach Blackrat

Ablach Blackrat

    GMC Member

  • Retired Staff
  • 1215 posts
  • Version:GM8

Posted 05 October 2003 - 03:34 AM

Oh, sorry. That was a typo. I do use the other brackets for my arrays. Hmmm...I'm going to have to think about for a while. Something is definately weird with my programming. Or maybe its just me.
  • 0

#5 Jodder

Jodder

    Da Prof

  • Retired Staff
  • 1410 posts

Posted 06 October 2003 - 06:12 PM

You just initialize an array like so:
array[]=0

no number between the "[]"

This will not initialize the array, it will only initialize the zeroth element.

IE
a[]=1
a[0]=1
Are functionaly identical, an empty set of square brackets, implies zero.

To initialize an array you can set GM to treat all unitialized variables as zero and then all elements will be treated as zero until a value is explicitly assigned, or use some kind of loop.

Single dimensional arrays use a single loop, two dimensional arrays need nested loops.

//set array_x[10] to all 1's
for (i=0; i<=10; i+=1) 
    array_x[i]=1;

//set array_y[2,5] to all 10's
for (i=0; i<=2; i+=1) 
  for (j=0; j<=5; j+=1)
     array_y[i,j]=10;


Jodder
  • 0

#6 agerhbour

agerhbour

    GMC Member

  • GMC Member
  • 3 posts

Posted 13 October 2006 - 11:47 AM

this is good, but im still having a bit of trouble understanding loops.
  • 0

#7 eFFeeMMe

eFFeeMMe

    GMC Member

  • New Member
  • 5 posts

Posted 14 October 2006 - 11:44 AM

I'm making a little OT to answer agerhbour's question (well, I think it was a question?):
Loops are just that: loops. Imagine them as "if" variations repeated multiple times.
For instance, the for statement is just a sequence of three expressions and one condition in this order:
for(#1 a statement which is only executed at the start of the loop; #2 a condition, if true goes to #3; #4 a statement, once finished goes back to #2){#3 action, once finished goes to #4}

The array tutorial is really well done, Chronic, not logorroic yet not too simplistic. I wouls revise a bit the ASCII tabel stuff though.
  • 0

#8 chaz13

chaz13

    GMC Member

  • GMC Member
  • 4212 posts
  • Version:Unknown

Posted 17 October 2006 - 07:31 PM

The only part i do not get about (1d) arrays, is why not use variables instead?
  • 0

#9 Master Pobble-Wobble

Master Pobble-Wobble

    GMC Member

  • New Member
  • 352 posts

Posted 17 October 2006 - 11:40 PM

It's because you can loop through them easier. If I want to check the list of multiple variables that are numbered, wouldn't it be easier to check with a loop? Same with setting their values.
  • 0

#10 beebear2001

beebear2001

    GMC Member

  • New Member
  • 4 posts

Posted 19 December 2006 - 01:04 AM

[quote name='Chronic' date='Oct 4 2003, 09:23 PM']
This tutorial is also avaliable on my site in a text document, i've posted it here for those who have no yet seen it. I've had quite a few comments from peopel saying that its helped them, I hope it helps more people.
==============================

I have a question about arrays:

I am new to GM and an working on a non shoot-em-up game. Basically I place a picture on the background for a room. I have a 10 X 7 grid layout with the grid size set to 64X64. I want to cover the background picture with individual 64X64 tiles with pictures on them. The tiles will have to be in sets of 2 so each one will have a match. Then above that layer (using the depth setting) there will be another layer of tiles all with the same picture or design on it. This will be a match game where you will click on the "top" tile to reveal the pictured tile below it. When you get 2 matching tiles they will be removed to show that portion of the background picture. Object of the game is to remove all the tiles either in a timed fasion or not to reveal the background picture.

So with that information, I would like to understand how I could use an array to randomally place the tiles, in pairs of 2, on my grid so I can keep track of them to see if there is a match when 2 are selected. Any information would be helpful. I have been searching through the forums and reading the manual. If anyone would have an example??? Thank you in advance

John
  • 0

#11 Merko

Merko

    GMC Member

  • New Member
  • 5 posts

Posted 27 December 2006 - 08:25 PM

Good Job!

Question: Is it posible to set an array? For example if player1's energy equal to 0
the number 0 is red and if not the number is green?

sry for that bad English!
I'm a German 13-year-old kid o_O :)
  • 0

#12 Girbicid

Girbicid

    GMC Member

  • New Member
  • 1 posts

Posted 05 January 2007 - 06:08 PM

Sometimes 2d arrays don't enought, in reason that GM allows only 1d and 2d, i use array in array, that allows to create more dimensional arrays
for example 3d array :

array1[array2[array2_x,array2_y],array1_y]
  • 0

#13 SunTzus

SunTzus

    Ying Yang Twinz

  • New Member
  • 1047 posts

Posted 10 January 2007 - 11:42 PM

You just initialize an array like so:
array[]=0

no number between the "[]"

This will not initialize the array, it will only initialize the zeroth element.

IE
a[]=1
a[0]=1
Are functionaly identical, an empty set of square brackets, implies zero.

To initialize an array you can set GM to treat all unitialized variables as zero and then all elements will be treated as zero until a value is explicitly assigned, or use some kind of loop.

Single dimensional arrays use a single loop, two dimensional arrays need nested loops.

//set array_x[10] to all 1's
for (i=0; i<=10; i+=1) 
    array_x[i]=1;

//set array_y[2,5] to all 10's
for (i=0; i<=2; i+=1) 
  for (j=0; j<=5; j+=1)
     array_y[i,j]=10;


Jodder

<{POST_SNAPBACK}>


Hehe, rather then putting all that stuff why not just go to global game settings and check "Treat Unintiliazed Variables as zero" If you do that it also works 4 variables :)
  • 0

#14 Big J

Big J

    GMC Member

  • GMC Member
  • 2848 posts
  • Version:GM8.1

Posted 06 March 2007 - 11:12 PM

Because, if you do that, you won't find your programming mistakes, and the variable_local_exists() and variable_global_exists() functions will always return true. I know this is an old thread, but I think it would be better to bump this one up, rather than having another array thread.
  • 0

#15 Lucy-in-the-Sky

Lucy-in-the-Sky

    GMC Member

  • New Member
  • 18 posts

Posted 17 March 2007 - 09:56 PM

Im still a little confused. Does the array just go in an object under a create event? Or in the rooms creation code? And does the object have to have the same name as the array, so it goes purple?

And does the code for reading the array just go underneath it? Or can it go anywhere?
  • 0

#16 novaz

novaz

    GMC Member

  • GMC Member
  • 587 posts
  • Version:Unknown

Posted 18 March 2007 - 11:03 PM

no, you can create an array in every event. The words are purple when
there is something with that name, if you create a sprite, a path, a timeline,
a script, an object etc. the word becomes purple. The object name can be
different from the array name of course. You can use the array in every object,
for example, in object "start" you have:

myarray[1]="apple"
myarray[2]="banana"
myarray[3]="star"

in another object you can use:

for (a=1;a<4;a+=1) {
show_message(start.myarray[a])
}

it will show 3 messages with the words in the array.
  • 0

#17 Big J

Big J

    GMC Member

  • GMC Member
  • 2848 posts
  • Version:GM8.1

Posted 21 March 2007 - 12:33 AM

Arrays and other user-defined variables will not be color-coded, AKA purple.

Arrays work the same way as normal variables, except they can hold multiple values.
  • 0

#18 molloyboy08

molloyboy08

    pyr0t3chnic

  • New Member
  • 473 posts

Posted 22 March 2007 - 12:23 AM

Arrays are used to store data, its sorta like a bunch of variables, just easier to sort through. As long as everything is set right you can assign values to the array and its index(or pl), anywhere you can put code. You can read it anywhere code can go too, just make sure the values are assigned first.
As for the name of the array, just answer this and you have your answer, does a variable need to have the same name as an object? no, in fact it can't, if it does how would the game know the difference. the object name is in fact a variable that you cant change containing the "reference number" for it.
Hope it helped, if it made it worse just completely ignore my post
  • 0

#19 PLAY: More

PLAY: More

    Press F8 to enter BIOS

  • New Member
  • 824 posts

Posted 24 March 2007 - 07:06 AM

Im still a little confused. Does the array just go in an object under a create event? Or in the rooms creation code? And does the object have to have the same name as the array, so it goes purple?

And does the code for reading the array just go underneath it? Or can it go anywhere?

<{POST_SNAPBACK}>

The array can be created anywhere.
You will get an error if the array name is the same as an object name.
The code must be after the array is created.
  • 0

#20 Rakech

Rakech

    GMC Member

  • New Member
  • 168 posts

Posted 10 April 2007 - 07:39 PM

0 | Chronic | player_info[0] = "Chronic"
|---------|
1 | 4 | player_info[1] = 4
|---------|
2 | 100 | player_info[2] = 100
'---------'

can u just use three variables and whats the advantage,soz for being nub

Rakech :D
  • 0

#21 inarma

inarma

    GMC Member

  • GMC Member
  • 599 posts

Posted 15 April 2007 - 03:32 PM

How can you make it so an array will hold an object, because I am working on an rpg and I would like a 2x2 array, to hold items, and the items would all be stackable. Is this possible? If so tell me.

Edit: Sorry meant 2x2 not 3x3 >.>

Edited by crystal bow, 19 April 2007 - 03:38 AM.

  • 0

#22 Chronic

Chronic

    Administrator

  • Global Moderators
  • 2729 posts
  • Version:GM:Studio

Posted 16 April 2007 - 03:59 PM

Game Maker doesn't support 3d arrays (array[a,b,c]). But what you are wanting can be done with a 1d array. There are plenty examples/tutorials that cover inventories.
  • 0

#23 DocRabbit

DocRabbit

    GMC Member

  • New Member
  • 3 posts

Posted 02 May 2007 - 11:48 AM

0 | Chronic |            player_info[0] = "Chronic"
        |---------|
      1 |    4    |            player_info[1] = 4
        |---------|
      2 |  100  |            player_info[2] = 100
        '---------'

can u just use three variables and whats the advantage,soz for being nub

Rakech :)

<{POST_SNAPBACK}>

Here is a real world example of why arrays are better and something everyone should be able to visualize.

You have a checkerboard. You want to describe where the pieces are at any given time, and if they are red or black.
A 2d array is the fastest and simplest way to do this, and why arrays are best for anything grid based.

checkerboard:
array[8,8] is the checkboard.

top left corner of checkerboard array[0,0]
bottom right corner of checkerboard array[7,7]
value for pieces present:
none=0
red=1
black=2

initialize board:(not looking so checker experts bear with me if off a square here)
RED
[X][_][X][_][X][_][X][_]
[_][X][_][X][_][X][_][X]
[X][_][X][_][X][_][X][_]
[_][_][_][_][_][_][_][_]
[_][_][_][_][_][_][_][_]
[_][X][_][X][_][X][_][X]
[X][_][X][_][X][_][X][_]
[_][X][_][X][_][X][_][X]
BLACK

First row top red side:
array[0,0]=1
array[0,2]=1
array[0,4]=1
array[0,6]=1

Second row top red side:
array[1,1]=1
array[1,3]=1
array[1,5]=1
array[1,7]=1

etc...
for(i=0;i<8;i+=1){
for(j=0;j<8;j+=1){
insert code to evaluate whole board, test for red, black, or whatever here.
just check array[i,j] for all its possibilities of value.
case 0......
case 1......
case 2.....
}
}

If you used variables for this example instead, you would have 64 variables, and 64 if statements with at least ONE elseif and else PER IF just to check what piece is there.
  • 0

#24 Pie Person!

Pie Person!

    GM 6+ Lover

  • GMC Member
  • 1973 posts

Posted 04 May 2007 - 01:23 AM

This really helps! I never new arrays were that simple.
  • 0

#25 Pie Person!

Pie Person!

    GM 6+ Lover

  • GMC Member
  • 1973 posts

Posted 05 May 2007 - 03:15 AM

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

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


What does the "i" mean? Also what are curly brackets used for? "{}"
  • 0

#26 GHZteam

GHZteam

    GMC Member

  • GMC Member
  • 266 posts
  • Version:GM:Studio

Posted 09 May 2007 - 04:51 PM

Kudos on the 'grid-like' example. I'm sure you've helped many people on this topic ;)
  • 0

#27 uncfan_2563

uncfan_2563

    GMC Member

  • New Member
  • 207 posts

Posted 23 October 2008 - 01:46 AM

WOW, actally understand how easy arrays are now, thanks a lot
  • 0

#28 Banjo-Kong

Banjo-Kong

    GMC Member

  • New Member
  • 31 posts

Posted 24 October 2008 - 11:04 PM

Thank you for the tutorial. :GM7:
  • 0

#29 Poris

Poris

    GMC Member

  • New Member
  • 533 posts

Posted 24 October 2008 - 11:36 PM

This helped lots! :GM7:
  • 0

#30 MythicRuler

MythicRuler

    GMC Member

  • New Member
  • 438 posts

Posted 26 October 2008 - 02:23 AM

Can somebody explain to me why I would use an array instead of just variables? I dont quite understand the uses of arrays.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users