Godot notifications
Some of these notifications, like draw, are useful to override in scripts. So much so that Godot exposes many of them with dedicated functions:
_ready()
: NOTIFICATION_READY_enter_tree()
: NOTIFICATION_ENTER_TREE_exit_tree()
: NOTIFICATION_EXIT_TREE_process(delta)
: NOTIFICATION_PROCESS_physics_process(delta)
: NOTIFICATION_PHYSICS_PROCESS_draw()
: NOTIFICATION_DRAW
What users might not realize is that notifications exist for types other than Node alone:
Object::NOTIFICATION_POSTINITIALIZE: a callback that triggers during object initialization. Not accessible to scripts.
: a callback that triggers before the engine deletes an Object, i.e. a ‘destructor’.
MainLoop::NOTIFICATION_WM_MOUSE_ENTER: a callback that triggers when the mouse enters the window in the operating system that displays the game content.
: a callback that triggers anytime one adds a child node to another node.
Node::NOTIFICATION_UNPARENTED: a callback that triggers anytime one removes a child node from another node.
: a callback that triggers after a Popup node completes any
popup*
method. Note the difference from itsabout_to_show
signal which triggers before its appearance.
One can access all these custom notifications from the universal _notification
method.
Note
Methods in the documentation labeled as “virtual” are also intended to be overridden by scripts.
A classic example is the _init method in Object. While it has no NOTIFICATION_*
equivalent, the engine still calls the method. Most languages (except C#) rely on it as a constructor.
So, in which situation should one use each of these notifications or virtual functions?
Use _process
when one needs a framerate-dependent deltatime between frames. If code that updates object data needs to update as often as possible, this is the right place. Recurring logic checks and data caching often execute here, but it comes down to the frequency at which one needs the evaluations to update. If they don’t need to execute every frame, then implementing a Timer-yield-timeout loop is another option.
GDScript
Use _physics_process
when one needs a framerate-independent deltatime between frames. If code needs consistent updates over time, regardless of how fast or slow time advances, this is the right place. Recurring kinematic and object transform operations should execute here.
One can check for input actions within the input callbacks just the same. If one wants to use delta time, one can fetch it from the related deltatime methods as needed.
GDScript C#
# Called every frame, even when the engine detects no input.
func _process(delta):
if Input.is_action_just_pressed("ui_select"):
print(delta)
func _unhandled_input(event):
"InputEventKey":
if Input.is_action_just_pressed("ui_accept"):
print(get_process_delta_time())
If the script initializes its own node subtree, without a scene, that code should execute here. Other property or SceneTree-independent initializations should also run here. This triggers before _ready
or _enter_tree
, but after a script creates and initializes its properties.
Scripts have three types of property assignments that can occur during instantiation:
GDScript C#
# "one" is an "initialized value". These DO NOT trigger the setter.
# If someone set the value as "two" from the Inspector, this would be an
# "exported value". These DO trigger the setter.
export(String) var test = "one" setget set_test
func _init():
# "three" is an "init assignment value".
# These DO NOT trigger the setter, but...
test = "three"
# These DO trigger the setter. Note the `self` prefix.
self.test = "three"
func set_test(value):
test = value
print("Setting: ", test)
When instantiating a scene, property values will set up according to the following sequence:
Exported value assignment: If instancing from a scene rather than a script, Godot will assign the exported value to replace the initial value defined in the script.
As a result, instantiating a script versus a scene will affect both the initialization and the number of times the engine calls the setter.
When instantiating a scene connected to the first executed scene, Godot will instantiate nodes down the tree (making _init
calls) and build the tree going downwards from the root. This causes _enter_tree
calls to cascade down the tree. Once the tree is complete, leaf nodes call . A node will call this method once all child nodes have finished calling theirs. This then causes a reverse cascade going up back to the tree’s root.
When instantiating a script or a standalone scene, nodes are not added to the SceneTree upon creation, so no _enter_tree
callbacks trigger. Instead, only the _init
call occurs. When the scene is added to the SceneTree, the _enter_tree
and _ready
calls occur.
GDScript C#
extends Node
var parent_cache
func connection_check():
return parent.has_user_signal("interacted_with")
func _notification(what):
match what:
NOTIFICATION_PARENTED:
parent_cache = get_parent()
if connection_check():
parent_cache.connect("interacted_with", self, "_on_parent_interacted_with")
NOTIFICATION_UNPARENTED:
if connection_check():
parent_cache.disconnect("interacted_with", self, "_on_parent_interacted_with")
func _on_parent_interacted_with():