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