Overview
This post describes the initialization and execution order of scripts that are attached to Nodes in the Godot game engine. I created a GitHub repository that logs these notifications and I recommend playing around with a similar project to familiarize yourself.
Initialization
Member initialization happens in this order:
static
members are initialized._static_init()
is called.- instance members are initialized.
_init()
is called.@export
initialization is applied, which uses property setters if available.
Notifications
Most execution is done from top to bottom of the order of the scene if all nodes are expanded. Exceptions to this are:
_ready()
,NOTIFICATION_READY
, andNOTIFICATION_POST_ENTER_TREE
are called children first. This means a parentNode
will only get these notifications when all its its children have already received these notifications._input()
and_unhandled_input()
are called bottom to top. This means nodes later in the tree get first chance to handle input._exit_tree()
and related notifications are called bottom to top. (Conversely,NOTIFICATION_UNPARENTED
are called top to bottom.)
Most notifications are given to all nodes before moving on to the next notification. Exceptions to this are:
_ready()
,NOTIFICATION_READY
, andNOTIFICATION_POST_ENTER_TREE
happen in that order for each node before moving to the next node._physics_process()
andNOTIFICATION_PHYSICS_PROCESS
happen in that order for each node before moving to the next node._process()
andNOTIFICATION_PROCESS
happen in that order for each node before moving to the next node._exit_tree()
and related notifications such asNOTIFICATION_EXIT_TREE
,Control.NOTIFICATION_MOUSE_EXIT
, andCanvasItem.NOTIFICATION_EXIT_CANVAS
happen in that order for each node before moving to the next node.
Signals
When a signal is emitted, it is received in the order that the connections were made. Signals are received immediately when they are emitted, even in the middle of a _process
sequence, for example.
*_input
Methods, Button
Methods, etc.
*_input
methods are called between _process
and _physics_process
calls and do not interrupt the sequence of _process
or _process_physics
. Button
signals and methods are similarly called after the all of the *_input
methods have been completed.
queue_free()
queue_free()
will free those objects without interrupting the _process
or _physics_process
sequence.