Signal emitting for all instances

Godot 4.2.1

I have a question, I have some enemies in the scene, and each one have a healthcomponent as the

in the script of the HealthComponent i have this:
when the enemy gets damaged, i emit the health_updated

image

and in my enemy script, i connect to this signal, and then play the animation hurt,
as this image show: Imgur: The magic of the Internet
but in this case all of the enemies plays the animation, i dont get why this happens

The concept of instances does not apply to signals. Think of them as always static/virtual.

You just need to pass some sort of key/identifier to specify that it was that specific enemy that died. You can also just pass the reference to the enemy object. For example:

in HealthComponent class, change signal declaration and add this var:

signal health_updated(enemy)
var enemy: Enemy #or w/e the name of your base enemy class is...

Whereever you’re assigning HealthComponent to an enemy:

my_enemy.health_component = hc
hc.enemy = my_enemy

When emitting signal inside HealthComponent:

health_updated.emit(enemy)

and finally in your enemy class:

func on_health_updated(enemy):
	if enemy == self:
		animation.play("hurt")

oh i realized why it was happening, it was not about the signal, it was the sprite 2d material that was not local scene turned on


but thank you for the reply

Great! B/c I wrote my reply before coffee and completely didn’t think that you’re connecting to each instance’s signal and… actually not sure what I was thinking :smiley:

1 Like