Arrays for Beginners

Favourite 239 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

[To follow along - download the animals_starter.capx starter file.]

[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


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:


[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:


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":


sqiddster 32.8k rep

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

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

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

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

thanks i liked the tutorial,but
i have a question what should i do here:
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
ramones 25.9k rep

@zsangerous I added some screenshots to the "setting values" section to make it clearer.

Wednesday, May 23, 2012 at 1:54:44 PM
zsangerous 12.9k 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
Yann 16.7k rep

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 is the same as,0) and,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
ramones 25.9k 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
AarongamerX 6,005 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
vidi 5,058 rep

very helpful for me ! Thank you! :)

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

This is a great tutorial. Thanks for writing it!

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

Thanks was helpful

Friday, July 13, 2012 at 9:31:59 PM
GenkiGenga 14.5k rep

Great tute Ramones.

Wednesday, August 22, 2012 at 1:31:56 PM
TELLES0808 23.1k 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
cesisco 10.3k rep

back to basics, very good tutorial.

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

Hey man! Thanks a lot 4 this tuto!

Its symple helping me so much!

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

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.