How do I Use The "For Each" System Function

Get help using Construct 2

Post » Wed Apr 06, 2016 4:38 pm

As the title tells I'm having problems using the "For Each" System Function.
I'm making a turret (turretBase) and the barrel to the turret (turretGun). I tried the "For Each" function:

For each "turretBase" --> system create object "turretGun" at layer enemy1 and at ImagePoint turret.ImagePointX("Gun")... and the same for X.

But however this doesn't seem to work, I even tried throwing in a "Trigger Once"!

If you could help it would be much appreicated and thanks in before-hand! :D
B
9
S
1
Posts: 26
Reputation: 731

Post » Wed Apr 06, 2016 4:49 pm

This is what the manual says about the For Each function:

For Each
For Each (ordered)
Repeat the event once per picked instance. This only repeats for instances that have been picked by prior conditions. See how events work for more information on picking. For Each is commonly mis-used or used redundantly - actions already apply for each instance picked by conditions, so it often is simply not needed. However, if you fully understand how the event system works, it can be used to force the event to apply once per instance where the event system would not normally do that. The 'ordered' variant allows the order that the instances are iterated in to be defined by an expression. For example, ordering by Sprite.Y ascending will iterate the top instances on the screen first, moving downwards.

https://www.scirra.com/manual/124/system-conditions
Download Pistons:

Image
B
23
S
5
G
8
Posts: 49
Reputation: 4,651

Post » Wed Apr 06, 2016 5:42 pm

@famousgnu6 All true. Except.

Needed, when you use it as a loop without prior picked conditions to run trough instances.

And that is what is happening here (i suppose, i see no CapX)

For each "turretBase" --> system create object "turretGun" at layer enemy1 and at ImagePoint turret.ImagePointX("Gun")... and the same for X.

@LudvigMarten

So, what you coded is a loop. (if u used system > create/no containers) It repeats turretbase.count times. Each time it creates an object 'turrentgun' on the position turret.ImagePointX, turret.ImagePointX.

There are 3 objects in there. One and only one 'turretbase' in the picklist. The object that gets created by a system > create does not need to be in the picklist. Obvous, it is new. So 'turrentgun' is fine. The object 'turret' on the other hand is not picked. The system does not know that you need the 'turrent' that goes with the 'turretbase'. In fact, it works, (it makes turretbase.count times an 'turrengun' on the same place), but not as you intended. Ofcours, that is differend if you mean turretBase.ImagePointX/Y.

Easy way is just:

Condition > for each > turret / action > spawn object > turrentgun at imagepoint (name or number), layer as you wish. The imagepoint is now the imagepoint (by name or nr) from that one picked 'turret'.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Wed Apr 06, 2016 5:50 pm

YES, it worked! Thank you so very much! :D
B
9
S
1
Posts: 26
Reputation: 731

Post » Wed Apr 06, 2016 5:54 pm

Do you want the guns created at the same time as the base? If so, right after you create the base, create the gun. The only time you would use For Each in creating the guns, is if the base are created, and then at some point later, you want the guns created.
B
14
S
5
G
1
Posts: 64
Reputation: 1,452

Post » Wed Apr 06, 2016 5:58 pm

Thanks! For anyone else doing this, i found an easy fix for my certain situation:

turretBase --> on created; wait 0.01 seconds --> turretBase spawn another object turretGun
B
9
S
1
Posts: 26
Reputation: 731


Return to How do I....?

Who is online

Users browsing this forum: psicodelicow3 and 3 guests