![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | siska |
As the title says …
I have a Menu scene that is preloaded and instantiated, but not yet added to the main sceneTree.
In the Menu, I need to get references to buttons, connect their signals to methods, and enable/disable some of the btns.
I usually do stuff like that in _ready(), but since the Menu is not part of the sceneTree, I can’t use that.
So : _init().
But is this safe to use ? I know that in other programming languages you shouldn’t touch nodes until the nodeTree is ready …
I’m not perfectly safe here but are you sure that _ready() isn’t called?
Godot notifications — Godot Engine (latest) documentation in English
“When instantiating a script or a standalone scene, nodes are not added
to the SceneTree upon creation, so no _enter_tree callbacks trigger.
Instead, only the _init and later _ready calls occur.”
As I understand _init() will be callled first for the parent, then for the childs (on _ready it’s the other way round AFAIK). So you may not get references to the button in the parents _init()?
But as I said on the begin, I’m not safe on this. If _ready() works, it should be safe to use though.
wombatstampede | 2019-02-19 15:26
Yeah, I’m sure _ready() isn’t called, but _init() is.
I have Menu instantiated in a global script (autoload), like this :
func _ready(): # global.gd
# preload menu and keep around
var m = preload("res://scenes/Menu.tscn")
menu = m.instance() # this does not trigger Menu._ready() !!!
In Menu.tscn, I have :
func _init():
print("init") # is called from global.gd
func _ready():
print("ready") # is NOT called
siska | 2019-02-19 16:01