How do I create a nested loop with condition?

Get help using Construct 2

Post » Mon Jan 26, 2015 7:51 pm

Hi!

I really want to get this work now. It's a nested loop seeking an empty slot and putting the item in the slot if it's true. It's useful when a player takes some items.

The problem is that the first loop moves too fast and it results in that the first loop getting maxvalue and the second loop getting minvalue instead.
You do not have the required permissions to view the files attached to this post.
B
3
Posts: 9
Reputation: 193

Post » Mon Jan 26, 2015 8:25 pm

@spokplumpen : I am not getting, what you do there exactly. Maybe it would be helpful to see the whole inventory mechanics? (I have to admit, I work not too much for loops, I don't see the point in them too much.)
B
8
S
3
Posts: 197
Reputation: 1,207

Post » Mon Jan 26, 2015 10:43 pm

Hi, i don't think that I should send my whole capx. Maybe only part of the capx but I'm too tired and going to bed now. I will do it tomorrow instead.

And it works better without else now. I think that I have to create local variables first istead of the loopindex. The X-loopindex disappears after the Y-for starts. Wrong x-loopindex but right y-loopindex.

Thank you for your quick response!
You do not have the required permissions to view the files attached to this post.
B
3
Posts: 9
Reputation: 193

Post » Mon Jan 26, 2015 11:17 pm

Solved! The y-loop have to be first in the event. The X-loop second. It's weird!
B
3
Posts: 9
Reputation: 193

Post » Mon Jan 26, 2015 11:22 pm

It might work better (next time maybe) to put the second loop as a sub event. Traditionally, a nested loop goes inside the first loop. What you have here are both loops running at the same time with the way Construct works.

Essentially:

Event: For X 1 to 10 : action
Sub Event: For y 1 to 10: action (Set position to tile map x,y)

Sorry, I'm at work and can't access C2 right now. If you need a better example then let me know. I can try and make one tonight or Wednesday.
Kurieus
Come visit and play Blast Box!
B
15
S
3
Posts: 205
Reputation: 1,349

Post » Tue Jan 27, 2015 8:56 am

The two for loops at the same level of condition work perfectly in some places, mepis. For example, Zatyka's calculating-rectangular-coverage-area .capx uses X and Y for loops in the same condition.
B
11
S
4
Posts: 152
Reputation: 1,236

Post » Tue Jan 27, 2015 8:58 am

Message: Unnatural20 can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
located at www.scirra.com/ tutorials/ 1339/ calculating-rectangular-coverage-area
B
11
S
4
Posts: 152
Reputation: 1,236

Post » Wed Jan 28, 2015 3:21 am

I'm pretty sure you would just be getting lucky and that the loops are processing so fast with such small indexes that they are eventually matching up. With the way C2 works, putting two for loops in the same condition run both loops at the same time.

If you want to be exact and loop through, say every part of a two dimensional array, you would want to use a true nested loop.

@Ashley can shine some better light on this, I'm sure.
Kurieus
Come visit and play Blast Box!
B
15
S
3
Posts: 205
Reputation: 1,349

Post » Wed Jan 28, 2015 9:09 am

you could do it like this as well
You do not have the required permissions to view the files attached to this post.
B
249
S
39
G
15
Posts: 1,952
Reputation: 40,579

Post » Wed Jan 28, 2015 9:09 am

you could do it like this as well
You do not have the required permissions to view the files attached to this post.
B
249
S
39
G
15
Posts: 1,952
Reputation: 40,579

Next

Return to How do I....?

Who is online

Users browsing this forum: plinkie, zenox98 and 10 guests