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

  • 14

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#2 Ablach Blackrat

Ablach Blackrat

    GMC Member

  • GMC Elder
  • 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

  • GMC Elder
  • 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

  • GMC Elder
  • 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
How many Game Maker members does it take to change a light bulb?
100. One registered member to buy the bulb, and 99 unregistered members to sit around complaining; that they should get to use as much light as the guy who paid for it.

-----------------
In Mod Preview; nobody can hear you scream!

#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
  • 4213 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
  • 2853 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

Get your GM 8.1 Anti-Aliasing here!

2712265.png

http://www.youtube.com/SporkinatorBZ


#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
  • 592 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
  • 2853 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

Get your GM 8.1 Anti-Aliasing here!

2712265.png

http://www.youtube.com/SporkinatorBZ


#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
Returning after a break from gamemaker.
Gamemaker experience: 6,7 both paid. 8 soon to be pro
Programming knowledge: (May be missing a few currently)
C, C#, C++, java, HTML, CSS, Javascript, PHP, ASP.NET, SQL, Visual Basic, Perl, assembly, batch

Temporary sig while I figure out if I have anything worthwhile to put.

#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
Posted Image
Posted ImagePosted ImagePosted Image

#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

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#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
Cool.

#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
Cool.

#26 Tulloch

Tulloch

    GMC Member

  • GMC Member
  • 562 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

2a5amo.jpg8y9kbp.jpgs28pow.jpg
- Help us out! LIKE and SHARE our Facebook page! -
- Check out some great music! HERE -


#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
Posted Image
Play Scratch!
Home Page

Ever felt like programming a game in Java but couldn't do it? Perhaps my Game-Maker interface will help you! It surely helped me.
Coffee Maker - Perhaps the world's only Game Maker - Java connection.
Posted Image

#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

#31 Poris

Poris

    GMC Member

  • New Member
  • 533 posts

Posted 26 October 2008 - 03:20 AM

It is much more organized, and it is sometimes easier to represent things.
  • 0
Posted Image
Play Scratch!
Home Page

Ever felt like programming a game in Java but couldn't do it? Perhaps my Game-Maker interface will help you! It surely helped me.
Coffee Maker - Perhaps the world's only Game Maker - Java connection.
Posted Image

#32 Chronic

Chronic

    Administrator

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

Posted 26 October 2008 - 02:32 PM

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

Take a look at my menu tutorial for an idea how they can be more useful than variables.
  • 0

My recent tutorials: [Auto-Tiling] [Text Based Menu] [Sprite Based Menu] [View more at my site]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
R.I.P Mom (1951-2008)


#33 Potado

Potado

    GMC Member

  • New Member
  • 401 posts

Posted 06 November 2008 - 01:54 AM

wow, I acually understand them now!
Thanks so much!
  • 0

#34 Murloc

Murloc

    GMC Member

  • New Member
  • 179 posts

Posted 13 November 2008 - 06:04 PM

A bunch of new understanding in arrays,now I will be able to finish what I've started with arrays yay! ;)

Thanks for tut ::lmao::

~Murloc.
  • 0
Proud Gamemaker 7 PRO user.

Theory-When you know everything,but nothing works.
Practice-When everything works,but you don't know why.
Programming merges these two-Nothing works,and you don't know why.

#35 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1956 posts
  • Version:GM8

Posted 29 December 2008 - 03:54 AM

Wow. I didn't understand much of what arrays were(I thought they were just values enclosed with []), until I read this tutorial. No wonder it is a staff nomination. -_-
  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#36 joelj

joelj

    GMC Member

  • New Member
  • 1 posts

Posted 25 February 2009 - 11:20 AM

What if I have an array, that I want initialised like

1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1


Is there some way I can do something like:

arrayGrid[] = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, .... ] etc..?
  • 0

#37 flexaplex

flexaplex

    GMC Member

  • Global Moderators
  • 4814 posts
  • Version:GM8

Posted 25 February 2009 - 10:36 PM

What if I have an array, that I want initialised like

1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1


Is there some way I can do something like:

arrayGrid[] = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, .... ] etc..?

Use a double for loop.

var i,j;
for (j = 0; j < array_height; j += 1)
{
  for (i = 0; i < array_width; i += 1)
  {
	if (i == 0 || i == array_width-1 || j == 0 || j == array_height-1)
	{
	  array[i,j] = 1;
	}
	else
	{
	  array[i,j] = 0;
	}
  }
}

Edited by flexaplex, 26 February 2009 - 04:00 AM.

  • 1
:]

#38 vanquish

vanquish

    GMC Member

  • New Member
  • 71 posts

Posted 12 April 2009 - 03:53 PM

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

I had trouble understanding this at first too, but I think the simple answer is that arrays allow the computer to FIND the variables on its own, based on equations and loops. Also, they allow more efficient storage of your variables. For instance, instead of typing five strings for five names, you can just type one array with the number five.
  • 1

#39 zeroonea

zeroonea

    GMC Member

  • New Member
  • 41 posts

Posted 21 May 2009 - 09:00 PM

are there have a different about memory when i use:

a[0] = 100;

vs

a[1000] = 100;
  • 0

#40 flexaplex

flexaplex

    GMC Member

  • Global Moderators
  • 4814 posts
  • Version:GM8

Posted 21 May 2009 - 09:04 PM

Yes.

When you assign an array all the indexes down to 0 are automatically initiated with the value 0.
  • 0
:]

#41 JanDeis

JanDeis

    GMC Member

  • New Member
  • 42 posts

Posted 05 November 2009 - 05:21 PM

So I have just one question about GM arrays:

In C++ when using an array (and anything for that matter) you must initialize it first.
[example]
double numbers[5];

Then from there you may use the array. Is there any sort of array initialization in GM or does GM automatically allocate more memory to the array as you use larger numbers in the brackets [] ?
  • 0

#42 DZiW

DZiW

    GMC Member

  • GMC Member
  • 840 posts
  • Version:Unknown

Posted 05 November 2009 - 06:49 PM

That's right, including

You can use 1- and 2-dimensional arrays in GML. Simply put the index between square brackets for a 1-dimensional array, and the two indices with a comma between them for 2-dimensional arrays. At the moment you use an index the array is generated. Each array runs from index 0. So be careful with using large indices because memory for a large array will be reserved. Never use negative indices. The system puts a limit of 32000 on each index and 1000000 on the total size.


  • 0

YOU CREATE: WHERE LiFE MAY FAiL, DEAћ SHALL NOT!


#43 riffraff

riffraff

    GMC Member

  • New Member
  • 41 posts

Posted 06 November 2009 - 05:39 PM

im un sure so could 1 of you nuce enough dudes send a example to gamemakerkfb@aol.com cheers.
Also another question:how do you actuly declare a array?
  • 0

#44 williamssam81

williamssam81

    GMC Member

  • New Member
  • 1 posts

Posted 13 November 2009 - 05:19 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

#45 kaliman777

kaliman777

    GMC Member

  • GMC Member
  • 125 posts

Posted 03 December 2009 - 06:11 PM

It seems good, but that pile of AAAAA make me confused? I dont know if are part of the contents or error of the page.

Thanks anyway!!!
  • 0

#46 FoxInABox

FoxInABox

    GMC Member

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

Posted 19 December 2009 - 06:00 AM

if you do see alot of , then copy it all to notepad, mark an and one space in front of it, then copy it with ctrl+c .. now press ctrl+h to enter the replace function of notepad, then press ctrl+v to paste the text you copied into the search field, write one single space in the replace field and then press "replace all", it should now be perfectly readable again.. enjoy

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? "{}"

the "i" is simply just a variable that is named "i", the letter "i" is more frequently used because ppl ofen made a list of instances, and used only the first letter to make it short, but it could as well had been "ins", "a" or some other variable name.

the bracers are to indicate where the code inside the loop starts and ends .. if you don't use it, then only the next function will be included in the loop.. so you only need it if you want more then one..

@JanDeis: to define a 1D array, just set the highest array you want to a value..
array[4] = 5;

array[4] will be given the value 5.. while array[0] to array[3] will be given the default value 0 when you define it

in a 2D array it is a bit diffrent:
a[3,3] = 5;

will just add the value 0 to a[3,0] to a[3,2], while a[3,3] will be given the value 5
a[0,*] to a[2,*] is yet undefined.. so to define all in a 4x4 2D array you must write:

for(b=0;b<4;b+=1) a[b,3]=0;

@williamssam81: quite right, if you know the width then you can make a 1D array a multi dimentional array:

array that got a width of 3:

a[0,0]=1;
a[0,1]=2;
a[0,2]=3;
a[1,0]=4;
a[1,1]=5;
a[1,2]=6;

will work even as good if you did:

b[0*3+0]=1;
b[0*3+1]=2;
b[0*3+2]=3;
b[1*3+0]=4;
b[1*3+1]=5;
b[1*3+2]=6;

row=1;
cell=2;

a[row,cell] is equal to b[row*3+cell]

using that method, your array can be as many dimentions as you want

EDIT: took myself some time to answer a few questions aswell

Edited by FoxInABox, 19 December 2009 - 06:37 AM.

  • 0

#47 ramses12

ramses12

    6

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

Posted 10 February 2010 - 09:52 AM

the "i" is simply just a variable that is named "i", the letter "i" is more frequently used because ppl ofen made a list of instances, and used only the first letter to make it short, but it could as well had been "ins", "a" or some other variable name.

The "i" custom could have also started because the unit vector on x axis is usually notated as i.
  • 0

#48 smileyface1505

smileyface1505

    GMC Member

  • New Member
  • 178 posts

Posted 12 February 2010 - 01:40 AM

Pretty nice tutorial :)
  • 0

- $0.01 Donate So Far
Please Donate Some Money to Cover the Cost of Sprites and Such

#49 ZeropunktuatioN

ZeropunktuatioN

    GMC Member

  • New Member
  • 18 posts

Posted 18 February 2010 - 01:05 PM

Thnx, Nice tutorial.

I'm making a racing game and want to store the driver's qualifying times in an array from quickest to slowest.

Could someone please point me in the right direction?
  • 0

#50 twelveways

twelveways

    GMC Member

  • GMC Member
  • 1383 posts

Posted 18 February 2010 - 02:06 PM

If you have pro then I would recommend using a ds list as they have functions that allow you to re-order and rearrange the variables easily.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users