I sent a signal from player to enemy to deal damage but it damages every enemy in the scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By javrocks

Its only suppose to reduce the health of that enemy i attacked not all of them

    #Enemy Script

var Player = get_tree().get_root().find_node("Player2D", true, false)
Player.connect("attack", self, "damage")
func damage(): 
    health -= 10

I also tried using this function to stop it from decreasing health if target is not itself but it didn’t work

func damage(target: Node) -> void:
    if not target == self:

    health  -= 10

javrocks | 2022-02-18 20:59

:bust_in_silhouette: Reply From: Inces

This fix definetely should work. Unless problem lies within your health variable. Can it be reference to something global ?

Show the code in player where signal is emitted, with binded argument, and show the code introducing health - is it in subclass ?

This is just it


signal attack

func strike() -> void:

    if is_colliding():
	    if get_collider().is_in_group("Enemy"):

javrocks | 2022-02-18 22:16

But You said You tried to do damage(target:node). How did You emit signal with this fix ? Because You had to do emit_signal(“attack”,get_collider())

Inces | 2022-02-19 14:39

If the player detects Collison and the collider is in group enemy it emits a signal called “attack” The Enemy receives the signal and calls the damage() function. The signal and damage() function work but it also damages all the enemies that are instanced. When i add in the function that’s below nothing works tho, health dosen’t decrease nor does it print “returned”

# in Enemy script
func damage(target: Node) -> void:
    if not target == self:
    health -=10

BTW Im not exactly sure what this function does i just know about it. Correct me if i’m wrong but it returns if Node is not itself. Do i have to declare target as a variable because i didn’t do that

javrocks | 2022-02-19 19:46

You are correct. This script should print “returned” only when he is the target. But this target must be passed during signal emission. SO the player should do :
Did You do it like this ? Or do You just emit_signal("attack") with nothing else ?

Inces | 2022-02-20 12:16

OMG i put in (“attack”,get_collider()) and it works. Thx alot

javrocks | 2022-02-20 13:20

You are welcome
hint - You can select answer as best to reward it :wink:

Inces | 2022-02-20 15:05