# For Loop!?

Get help using Construct 2

### » Fri Oct 10, 2014 5:51 pm

Could someone explain what this for loop is doing when executed in construct?
sounds a silly question but its doing strange things.

Cant seem to get predictable behavior from it. All I want to do is

when something happens

from 1 to variablestorednumber
every 1 second
---------------------------- do something

if i repeat it once for an action what i get is the the loop seems to just jump to the end, no matter how i play around with it.

And if i dont do that it just keeps counting up.

Cant seem to find a working example of the for loop about.
Some games: https://play.google.com/store/search?q= ... s&hl=en_GB Feel free to show the panda some support, he would appreciate it :)
B
19
S
5
G
1
Posts: 155
Reputation: 1,619

### » Fri Oct 10, 2014 6:03 pm

Loops are only executed in one tick.
For what you are trying to do try some variables like:
+compare variable variablestorednumber is >0
>+every n seconds, do fo, subtract 1 from variablestorednumber
B
178
S
50
G
206
Posts: 8,692
Reputation: 127,727

### » Fri Oct 10, 2014 6:07 pm

Yeah have it working that way too, but was trying to add in some other checks and thought the for loop would allow me to get those things working easier. Clearly not
Some games: https://play.google.com/store/search?q= ... s&hl=en_GB Feel free to show the panda some support, he would appreciate it :)
B
19
S
5
G
1
Posts: 155
Reputation: 1,619

### » Fri Oct 10, 2014 6:14 pm

A For loop will go from a starting value to its end value with an increment of 1 each time it loops.
So:
For 0 to 5
(Will start at 0 and end at 5)
So 0,1,2,3,4,5

You can get the current index of a loop by using loopindex. This also means as C2 encounters a loop it will finish it before continuing. And guess that's why you think it jumps to the end, but in fact its running 5 times through the loop. So if you at the end of the loop have something that would be an end, then that is what it will be.

But when the loopindex is = 5 in the above example then the loop is done and C2 will continue with whatever.

In your example you would normally do like this:
Every 1 second
For 1 to Variablestorenumber
...do something

(Which means that every 1 sec you run a loop starting at 1 and end at "Variablestorenumber")

So its predictable in the sense that you might know the starting value and the end value, or at least based on some calculations know them and you know that it increment by 1 each time.
B
45
S
12
G
3
Posts: 1,210
Reputation: 7,559

### » Fri Oct 10, 2014 6:23 pm

Forgot I did try repeat once, but still not going to work with the FOR.

What i was trying to do is execute one loop, wait a few seconds then move onto the next loop.

Not have all the loops running at the same time but in sequence.
Some games: https://play.google.com/store/search?q= ... s&hl=en_GB Feel free to show the panda some support, he would appreciate it :)
B
19
S
5
G
1
Posts: 155
Reputation: 1,619

### » Fri Oct 10, 2014 7:01 pm

Ah resolved it thanks
Some games: https://play.google.com/store/search?q= ... s&hl=en_GB Feel free to show the panda some support, he would appreciate it :)
B
19
S
5
G
1
Posts: 155
Reputation: 1,619

### » Wed Oct 15, 2014 4:37 pm

@BinaryPanda - How do you resolve this?
B
10
S
1
Posts: 67
Reputation: 909