[SOLVED] How to generate different random instance variables for each instance?

Heyo, I’m trying to make each instance of an enemy to create an AoE attack near the player at different X and Y values. Here is the code below:

The problem is that the randomly generated values are identical for each instance, which I do not want. I want the enemies to attack at different places.

This question is related (Creating multiple instances of an object with random values in them) but it wasn’t able to help me because the enemies in my game are not created after the scene begins and generating random variables multiple times does not result in different variables for each instance on my side.

Does anyone know how to approach this problem? Thanks! I apologize I couldn’t add more images, I’m new and can only embed one image at a time :D.

Hey hey, welcome!

Your event will run once and set the same random values for all the picked enemies. You need a “for each object” event to repeat the same actions for each enemy instance separately.

https://wiki.gdevelop.io/gdevelop5/events/foreach/

I don’t know what the object’s center point is set to but for random in range the 2nd number needs to be larger than the first because it’s used as a range. Example 1 to 100 works while 100 to 1 won’t. I believe it would always return 100.

It’s currently using +150 and -150. Repending on the value of centerX and cwnterY, switching the order of the values might fix it.

Yes, I was about to say that insein’s solution wasn’t working for me. I tried your solution and it works now! Thanks so much!

1 Like

After fixing what Keith_1357 pointed out, this solution worked perfectly. Thanks so much!

1 Like

For anyone needing this in the future, here’s what my code looked like in the end.

I’d suggest a slightly more efficient method. Put those two conditions in a normal event, and then move the “Repeat for each stationary_enemy_1”` as a sub-event off that.

This way the repeat only goes over the eligible objects, and not over all objects to see if they’re eligible. There is a processing cost with the latter.

Like this?

(i also included other sub-events that weren’t in the other screenshots here)

Yes, but remove the trigger once in the repeat event.

And I’d check whether those 3 second waits do anything. I’m not sure if the subevents are delayed in getting run, or if they run regardless.

removing the “trigger once” event spawns multiple copies of the attack, which I need only one of since the game is turn-based (sorry for not providing enough info!).

and yes, the wait actions do work.

In that case, ditch the repeat event, and use a standard event with a “Pick random stationary_enemy_1” condition. There’s no point in have a repeat if you only want the events to be actioned once.