How do I connect to a signal without using the node that has the signal?

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

When wanting to connect to signals you need to have a reference to the script that emits that signal. So I want to know, is there a way to connect to a signal without the reference so that you’d just have to type the name of the signal in the connect() function?

:bust_in_silhouette: Reply From: cm

It’s pretty common to do this with an event bus autoload. The idea is that you define the signal in an autoload, and then connect and emit form there:

event_bus.gd (added as an autoload named EventBus)

signal my_signal()

player.gd

func _ready() -> void:
    EventBus.connect("my_signal", self, "on_my_signal")

func on_my_signal() -> void:
    print("my signal was called")

some_other_file.gd

func emit_signal() -> void:
    EventBus.emit("my_signal")

This way player.gd and some_other_file.gd don’t have to know anything about eachother… they only have to know about the event_bus.gd.

1 Like