Does _enter_tree signal triggers upon instancing or over adding the node to the tree?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Guilherme Furst
:warning: Old Version Published before Godot 3 was released.

So to give a quick brief of what I thought of doing:
I have a base scene, exporting attributes pertaining a certain type of situation, this base will be inherited by several others, filling in the attributes with content where due, good design right?

I’m going to use a resourcePreloader to pipe the loading process of everything and the depending resources. Then when needed, the running game scene can just call from the storage and child it on the tree. I am however unsure if I should store these scenes as PackedScenes or instanced versions of it, because of two key aspects:

  1. whether or not _enter_tree is sent upon instancing or upon entering the working tree, added as a child to main viewport, this is important to know when the processes will take place, as it will need a creating phase, and a showing phase.

  2. it may be needed to make alterations to the scene while on storage, change of the exported attributes, it may be the case that you can’t do that on PackedScenes, its a different question but related to the above.

Thanks, any other insightful suggestion to the above use case is also welcomed.

:bust_in_silhouette: Reply From: hilfazer

_init() is called upon instancing. It’s something like a constructor.
_enter_tree() is called after node is added to SceneTree and before its children are.
_ready() is called after both node and its children are added to SceneTree.
_exit_tree() is called before node is removed from SceneTree. It’s not destructor’s equivalent. ‘Destructor’ in GDscript is this:

func _notification(what):
        # destruction code

One thing to remeber is the node will not have access to SceneTree instance before actually entering it. It can be solved by creating and autoload with function that calls get_tree() but i have no idea if it’s a good practice or not.

Hope that helps.

Thanks, I was thinking about this and indeed I forgot about the _init function, which should be the proper constructor indeed. The reason for the mess I thinkis the way _enter_tree is described on the documentation:

Called when the node enters the SceneTree (e.g. upon instancing, scene changing or after calling add_child() in a script). If the node has children, its _enter_tree() callback will be called first, and then that of the children.

But thanks, I have a better idea on how use each of these now.

Guilherme Furst | 2018-01-28 13:50