[Solved] For Each and Trigger Once doesn't behave as expected?

I’m trying to apply different animations for each instances. Basically, my event looks like this:

For Each instance of "enemy"
Condition:

  1. Variable 'enemy_id' of "enemy" > 0
  2. Trigger Once

Action:

  1. Change the number of animation of "enemy" set to: RandomInRange(1,3)

I expect for each instances to get a random animation once. However, the Trigger Once seems to only apply to the first instance (despite each of my “enemy” instances having different ‘enemy_id’ variable).

Is there any alternative(s) to do this correctly or I’m messing up something here? Even tried switching to sub-event, reordering conditions/actions and still don’t get the result I hope for.

Any help would be greatly appreciated.

Try only this line and remove the trigger once

  1. Variable 'enemy_id' of "enemy" != 0

Just done that. The enemies were rapidly changing animations in every tick which is not what I hoped for unfortunately.

Yeah, there are 2 schools of though how that one should work. Here’s an earlier thread on the issue, and a work-around at the end of the first post.

1 Like

As MrMen mebtioned, trigger once in a for each behaves as a computer would expect, only once (meaning the first object instance that meets the conditions only), even though that doesn’t match up with what you or I would first expect.

The linked thread does provide more details and alternatives.

1 Like

Thank you! It now works after following your logic now. Here’s what I changed:

I replaced the Trigger Once condition with: The number of animation of "enemies" = 0

I have to add an empty animation #0 in the “enemy” object’s animation. So that each enemy instance with animation #0 will change randomly in range 1 to 3. That’s my solution for now.