0 votes

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?

Godot version v3.4.2
in Engine by (51 points)

1 Answer

+1 vote
Best answer

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.

by (280 points)
selected by
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.