The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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

in Engine by (101 points)

I'm not perfectly safe here but are you sure that _ready() isn't called?

"When instantiating a script or a standalone scene, nodes are not added
to the SceneTree upon creation, so no entertree 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.

Yeah, I'm sure _ready() isn't called, but _init() is.
I have Menu instantiated in a global script (autoload), like this :

func _ready():              #
# 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

func _ready():
print("ready")   # is NOT called

1 Answer

+3 votes
Best answer

_init() is the constructor and in that sense is supposed to be used for internal state initialization. It's possible to set up the scene from here as well, but there are cleaner alternatives.

The direct and indirect children of the scene's root node receive the PARENTED notification one by one as they are added to the isolated node tree. You can hook into this notification by checking for NOTIFICATION_PARENTED in the _notification() callback:

func _notification(what):
    match what:
            print("node added as child to its direct parent")

Nodes are added from top to bottom, depth-first.

Finally, by the same mechanism, the root node of the scene receives NOTIFICATION_INSTANCED once all children have been added to their parents. Since the root node does not have a parent during scene instantiation, it will not receive the NOTIFICATION_PARENTED notification.

by (1,096 points)
selected by

Great explanation ! Thank you !

NOTIFICATION_INSTANCED works perfectly for what I want to do.

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.