0 Favourites

# How do I Create "for loop" WITHOUT index reduce?

• 7 posts
• I found an odd behavior (in my opinion of course) about for loop.

According to the manual (link: https://www.scirra.com/tutorials/40/basic-loops-and-arrays), if the start index is greater than the end index, the loop will not execute. However, the reality isn't like this.

If in construct 2, I put "for loop" like this:

• start = 0; end = 0; loop run once (0)
• start = 0; end = 1; loop run twice (0, 1)
• start = 0; end = -1; loop run twice (0, -1)

This isn't what I expected. What I expect from "for loop" is supposed to be what I expect to happen in C++:

``````for(i = 0; i < array_width; i++)[/code:1jci93sg]
"i < array_width" and "i++" should be the deciding factor. So if end = -1, the loop shouldn't start.

So is there a way to create a "for loop" without negative index? Or if this is by design, what is the reason?

I could use repeat instead of for loop, but I would prefer for loop because I can get loopindex("name") by using for loop, instead of having to add several variable just to store loopindex (I'm using nested loop, so I need to get loopindex of the parent loop).``````

Develop games in your browser. Powerful, performant & highly capable.

Construct 3 users don't see these ads
• I wasn't aware of this and am also interested in hearing the answers to your questions, but if you need a simple work-around you can always multiple by -1 or take totalindex-loopindex to count backwards

• The problem isn't about the negative index itself, but the behavior of the loop itself. By using end = -1, what I expect is that the loop shouldn't run at all.

This is what I was doing:

``````for "spawn" from 0 to array.width-1 {
create enemy...
}
[/code:hpp5z9qp]
So if the array size (width) is 2, I will get loop: from 0 to (2-1) ==> result: new enemy.IID 0 & 1.
If the array size is 1, I will get loop: from 0 to (1-1) ==> result: new enemy.IID 0.
If the array size is 0, I will get loop: from 0 to (0-1) ==> result: new enemy.IID 0 & -1???

Using multiple by -1 will make: from 0 to 1 ==> result: new enemy.IID 0 & 1, which is still wrong as I got empty array.

In short, the for-loop is more like do-while.``````
• The problem isn't about the negative index itself, but the behavior of the loop itself. By using end = -1, what I expect is that the loop shouldn't run at all.

This is what I was doing:

``````> for "spawn" from 0 to array.width-1 {
create enemy...
}
So if the array size (width) is 2, I will get loop: from 0 to (2-1) ==> result: new enemy.IID 0 & 1.
If the array size is 1, I will get loop: from 0 to (1-1) ==> result: new enemy.IID 0.
If the array size is 0, I will get loop: from 0 to (0-1) ==> result: new enemy.IID 0 & -1???

Using multiple by -1 will make: from 0 to 1 ==> result: new enemy.IID 0 & 1, which is still wrong as I got empty array.

In short, the for-loop is more like do-while.
``````

So you are saying that the problem is that it will execute with a negative value?

That sounds like what it should do because if the ending index is lower than the starting it should change to for(i = 0; i > array_width; i--)

If your only issue is the loop executing when empty why don't you just but a pre-condition as array.width > 0 ?

• So you are saying that the problem is that it will execute with a negative value?

That sounds like what it should do because if the ending index is lower than the starting it should change to for(i = 0; i > array_width; i--)

If your only issue is the loop executing when empty why don't you just but a pre-condition as array.width > 0 ?

Yes, I know by logic on what you put in the parameters, it makes sense. That's why I didn't say it's a bug or anything, just IMO a bit odd behavior as you can't have the loop run 0 times.

What I use now is "repeat for array.width" but I have to have a variable to keep the loopindex in a nested loop. I was hoping by using loopindex("name"), I could make it simpler. Using pre-conditioning sure can do the trick, but that add some complexity, so it will just the same as using repeat.

• The tutorial is old. A change was made to support going from higher to lower indicies. If you want to control the loop, you have to add some extra logic, as badmoodtaylor suggests. Since there are only two inputs, rather than three, this is a limitation you have to deal with.

• I agree it is a bit odd you can't have the for loop not execute but as blackhornet puts it is just a limitation to deal with. Construct being a 'no coding required' engine has the goal of making things simpler, but when anything is made simpler you lose flexibility.

You can do it the way you mentioned, you can do it the way I mentioned, you could do x = array.width; while x > 0 spawn & x-- Or while array.width > 0 spawn & remove item from array. All the loops do the same thing just with different formatting to make it simpler in different situations.

• 7 posts