Connecting signals through editor, translated in GDScript

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

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:
https://forum.godotengine.org/140848/signal-is-not-reaching-node

:bust_in_silhouette: Reply From: Whom

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():
    pass

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).

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!)

toxicvgl | 2022-10-26 17:07

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.

Whom | 2022-10-27 08:47

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).

toxicvgl | 2022-10-27 17:27