Basic Loops and Arrays

Favourite 115 favourites
Tutorial written by cow_trixOriginally published on 29th, June 2011 - 11 revisions

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'.

Inserting a Loop Event


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:

A While loop

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.

For Each Events


For Each output
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.

Example of a 2D array
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.

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

Size Expressions

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.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Comments

3
Tom 54.1k rep

Awesome tutorial!

Wednesday, June 29, 2011 at 9:27:31 AM
7
Ashley 193.8k rep

Nice, but I think a different For Each examples would fare better - the first one functions the same as 'Every tick', and the second one you could just use 'Box.Count'. Remember the event system is internally doing for-eaches for running conditions and actions... usually it's for things like 'Compare values', which makes no effort to pick any object.

Wednesday, June 29, 2011 at 2:14:35 PM
3
arandaschimpf 3,204 rep

im such an idiot.
I really dont saw the loop section and i had 3 mini-games made usings loops comparing variables and adding 1 to them.

Friday, July 01, 2011 at 3:16:10 AM
2
ludodesign 30.3k rep

I trying learn the new things. Learning...
Good tutorial.

Saturday, July 02, 2011 at 4:24:31 AM
1
cow_trix 9,364 rep

Comment by Ashley

Nice, but I think a different For Each examples would fare better - the first one functions the same as 'Every tick', and the second one you could just use 'Box.Count'. Remember the event system is internally doing for-eaches for running conditions and actions... usually it's for things like 'Compare values', which makes no effort to pick any object.
Ah, cheers Ash. I'd forgotten about the Count expression. The thing

Monday, July 04, 2011 at 1:58:36 AM
5
Dicon 2,846 rep

A couple of complete examples would help us beginners. Although coming from Blitz basic3D I know about the theory, it's the syntax in Construct that's a bit wobbly. It's good though.
Thanks.
Dicon

Wednesday, July 27, 2011 at 12:15:24 PM
2
the_chosen_byte 3,929 rep

maybe i don't know anything...but isn't the third geometric axis, for depth, called the Z-axis? It's either a typo or I am a jackass...

(2nd paragraph under "Arrays in Theory")

Friday, July 29, 2011 at 3:44:54 AM
1
vidi 5,058 rep

For 0 to 9
Print "Hello!"

Sounds theoretic easy, but I do not know how to applies it practically in C2.

Thursday, September 08, 2011 at 10:59:51 PM
2
vidi 5,058 rep

I understand it now. Thank your for your tutorial very helpfull for me

Tuesday, September 20, 2011 at 5:56:11 PM
1
carminielloz 8,643 rep

hey guys sorry , not sure this is the right place to post this question but was wondering what to do to insert a health bar , so far I can only add a global variable then get the system to subtract from that number .... i think a health bar looks a lot better . can anybody suggest where to find the answer , thanx a lot >

Sunday, October 09, 2011 at 12:11:22 PM
1
EgoAnt 6,408 rep

Thanks! This is a great introduction to how arrays work in C2!

Thursday, November 24, 2011 at 3:59:35 PM
1
Lordxorn2 3,532 rep

Comment by carminielloz

hey guys sorry , not sure this is the right place to post this question but was wondering what to do to insert a health bar , so far I can only add a global variable then get the system to subtract from that number .... i think a health bar looks a lot better . can anybody suggest where to find the answer , thanx a lot >
Hey Carminielloz,

Your question does not really apply to Arrays and Loops, and the forums "How do I?" is always the best place to ask.

May I also suggest you do a full search of the tutorials, and forums to see if someone else always answered your question. As it is generally frowned upon to ask a question that has already been answered in depth.

For your convenience I am linking a great tutorial on health bars.http://www.scirra.com/tutorials/182/making-a-fancy-lifebar

Monday, December 26, 2011 at 4:58:47 PM
3
Lordxorn2 3,532 rep

Comment by vidi

For 0 to 9 Print "Hello!"

Sounds theoretic easy, but I do not know how to applies it practically in C2.
This is a great tutorial as well because I now have a nice picture of how arrays are used, however like you mentioned I am also wondering what makes them powerful and who to apply them in a practical way.

I found a good tutorial on the GM website, and if you just ignore the GM parts, the rest of tutorial gives a good example of why Arrays are powerful.
http://gmc.yoyogames.com/index.php?showtopic=105
http://gmc.yoyogames.com/index.php?showtopic=524500

Correct me if I am wrong, from but for example having a player inventory is a good example. Instead of having to track separate variables, you can store them in arrays in one nice place.

Monday, December 26, 2011 at 5:12:26 PM
1
Cross_ 2,647 rep

So that for loop example is supposed to be an infinite loop? It would be a more useful example if you had some way of exiting the loop.

Saturday, January 14, 2012 at 11:45:45 PM
1
scunliffe 2,963 rep

When I tried to create a for loop (because it runs every tick) it actually loops over and over forever.

I was hoping to set up a loop this way, but can't seem to add loops at the "action" level.

In pseudo code I want this:

if(someCondition){
for(0 .. 10){
spawnObject();
}
}

However if I try to create the "if" condition as a System event that compares a variable... then I can't add the for loop as an action when that condition is met. :-(

Am I missing an easy way to do this?

Wednesday, February 01, 2012 at 11:38:03 AM

Leave a comment

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