Order
Inserting a node into the scene tree
As far as I can tell from the docs this is the order of events when creating a new node and adding it:
- the constructor (
MyObject.new()
) _init()
_enter_tree()
- (This happens after the node is added to the Scene Tree using
add_Child()
)
- (This happens after the node is added to the Scene Tree using
- ready in Godot
- This happens after every child node1 has been initialized, added to the scene, tree and has already called
_ready()
.
- This happens after every child node1 has been initialized, added to the scene, tree and has already called
_init()
usually is called after the constructor finishesIt seems that we should assume that
_init()
will be called after the constructor, but unfortunately Godot doesn’t seem to guarantee the order? Read this from the docs“
_init()
is Called when the object’s script is instantiated, oftentimes after the object is initialized in memory (throughObject.new()
in GDScript, ornew GodotObject
in C#). It can be also defined to take in parameters. This method is similar to a constructor in most programming languages.”
Instantiating a scene
As for as I can tell from the docs, this is the order of events when instantiating a scene and adding it2:
- Initial value assignment: the property is assigned its initialization value, or its default value if one is not specified. If a setter exists, it is not used.3
_init()
assignment: the property’s value is replaced by any assignments made in_init()
, triggering the setter.- Exported value assignment: an exported property’s value is again replaced by any value set in the Inspector, triggering the setter.
GDScript Example
Gotcha: Setting a Resource type in the inspector and leaving all its properties at default values
Quote
From: Godot notifications — Godot Engine (stable) documentation in English @ User-contributed notes:
There’s a gotcha with object initialization. When setting Resource types in the inspector, and leaving all its @exported Variant data members (for example) at default values, the default values will overwrite values set in its _init() function as if they were set via the inspector. Example below:
(https://docs.godotengine.org/en/stable/tutorials/best_practices/godot_notifications.html#init-vs-initialization-vs-export)If my_resource is set in the inspector, MyResource.my_int will be -1. If my_resource isn’t set via inspector, it will be 2.
Work-around: Implement a private initialization() function that you [[
call_deferred()
in Godot|call_deferred()]] at the end of the _init() function. The deferred function will execute after the @export properties are initialized so are overwritten.