Arrays for Beginners

28
  • 255 favourites

Index

Attached Files

The following files have been attached to this tutorial:

.capx

animals-starter.capx

Download now 173.28 KB
.capx

animals-complete.capx

Download now 174.16 KB
.capx

highscores-example.capx

Download now 182.31 KB

Contributors

Stats

52,024 visits, 166,632 views

Tools

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

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

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

Result:

And the fifth cell’s value to "dog":

Now the array looks like this:

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:

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

  • 8 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Thank you very much.

    With new version we can change WebStorage plugin to LocalStorage plugin, And use LocalStorage On item "array_save_example" event for set animals when LocalStorage Get item "array_save_example" loaded

    1. btn loadArray on clicked > LocalStorage Get item "array_save_example"

    2. LocalStorage On item "array_save_example" get > animals Load from Json string LocalStorage.ItemValue

    • Took me a while to look down here at this update and figure it out but i got there in the end.

      Any one reading this and struggling try changing the bullet pointed bits first and the rest makes more sense and dont forget to add the JSON plugin.

      Thank You

    • Thanks for trying to provide an update to the Webstorage reference. i'm totally lost from your update however. i'll have to find a more up to date tutorial.

  • Hi, what's going on with second and other pages of this tutorial - it shows just an error...

  • Best writing on arrays

  • Thanks for very good and important topic with detailed explanation

  • Thank u, it helps a lot. 😁😁😁😬😬😬😬