How do I stop a for loop?

Get help using Construct 2

Post » Tue Apr 07, 2015 3:40 am

I'm having trouble understanding the for loop logic in Construct. I've looked at the manual, tutorials, other forum posts, other .capx and I'm still confused.

The way I understand it is you make a for loop give it a starting number and a ending number once the loop runs it increments the index and once the number reaches the end point the for loop should stop. But it never stops it just runs forever.

The only way I've been able to get it to stop is to run it as a sub event under a compare two values event where I compare the index is less than a set number. But if I have to do that I don't understand the purpose of the beginning and end numbers in the for loop. If I'm having to run an outside comparison to tell it to step.

Image

The ORIGINAL block of code is setup how I think it should work.

The FIXED block of code is the only way I've been able to get it to work.

Does it have to be nested in a trigger once event like on Start of Layout?
Image
B
33
S
16
G
2
Posts: 185
Reputation: 4,036

Post » Tue Apr 07, 2015 4:03 am

looks like you are using constants in your for loop... not variables.. at least one of them should be a variable.

for that kind of code you probably want to use Repeat.
B
67
S
20
G
8
Posts: 989
Reputation: 10,148

Post » Tue Apr 07, 2015 1:14 pm

Image

I've used variables before for the ending position with the same result. I mean a variable is just a container for a integer , string, float, or boolean. I don't understand how using a variable number would cause it to work differently.

I'm guessing they are meant to be used as a sub-event under a trigger once event.
Image
B
33
S
16
G
2
Posts: 185
Reputation: 4,036

Post » Tue Apr 07, 2015 1:26 pm

try using function to call that loop event, so it will trigger once per call.

- (your conditions) > Function > Call function > Loop()
- On function "Loop"
- For "blocks" from 1 to to 10 > create object
"If you want to move a mountain tomorrow, you should start by lifting stones today."
B
55
S
13
G
2
Posts: 282
Reputation: 6,241

Post » Tue Apr 07, 2015 1:52 pm

Quite simply, you're setting up a loop inside the main loop (which the event sheet is). Every tick, the loop is executed and 10 block objects are created. You need to restrain the execution one way or another if that is not what you want. The simplest method is adding a "start of layout" condition to the loop event.
B
54
S
21
G
20
Posts: 871
Reputation: 15,450

Post » Tue Apr 07, 2015 3:05 pm

@VictoryX yes as @Magistross says, you need some sort of condition so that is not "always happening", that's what I meant by using a variable...i.e. assuming the condition is not always true... otherwise if you use constants you are running that forloop 60 times per second indefinitely...
B
67
S
20
G
8
Posts: 989
Reputation: 10,148


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 2 guests