In most cases, you don’t need For Each because the engine automatically loops through instances. For example,
Enemy: Set HP to random(50,100)
This will assign random HP values to each enemy instance, not the same value to all.
However, you do need For Each if you specifically need to process each instance separately. For example:
For Each Enemy → Call Function DealDamage(Enemy.UID)
Without For Each, the function would only be called once for the first enemy instance.
Another interesting fact I only recently learned: when multiple objects with multiple instances are referenced in the same event, the engine tries to process them in pairs.
For instance:
Enemy: Set size to Box.size
This works fine if there’s only one Enemy or one Box. But if there are multiple instances of both, the engine will match them like this:
- Enemy(0) → Box(0)
- Enemy(1) → Box(1)
- Enemy(2) → Box(2)
Once it runs out of Box instances, it loops back to the beginning, meaning Enemy(15) might get Box(0)’s size.
To avoid this, you’d need a For Each loop or another way to pick the correct instances.
.
Here’s a more obscure example of the same issue. Since the "On Timer" event picks both Tree instances, the engine compares Leaf.treeID with Tree.ID in pairs. As a result, some of the leaves are not picked.
A "For Each Tree" loop is needed here: