Tutorial: When to use 'for each'

Post your own tutorials, guides and demos.

Post » Thu Apr 08, 2010 9:57 pm

There seems to be some confusion about when to use for each, so I hope to clear that up here.

What for each does

For each will run a loop containing all of the conditions and actions after it for each instance of the object specified, picking only one of those instances each time.

for each sprite
- rotate 5 degrees

Let's assume there are 10 instances of a sprite for this tutorial. What happens here is Construct picks one of the sprites, forgets the others, then runs the actions. Since there are 10 sprites, the actions are performed 10 times, but since construct picks only one of the sprites each time, all of the sprites are only rotated 5 degrees.

The event above is an example of when NOT to use for each, because you would get the same effect as if you deleted the for each sprite condition, and it would be faster and more efficient as well because construct would rotate them all at once instead of one by one individually. Here's an example of when you would want to use for each.

for each sprite
- Create object "selectioncircle" at sprite.X, sprite.Y

In this example, construct will run the actions once for each sprite. This way construct creates a selection circle for each sprite, and since only one sprite is on the selected object list when it runs the loop, it also knows where to place the selection circle when using sprite.X and sprite.Y. The result is 10 selection circles created, each at a sprite's location.

If you did not use for each sprite in the example above, it would pick all of the sprites, create one selection circle and place it at one sprite.

Construct not only runs the events for each sprite selected in the for each loop, but it also checks any conditions after the for each condition for each object. Here I show the number of times Construct runs each condition and action.

For each sprite - 1
if sprite is visible - 10
- actions - 10

In this example, construct checks if each sprite is visible one by one. This is less efficient than checking them all at once which construct does if you place the "if sprite is visible" condition before the for each condition like so.

if sprite is visible - 1
For each sprite - 1
- actions - 10

Another benefit of putting the if sprite is visible condition before the for each condition is it reduces the number of loops run by the for each condition to the minimum number required. If there are 5 visible sprites, then the event above would run this many times:

if sprite is visible - 1
For each sprite - 1
- actions - 5

For each is useful if you need to run the actions once each time for each object, but should not be used when you can do all of the actions at once.

Another example when not to use it:

for each sprite
- set private variable to 1

If you're setting a private variable, you generally don't need to use for each, even if the result will be different for each sprite. For example, setting the sprite's private variable to sprite.x will result in each sprite having its private variable set to its own x position.

However in this example:

for each sprite
if selectioncircle value 'spriteuid' is equal to sprite.UID
- set selection circle position to sprite.X, sprite.Y

Construct will pick one sprite each loop, check all selection circles for if their private variable is set to the sprite's UID, then set the position of that selection circle. This would not work without for each.

Also note that any subevents of an event with a for each condition will be run for each instance of the for each loop.

In summary: for each will run all conditions, actions and subevents after it for the number of times equal to the number of instances picked, picking one of those instances each time.

Edit: fixed grammar.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Fri Apr 09, 2010 6:15 am

Not used the for each loop yet, so this question is just speculation ..

If Sprite is Visible
For Each Sprite - > Action

Wouldn't this code see that a sprite is visible, then run the for each on all the sprites if atleast one was?
B
2
S
2
G
3
Posts: 114
Reputation: 1,553

Post » Fri Apr 09, 2010 7:00 am

[quote="MisfitBYTE":2d81acil]Not used the for each loop yet, so this question is just speculation ..

If Sprite is Visible
For Each Sprite - > Action

Wouldn't this code see that a sprite is visible, then run the for each on all the sprites if atleast one was?[/quote:2d81acil]

No. It would run the loop only for the sprites that are visible.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

Post » Sat Apr 10, 2010 12:32 am

Just to clarify, for my own sake (I still struggle with picking)...
[list:1cv9a7p5]
[*:1cv9a7p5]The condition "if sprite is visible" will run once and add all visible sprites to the SOL[/*:m:1cv9a7p5]
[*:1cv9a7p5]The condition "for each sprite" will loop through all of the sprites in the SOL[/*:m:1cv9a7p5]
[*:1cv9a7p5]Actions under this event will be executed on each sprite in the SOL as the loop is iterated[/*:m:1cv9a7p5][/list:u:1cv9a7p5]
Is this correct?

So, "for each sprite, if sprite is visible" will produce the exact same result as "if sprite is visible, for each sprite", however the later is more efficient because the SOL has been cut down before the loop.

I think it's another example of the need to unlearn some of the conventions of traditional programming when working with eventing. Normally, I would do it like so (psuedocode):
[code:1cv9a7p5]
for (sprite in sprites) {
if(sprite.visible == true) {
do stuff
}
}
[/code:1cv9a7p5]
But with eventing you have to think a little differently.
B
2
S
2
G
3
Posts: 105
Reputation: 1,510

Post » Sat Apr 10, 2010 1:08 am

That is correct.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Sat Apr 10, 2010 2:07 am

Very useful info about the "Is sprites visible" part. I didn't know that. Thanks. :)
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Mon Apr 26, 2010 2:49 am

Does the "Is visible" work for multiple instances of a sprite. So say you have 100 instances of one sprite on the layout, but only 5 that are visible would that only put the 5 instances in the SOL?
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Mon Apr 26, 2010 4:35 am

[quote="scidave":2y9thciz]Does the "Is visible" work for multiple instances of a sprite. So say you have 100 instances of one sprite on the layout, but only 5 that are visible would that only put the 5 instances in the SOL?[/quote:2y9thciz]

Yeap. You can even invert "Is visible" to put the other 95 into the sol.
Image Image
B
161
S
48
G
90
Posts: 7,347
Reputation: 66,749

Post » Fri Apr 30, 2010 11:51 pm

Wow. I also didn't know or thought much about that 'is visible' part. Yea, eventing needs a different mindset, I was doing picking by going for each before testing it.
B
16
S
10
G
5
Posts: 255
Reputation: 3,934


Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 0 guests