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

Get help using Construct 2

Post » Fri Oct 14, 2016 9:40 am

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++:
Code: Select all
for(i = 0; i < array_width; i++)

"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).
B
33
S
10
G
2
Posts: 46
Reputation: 3,063

Post » Fri Oct 14, 2016 11:30 am

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
ImageImageImage
B
44
S
26
G
11
Posts: 518
Reputation: 10,502

Post » Fri Oct 14, 2016 12:04 pm

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:

Code: Select all
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.
B
33
S
10
G
2
Posts: 46
Reputation: 3,063

Post » Fri Oct 14, 2016 12:19 pm

tmntppn wrote: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:

Code: Select all
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 ?
ImageImageImage
B
44
S
26
G
11
Posts: 518
Reputation: 10,502

Post » Fri Oct 14, 2016 1:08 pm

badmoodtaylor wrote: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.
B
33
S
10
G
2
Posts: 46
Reputation: 3,063

Post » Fri Oct 14, 2016 1:09 pm

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.
ImageImageImage
B
71
S
22
G
249
Posts: 3,764
Reputation: 138,579

Post » Fri Oct 14, 2016 2:40 pm

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.
ImageImageImage
B
44
S
26
G
11
Posts: 518
Reputation: 10,502


Return to How do I....?

Who is online

Users browsing this forum: jezjones24, NN81, R0J0hound, Yahoo [Bot] and 9 guests