+2 votes

We can connect signals through the editor, but what code is equivalent to it?
To be precise, suppose we have an Emitter node and a Receiver node in a scene, Emitter has a signal signal_emitted, and Receiver has a function _on_Emitter_signal_emitted(). My first guess is:

# Receiver.gd
# How does the editor know the path of the Emitter?
onready var emitter = get_node(emitter_path) 

func _ready():
    emitter.connect("signal_emitted", self, "_on_Emitter_signal_emitted")

Is this code exactly equivalent to connecting it through the editor?
What I would like to ask is the exact function of signals in editor.

I saw the below and came up with this question:

Godot version 3.5.1
in Engine by (46 points)
edited by

1 Answer

+1 vote

Yes. For example, if I had the following scene...

VBoxContainer (script.gd)
- HBoxContainer
-- Button
- HBoxContainer
-- Label

...and I wanted connect the Button node's signal "button_up" to the root node in the script attached to it (script.gd in this case), I would connect it like this:

onready var button = $HBoxContainer/Button

func _ready():
    button.connect("button_up", self, "_on_button_up")

func _on_button_up():

If you need to emit and connect signals from different scenes, it is a good idea to have EventBus singleton (here is a good tutorial).

I always connect signals in code but there's no right or wrong way here. But I think that it would be extremely hard to only connect signals through the Editor so it's likely you will need to connect signals in code anyway so I think it makes sense to always do it in code just to be consistent (of course, this is just my preference).

by (98 points)

Thank you for your answer! I am sorry for not being precise, what I would like to ask was how exactly signals in editor works (edited the question). For example, does the editor takes the path of button like you wrote? Does it happen in _ready()? These details may be important when nodes can move or be instantiated. (And yes, that's the reason why I like connecting signals only through codes!)

Sorry, I don't know the exact workings of signals. What do you mean by taking the path like I wrote? But yes, because I used the keyword onready, it is initialized "on ready":

"onready initializes a variable once the Node the script is attached to and its children are part of the scene tree" (Docs)

Are you asking if the signals reconnect automatically in the case that a node's position in the scene tree changes or if a new node is added? As far as I know, that's not possible.

Thank you for your reply, and that's what I am asking. I cannot help saying "never ever connect signals in the editor" now.
My whining is that the Docs should contain the equivalent code (it may become obsolete after 4.0 though).

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.