How do I Use The "For Each" System Function

0 favourites
  • 6 posts
From the Asset Store
Template for dungeon/maze generation, using wave function collapse
  • 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!

  • 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

  • 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'.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • YES, it worked! Thank you so very much!

  • 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.

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)