Basic Loops and Arrays

6

Stats

32,241 visits, 51,640 views

Tools

Translations

This tutorial hasn't been translated.

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.

Published on 29 Jun, 2011. Last updated 19 Feb, 2019

This tutorial is under construction! Please tell me if I've gotten anything wrong, or missed something you think is important.

In this tutorial we'll look at the basics of arrays and loops in Construct 2. These two elements are quite powerful and you can make some very cool things with them, especially when combined.

Loops In Theory

A loop is an event that is executed a number of times, or 'iterations', every tick. There are two types of loops that you need to know about: the While loop and the For loop.

A while loop executes an instruction while a condition is true, e.g.

    While (You are breathing)
           You are alive

As you can see, a while loop is useful when you don't know how many iterations you're going to have - after all, you don't know how long you're going to be breathing, but while you are, you'll be alive.

A for loop can be considered a subcategory of a while loop. It is used when you know how many iterations you want, e.g.

    For 0 to 9
          Print "Hello!"

This will print "Hello!" 10 times. It's important to remember Construct 2 (and a lot of programming languages) are zero-based as a rule (however, this can get confusing when we get to Arrays, but don't worry about that just yet).

Loops in Construct 2

Most loops in Construct are system conditions. You can insert one by creating a new event, selecting 'System' and then a condition under the heading 'Loops'.

For

After creating a For loop, you'll be prompted for 3 fields:

Name - the name of the loop. Try to give it something descriptive, so it gives you an idea of what it's counting.

Start Index - This is the number the loop starts counting at.

End Index - This is the number the loop finishes at.

Keep in mind that both indexes are inclusive. Also, if the start index is greater than the end index, the loop will not execute.

A while loop can be made by putting a variable in either of these index fields like so:

For Each

This type of loop will execute once for each instance of an object. It is important to note that events included in this loop relating to the object specified will pick to that object. E.g. the following events will create a text object for each 'Button' sprite, move it to the button's location, and set the text to the Button's "ButtonText" variable.

It's important to note that any 'For Each' event will pick to the object it is currently counting.

Repeat

The repeat event just repeats a series of events a set number of times. Note that this number can be a variable (and therefore adjusted at runtime).

Arrays In Theory

If you've ever done Euclidean geometry, you've already encountered a form of arrays in the form of the X and Y axis. An array is a set of values with distinct coordinates.

The X axis is usually referred to as 'width', the Y as 'height' and the Z as 'depth'.

Each coordinate in the array contains a value. For instance, in the array above, (1, 0) has the value "World".

Arrays in Construct 2

Arrays are an object, which means they are created like any other object by double clicking the canvas, or right clicking and selecting "Insert new object".

In Construct 2 (currently) arrays are 3 dimensional, although often they can be used as 2 dimensional or 1 dimensional. This means they have a X (width), Y (height) and Z (depth) axis.

Size

The size of an array determines how many values it can hold in each axis. Remember when I said Construct 2 was zero-based? This is one slightly confusing thing about arrays. Size is not zero-based, but coordinates are.

Let me give you an example. An array with a width, height and depth of 1 will only have 1 location where a value can be stored. This value will be at x = 0, y = 0 and z = 0.

Depth, height and width can be changed by clicking on the array object and altering their values in the Properties panel.

You can call the size of an array with an expression:

For Each Value

This condition is a combination of what we've learned. This loop cycles through every value in the array and will perform the actions specified for each one. There's a few expressions necessary for this to work:

Current X - This will return the X coordinate of the value the loop is currently at.

Current Y - See above

Current Z - See above

CurrentValue - this will return the value at (CurrentX, CurrentY, CurrentZ)

Using these expressions in conditions and actions can let you do some pretty cool stuff.

  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • "Keep in mind that both indexes are inclusive. Also, if the start index is greater than the end index, the loop will not execute."

    It will ;)

    Thanks for tuto