Signal affecting all instances of an enemy

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

I have a boundary that when passed emits a signal to stop an enemy from shooting, but when I add two enemies to a scene, if just one of them passes the boundary they all stop firing.

Enemy shooting code

  func _on_VisibilityNotifier2D_screen_entered():

func _on_halt_fire_halt():

Boundary code

extends Node2D

signal halt

func _on_Halt_area_entered(area):
	if area.is_in_group("enemies"):
:bust_in_silhouette: Reply From: exuin

Try calling a method on the specific enemy that entered instead of using a signal that is connected to all enemies.

:bust_in_silhouette: Reply From: jgodfrey

Or, as an alternative to @exuln’s (perfectly valid) answer, you could continue to use the generic signal but pass the specific area along with it. Then, each receiver can check the included area argument and only act on the signal if they are the specifically intended receiver.

So, (based on your above code) emit something like this:

emit_signal("halt", area) # pass the specific area along in the signal

And, in the enemy signal handler, something like:

func _on_halt(area):
    if area == self:
        # signal was intended for THIS INSTANCE - process it.

So, essentially, while every enemy will receive the signal, only the intended enemy will react to it.

Worked perfectly. Thank you.

wolvertox | 2022-12-13 13:38