0 votes

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():
    get_node("bullet_generator1").fire_rate_timer.start()
    get_node("bullet_generator1")._fire()
    print("start")

func _on_halt_fire_halt():
    self.get_node("bullet_generator1").fire_rate_timer.stop()
    print("stop")

Boundary code

extends Node2D

signal halt

func _on_Halt_area_entered(area):
    if area.is_in_group("enemies"):
        emit_signal("halt")

https://ibb.co/n1Vq291

in Engine by (46 points)

2 Answers

+1 vote
Best answer

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.

by (19,274 points)
edited by

Worked perfectly. Thank you.

+1 vote

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

by (8,528 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.