# How do i use arrays?

• I have a vague understanding of arrays. Could someone help me understand how they work. I feel i'm missing out on something big. Could you also give me a practical situation where arrays could be used. thx.

The only thing i know is it can hold numbers and strings. But how do i access them and or shuffle the numbers in an array?

• Think of the array as a huge grid, which holds values in each of its grid squares.

You can access the value in a specific grid square by using array.at(x,y,z)

Shuffling all the elements in the array is a bit complex, but I think there is an example of it somewhere...

• Oh i see. Can an object have a list of arrays? Like if i was going to make a card game i would create an array starting from 0 to 21 right? Could i assign array 11 to the card Jack. Or 20 for A?

Why would i want to hold values in an array?

• i took a class where we learned how to push and pop arrays but apparently my mind went blank. I never really learned how to use it correctly.

• ## Try Construct 3

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• How about a Tic Tac Toe game where you want to keep track of which square contains what symbol? One option is to have a variable for each square. You could have variables Top-Left, Top-Middle, Top-Right, Middle-Left, Middle-Middle, Middle-Right, Bottom-Left, Bottom-Middle, Bottom-Right. And then if the 'x' player clicks on the symbol called 'Top-Left' I could have an event that sets the variable Top-Left to 'x'. And then if the 'o' player clicks on the symbol called 'Top-Right', I could have an event to set the variable Top-Right to 'o'. Oh, and now I have to test to see if anyone got three in a row. So what are my possibilities?

If Top-Left, Top-Middle, Top-Right are all 'o',

If Top-Left, Top-Middle, Top-Right are all 'x',

etc.

What happens now if I want to make a simple change in logic, or even worse, make a 10x10 tic tac toe game. So many more possibilites, the events would be endless!

OR I can just use an array that is 10x10 and use math to calculate where the player places an 'o' or an 'x'. Let's say the player 'x' clicks position 3,5. And then all I have to say is set Array(3,5) to 'x'.

That's one possible use of it, out of INFINITELY many! The more game development you do, the more uses you'll see of structures such as arrays. Just wait... In fact, the whole JavaScript game engine that Construct runs on is guaranteed to be full of arrays and loops through those arrays, because it has to keep track of several objects and set common properties on all of them. We are lucky that Construct handles all that stuff for us, so that we only have to use arrays for more specific applications.

• Ashley , i know that manual will help but the problem its hard to know these stuffs for people that have no knowledge about codes...

here the topic i asked to explanation:

http://www.scirra.com/forum/arrays_topic50950.html

but for real when i hear in forums about it , i feel its easy and easy to understand it , but when i try it i see its very complex ...many times i tried to make the level page but i really got confuse.

also i have tried to see tutorial in making levels page or any array example but i couldn't see a good one.

• i agree with Zsagerous. Explaining arrays can be a bit challenging. Thx dalal for the tic tac toe analogy. I'm starting to get it.

• i agree im looking at the

and its still not clear

• A good way to learn how to use array might be to start with one dimensional array.

A one 1D-array is just like a simple indexed list.

Array are usefull because they provide a unique name (the name of the array) for more than one value.

Classical variables (instance, global, local, etc) don't, it's always one name <-> one value.

Using one name for many values helps to lessen the number of variable.

Imagine a list of enemy. With variable you'd have to do stuff like:

``````global variable enemy1 = "wolf"
global variable enemy2 = "bear"
global variable enemy3 = "cactus" // why not?
etc``````

There's no advantage and many drawback to this method

I'd use Array like this

``````+On Start of layout
-> enemy set size to (3,1,1)
-> enemy.At(0) = "wolf"
-> enemy.At(1) = "bear"
-> enemy.At(2) = "cactus"``````

(note that array are 0-based)

This way you can do stuff like

``````+On Start of layout
-> Text: set Text to "There are "&enemy.width&" types of enemy in this area:"&newline
+for each X element
-> Text append "  -"&enemy.CurValue&newline``````

Note that:

• using foreach X element is the same as doing:+for "" from 0 to enemy.width-1

-> Text append " -"&enemy.At(loopindex)&newline

- using enemy.CurValue is the same as using enemy.At(enemy.CurX)

Once you're used to work with 1D array, you can begin to use 2D arrays. For instance you could store the area index in X index and use the Y indexing for the list of enemies.

Like

``````+On Start of layout
-> enemy set size to (2,3,1)
-> enemy.At(0,0) = "wolf"
-> enemy.At(0,1) = "bear"
-> enemy.At(0,2) = "cactus"
-> enemy.At(1,0) = "pony"
-> enemy.At(1,1) = "jar jar binks"

And you could go really far with this idea if you want to even store the name of the area and some characteristics.

There you can do it by organising your array such as the 2 first Y indexes are for these data and the rest is for the list of enemies

``````+On Start of layout
-> enemy set size to (2,5,1)
-> enemy.At(0,0) = "The Shire" //name of the area
-> enemy.At(0,1) = "mostly cloudy" // type of weather
-> enemy.At(0,2) = "wolf"
-> enemy.At(0,3) = "bear"
-> enemy.At(0,4) = "cactus"
-> enemy.At(1,0) = "DreamLand" //name of the area
-> enemy.At(1,1) = "sunny"  // type of weather
-> enemy.At(1,2) = "pony"
-> enemy.At(1,3) = "jar jar binks"

Or you could build a 3D array to split things up a bit more

``````+On Start of layout
-> enemy set size to (2,2,3)
-> enemy.At(0,0,0) = "The Shire" //name of the area
-> enemy.At(0,0,1) = "mostly cloudy" // type of weather
-> enemy.At(0,1,0) = "wolf"
-> enemy.At(0,1,1) = "bear"
-> enemy.At(0,1,2) = "cactus"
-> enemy.At(1,0,0) = "DreamLand" //name of the area
-> enemy.At(1,0,1) = "sunny"  // type of weather
-> enemy.At(1,1,0) = "pony"
-> enemy.At(1,1,1) = "jar jar binks"

(overkill isn't it?)

Anyway in the end the most important thing to consider when using array is how you'll organize it.

And also, I never set so much data by end. I always end up putting all that in a string and parsing it (via tokenat and tokencount)

But that's another story (:

• Guys, be sure to check the items listed for "Arrays" in the how do I FAQ.

There are already numerous topics on the subject with example capx in this, that or again this topics.

Active reading and exercising should help get the hang of arrays.

• Yann ok, this is the easiest bar of the array (the learning), ok the questions how can i set them on construct2 here is the problem .

sometimes i have global variable = 0 and sprite frame number,and my array is : at(global variable,sprite frame number)=0 ---> action = sprite destroy

**this can unlimited numbers of arrays that has value = 0...

this is diffrant between

array value = 0 ---> action sprite destroy

but the problem in 2nd one how can i store the things i want on it , for example i can make array with at(0,global variable) = 0 or

at(0,sprite fram number)= 0 , many stuffs like this...

but i found , the easiest way to store array values by doing like :

on start of layout ---> array (0,1) = 1

array (0,2) = 2

array (0,3) = 3

array (1,1) = 4

then in next event we do like this :

if array current value = 1 , spawn enemy1

if array current value = 2 , spawn boss1

if array current value = 2 , spawn enemy2

if array current value = 2 , spawn boss2

**the problem i couldn't get it on this way is how can i combine the array with the global variables i don't know the expression...

in all that stuffs there are more complex stuffs , and im having fun learning them.

the good point on arrays that if you want your game to be good you must have at least one array ... mostly one array is enough you can make 999999999999999 stuffs with it but some use more for organizing only.

• i see. So arrays are a way to organize your work. Cool. and I don't think jar jar binks would be considered an enemy hehehe, great tutorial yann.

• http://www.scirra.com/tutorials/307/arrays-for-beginners

I spent the best part of this evening on this tutorial. Let me know if there are any typos, errors, omissions etc.

• The absolute best way to learn what arrays do and how to use em is to open up some kinda spreadsheet and start setting values. Each cell on the spreadsheet relates exactly to a location in the array.

A 1d array = a spreadsheet with just 1 column but however many rows you want

a 2d array = a spreadsheet with 2 columns and however many rows.

a 3d array = a spreadsheet with 3 columns and however many rows.

It really is that simple. Try to work with an array in c2 and at the same time have a spreadsheet open and use the spreadsheet to input your values you want in your array then you can use the exact locations of the cells to know where that value should be in your array

• 44 posts