[SOLVED] Give my enemies an independant behavior

Hi guys!
I’m sorry to bother you with this question but I can’t figure out what is wrong. I’ve read some of the threads talking about the same issue but I can’t seem to get this basic problem fixed.

I’ve created an enemy "SmallSlime_Green " and a “vision” box (a sprite) : “SP_Enemy_CAC_Vision”. I’ve used the “stick” extension to link them together.
I’ve successfully coded them to “patrol” to and fro. When they are patroling and the player is entering the vision box (in yellow), they change the animation and they go toward the player to attack him.
It works well when there is only one instance of the enemy.

When there are 2, things don’t work as I’d like : when the player enters the vision box, the 2 enemies change their animation and move toward the player to attack.

I’d like them to behave independently.

I’ve tried using the “for each instance” block but it didn’t make any difference. Perhaps I didn’t use it at the right place…
Here is a screenshot of the code that set the boolean “NR_detected” true. Once it’s true, the enemies attack.

Truth is, in spite of reading about the “for each instance” block, I don’t really understand what it does…

Thanks for your help!

When you setting NR_detected, you need to pick the matching slime object. I’m not positive if would require a for each object Without the picking I’m assuming it’s set all of the instance to true.

After the vision is in collision with rose add the condition is stuck to another object under [other] [stick objects to others] that will pick the slime object that the vision is stuck to.

As far as for each object this goes thru the list of picked objects and runs any events and sub events. The actions and conditions only apply to one object at a time. They’re usually only needed if you’re comparing the objects to different objects or values or you have an action that you’re using with different values.

Hi @Keith_1357 !

Thanks for taking the time to look at the code!
Your idea was on point! And I’m amazed at how simple that was…I’ll try removing the “for each” blocks as I put them only because I had run out of ideas!
Anyways, thank you very much because I have been struggling with this for several days now, writing and rewriting the code!
Thank you for the explanation about “for each”, I’ll meditate on that :smiley:

1 Like

Hi again!
Just to let you know that I’ve made the changes, modified a few things, among which I removed most of the “for each”, used the condition “is stuck to” and everything works perfect!
Thanks a lot cause I’d never thought about it!

1 Like

You’re welcome. The way instances behave still surprises and confuses me sometimes. You don’t need the for each most of the time because GDevelop seems to do it’s own form of a for each internally.

Good luck with your project.

1 Like