Multiple instantiated versions of an enemy - not triggering bools like it should

Godot Version

4.5

Question

Hello! Im having a strange problem that is probably just me missing something obvious, but i cant seem to figure it out so i was hoping someone might have an idea.

The problem is when i instantiate multiple scenes of the same enemy. I have a Shapecast3d called “player detector” that sensen when the player is infront of it and in reach for attack. the attacking works well on its own, but when the player dies the enemy is supposed to execute them with an animation. The issue is that this works fine when its just one enemy, but if i add more then i works for the last enemy that was added, but not for the ones before.

So if i add one enemy, it works for that one. if i add another enemy so i now have enemy1 and enemy2, it doesnt work for enemy1 but it works for enemy2. if i then add a third, it works for enemy3 but not for 1 and 2. The issue i seem to be having is that the bool thats supposed to be set to true when the player is in enemy reach doesnt get set to true for any enemy other than the last one.

in the pictures you can see the code responsible for setting the InEnemyReach bool to true. in the output picture we can see that for the last enemy the inEnemyReach bool is true, and for the first enemy it remains false despite the player detector turning to true for both of them.

So far ive tried to make each enemy local and access as unique name. those were the solutions that kept coming up when i was searching for a solution on my own. Those might still be the issue, i might just have missinterpreted how to use them, so if you think those are still the problem, please explain how, thanks!

In the meantime ill keep looking and update this post if i find a solution for any other noobs running into this issue, cheerio!

also, ignore the commented out code, its remnants from another thing thats been removed.


It is a confusing set up. I would guess the problem lies in player.set_enemy(self). Is it setting an array of enemies? Same for set_in_enemy_reach.

Why, if you are in a for loop for getting collision counts, do you then check in another if statement if the player detector is colliding or not? What happens if collision count is 0? And what are you using the collision_id for?

Personally I think your code is confusing and needs some refactoring. And I think we need to see more code.

Lets say your enemy is in a state ‘looking for player’. The enemy should do their own player detection. If the player is in reach, then change its state to attacking. During this state, if the player dies, change state to executing. If the player does not die during an attack, when the attack is over, change back to ‘looking for player’ (or whatever). If the player is still in reach, change back to attacking again and repeat.

When the player dies, emit a signal. Any enemy currently attacking can then enter execution state.

These states and what you call them etc will all depend on how you want the gameplay to play out of course. But the point is the player does not need to keep a list of enemies in reach, or what enemy is attacking. The player only needs to know it is receiving damage.

Any way, I think we need to see more code to properly help. But I suspect your player is only storing one enemy at a time.

I hope that helped in some way.

1 Like

Hello! Yea the player is storing the enemy currently interacting with it, which admittedly is a remnant from an even older code that i started out with and has kinda worked well enough until now. the idea behind checking if player is in reach with a bool is that it tells the player “hey, the enemy is gonna execute you now, and you are in reach, so play your animation”

Im gonna sit down and remove all of the older code as i feel like its becoming a bit too much work to keep it relevant. the thing that confuses me about the issue im having tho is that if i print out the enemy, their player detectors and all that, they all have unique ID’s, so crap code aside, im not sure why the issue crops up in the first place.

“But the point is the player does not need to keep a list of enemies in reach, or what enemy is attacking. The player only needs to know it is receiving damage”

the execution animation is a paired animation that depends on the player and enemy positioning themselves correctly, is there a way to make the player and enemy get to proper position without the player knowing which enemy is going to play the animation? or perhaps the player could have an execution position and rotation that is set in the enemy when the animation plays.. ill have a look at that.

Im gonna follow your suggestion and see where i end up, thanks!

1 Like

This refactoring is quite normal, don’t feel bad. It’s not you, it’s the nature of the game dev beast.

Yesterday I decided to change my mission targets UI. That made me have to change the way missions were stored, which meant changing the mission generators, which meant changing the mission progression checkers, which meant changing the game manager. Not small changes either, massive ones. By the end of today I might be back to where I was two days ago, only this time my missions UI will be a tiny bit different! The code will be lovely though.

TBH I am probably over-optimising early but when you get an idea in your head…

2 Likes