Doubt about when and why we use "Repeat for each instance" on certain occasions

Hello!

The Gdevelop Wiki makes it clear that the use of the “For each object” event should be used in at least these three circumstances:

I’m not sure if I understand the second point… someone can give-me an example or explain? I’ve never used this in my code for the variables…

Honestly, I’ve never understood why it’s necesseary. If Gdevelop already acts in all instances and we filter them through conditions (and sometimes through some actions) it seems redundant to me to tell the game engine that it needs to happen in all instances just in some circunstances. Of course there is a reason, but I can’t figure out what it is. If anyone can explain, I would be very grateful. I believe that understanding the “why” to use as well as knowing the “when” to use it will help me identify the necessary moments to add this special event.

Thanks!

An example is where you have a number of enemies within a set distance of the player and you’d them to chase at different speeds, move in different angles, take a random amount of damage etc.

Or you’d like the enemies to take a random action (for example chase, shoot at, lob a bomb at etc). By using a repeat for each object, you can set each of these to be different.

1 Like

Oh! Like here in my code, right?

If I don’t put repeat for each instance then Gdevelop will take into account all instances instead of considering them separately… I think in this second point I read conditions instead of actions and so I didn’t understand :face_with_peeking_eye:

Thanks, MrMen! My code was already this way by pure trial and error. But now I think get it this second point!

Edit.: From now on, when I think of this event, I’m going to think of “Repeat for each instance separately”. It may be kind of redundant, but for some reason it’s clearer to me.

Not quite in this case. Those conditions are better done without the repeat because they can be applied to a all the Morcego objects than are to the left of Jogador (or to the right) in one go. There’s no unique change that’s happening.


That’s probably a good way to approach it. And then ask, “are the actions applied all different, or can they be applied to a group”. If the latter, then the “Repeat for each object” event may not be the most suitable.

1 Like

Thanks again, MrMen. About the example of “Jogador” and “Morcego” I did a test in a separated project without “Repeat for each instance” and I got this result:
gif

The event only works correctly if I use the Repeat. But thinking here I believe there is another explanation for this example: the condition “Compare two numbers” or “Compare two strings” does not filter the instances (I remember someone talking about this here on fórum, but don’t remember where). So my guess is that Repeat ends up being necessary to cover this specific issue of this conditions.

Yes, I’m going to try that. I’m almost done with the first phase and I’m going to start doing the next ones… So I should get some practice.

Apologies, I misread the condition - the function of comparing 2 numbers doesn’t filter the objects.

Carry on :smiley:

1 Like