General mechanism of how scenes are initialized

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

I started learning Godot and I’m starting to guess what happens when a scene is initialized.
Here are the steps I think might be happening :

  • first the engine instantiates the root node
  • the next node, a child of the root node is instantiated, but with parameters that depend on the root node : the engine looks for classes that the the child and parent node have in common in their respective inheritance trees, then modifies the parameters of the common classes. For example, all 2d nodes have Node2D in common, so for the position parameter, child nodes will be initialized with Vector2(childX + parentX, childY + parentY).
    -the engine descends the node tree applying this principle, and does the same thing when entering a scene (ie new node tree)
    If a script is attached to a node, then when the engine gets to it, that node is not initialized: instead a new node that inherents from that node with a custom script is initialized.

That basically sums up my understanding. I was wondering if this actually corresponds to what the engine is doing behind the scenes, and if anyone has more details or interesting thoughts on the matter. I’m also open to answers relating to source code if someone finds it interesting. I won’t dive in myself because I don’t know C++ and I’ve never tried reading a codebase as large and complexe as one like Godot.

Thank you for reading

Here’s a good start: Using SceneTree — Godot Engine (4.0) documentation in English

spaceyjase | 2023-07-03 16:15

Why would You need this knowledge ?
For practical uses, all You should know is that first init() is called descending down the tree statrting from the root, so the scripts and instances are created, and next ready() is called ascending up the tree, so individual instances are childed, and root is childed in the end.

Inces | 2023-07-03 18:03