Hi guys!
I have a question about placing several entities of the same object (the enemies in the game). I try to give them some behaviour, like a super simple FSM. When you trigger them they follow you and if they touch you they run away again. This works fine for the first enemy. The second enemy only does part of the behaviour though. I placed an “repeat for each object” in front of the whole behaviour, because I thought that would do it, but not really. Are there ways to use several of the same object, when they have a little more complex behaviour? Or should it in principle work, but I just don’t get something?
Thanks a lot!
@pregonateSloth Probably it would be best to actually create a new “behavior”. Similar to the existing object behaviors (drag & drop, pathfinding, platformer, etc) you can create your own using events (or javascript)
As far as I understand this would ensure that every instance will have similar behaviors relative to the instance.
To learn more on how to create them take a read at the manual section for that: Custom behaviors: create your own behaviors with events [GDevelop wiki]
Regarding the for each, behind the scenes it’s actually a for loop that operates on an array of instances (sort of)
So whatever condition you put as part of the for each is like saying “if this thing happens, do something for each of these objects” so the condition is “global” and the actions become specific to the objects.
You should probably need to give each enemy an instance variable with a number Id (e.g 1,2,3, etc), it seems a common pattern mentioned around here.