x
CONSTRUCT 2 SALE NOW ON For a limited time only - 30% off all Construct 2 licenses!

 File Size animals_starter.capx 173KB animals_complete.capx 174KB highscores_example.capx 182KB

## Translations

Know another language? Translate this tutorial!

## Stats

1,459 visitors
152K page views
438 translation visitors
17.4K translation page views

# Arrays for Beginners

Favourite 187 favourites
Tutorial written by ramonesOriginally published on 23rd, May 2012 - 10 revisions

(This tutorial assumes you have some knowledge of how Construct’s Conditions and Actions work. If not, see the beginner tutorial.)

## What is an array?

Basically, you can think of an array as an ordered list of cells; each cell can contain a value (a number or some text). An array can have 1, 2 or 3 dimensions, known as the x, y and z dimensions, also known as the width, height and depth.

### 1-dimensional arrays

[1] Let’s create a list of five animals. Right-click or double-click on the layout and insert a new Array object. Call it "animals".

Now select the array in the objects window and let’s look at it’s properties. The default array size is Width: 10, Height: 1, Depth: 1. We only want to store 5 animals in the list so go ahead and change the Width to 5.
By default all elements are set to zero (0); so we can imagine the array looks like this:
Open full size image

(By default, all values are set to 0.)

The position of a cell within the array is called it’s index. The indices start at 0, so the first cell’s index is 0 and the last cell is width - 1, or 4 in this example.

### Setting values

[2] Let’s set some values in the array. Add a "System: On start of layout" condition and the following action to set the value of the first cell to "cat":

Open full size image

Result:

And the fifth cell’s value to "dog":
Now the array looks like this:
Open full size image

The debugger is very handy for viewing the data in your array. If you run "Debug Layout" and select the array in the debugger, it will show of all the array's data:
Open full size image

Let's go ahead and fill the rest of the array:

### Getting values

To retrieve a value from the array we can use the expression:

```    Array.At(index)
```

[3] Let's add a button and a text object to the layout. When you click the button, the text should be set to the first animal in the array.

To do this, we set it to animals.At(0):

If we know a value and want to find it’s position in the array we can use the expression:

`    Array.IndexOf(value)`

animals.indexOf("dog") equals 4. If we wanted to get the animal that comes before dog we could use:

`    animals.At(animals.IndexOf("dog") - 1)`

which will return "rabbit".

### Looping through the array

-

Arrays have a couple of conditions, "For each element" and "Compare current value", that let us loop through the array cell by cell and inspect each value. When looping through the array we can use the expressions:

```    Array.CurValue        - to get the current value
Array.CurX            - to get the current X index
Array.CurY            - to get the current Y index
Array.CurZ            - to get the current Z index

```

[4] Let's loop through the animals array and display every value in it by appending animals.CurValue to a text object:
[4.1] Or we can loop through the array and anywhere we find the word "cat" we'll replace it with the word "lion":

17
sqiddster 32.2k rep

Great! I'm sure it will help beginners get the hang of arrays.

Wednesday, May 23, 2012 at 12:54:06 AM
8
Joannesalfa 17.6k rep

Nice one! im going to learn how to use arrays :)

Wednesday, May 23, 2012 at 9:49:13 AM
4
zsangerous 11.7k rep

thanks i liked the tutorial,but
i have a question what should i do here:
http://i45.tinypic.com/dbh93.jpg
also i have a question now how can i combine the array with global variable so when i add to global the array change for example :
array (global, 1) = 1
so when any array like ( (1,2,3,4,5,6...), 1 ) = 1

Wednesday, May 23, 2012 at 11:12:05 AM
6
ramones 23.8k rep

@zsangerous I added some screenshots to the "setting values" section to make it clearer.
http://dl.dropbox.com/u/8367729/construct/pics/array_global.png

Wednesday, May 23, 2012 at 1:54:44 PM
2
zsangerous 11.7k rep

@ramones hehehe now im fine thanks for the good job i wish you add the picture and these stuffs like you made global when you press space is change the array :) thanks for your help wish you do more ^^

Wednesday, May 23, 2012 at 4:03:14 PM
4
Yann 16.1k rep

@ramones
With a foreach XY, I think Array.CurZ will always be equal to 0 even if you have a 3D array.
Also, on your example, Array.CurY will also be equal to 0 since the height of your Array is 1
And Set value at(animals.CurX,animals.CurY) to "mouse" will work since (I think) under the hood 1D array are just 3D array with a height and depth of 1.
So Array.at(5) is the same as Array.at(5,0) and Array.at(5,0,0)

But it would have been more consistant to write your event like that:
animals: for each X element
animals: Current value = "cat"
->animals: Set value at (animals.CurX) to "mouse"

Wednesday, May 23, 2012 at 5:50:44 PM
2
ramones 23.8k rep

@Yann Oops, that's the way I had it but I accidently used the screenshot from the 2-d section. Fixed now.

When I wrote z = Array.CurZ, I meant in general. It wasn't supposed to be related to the previous example but I suppose it doesn't make sense to mention CurY and CurZ in the section on 1-d arrays.

Wednesday, May 23, 2012 at 6:33:32 PM
2
AarongamerX 5,755 rep

Great Tutorial. Gonna read the rest when i get home. Looks like a lot of fun.

Friday, May 25, 2012 at 12:29:36 AM
3
vidi 4,998 rep

very helpful for me ! Thank you! :)

Saturday, May 26, 2012 at 8:18:21 PM
5
Ashley 166.6k rep

This is a great tutorial. Thanks for writing it!

Friday, June 01, 2012 at 6:14:03 PM
3
RikuChunsa 3,441 rep

Friday, July 13, 2012 at 9:31:59 PM
2
GenkiGenga 7,630 rep

Great tute Ramones.

Wednesday, August 22, 2012 at 1:31:56 PM
1
TELLES0808 17.3k rep

Thank you @Ramones, I can't believe you wrote this tutorial, because it helped a lot =]

Tuesday, September 25, 2012 at 2:56:44 AM
2
cesisco 10.1k rep

back to basics, very good tutorial.

Saturday, October 06, 2012 at 10:56:48 PM
1
jardelbr 4,817 rep

Hey man! Thanks a lot 4 this tuto!

Its symple helping me so much!

Thursday, October 11, 2012 at 5:25:30 PM