Godot notifications

Every Object in Godot implements a _notification method. Its purpose is to allow the Object to respond to a variety of engine-level callbacks that may relate to it. For example, if the engine tells a CanvasItem to “draw”, it will call _notification(NOTIFICATION_DRAW).

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
  • _input() : NOTIFICATION_INPUT
  • _unhandled_input() : NOTIFICATION_UNHANDLED_INPUT
  • _draw() : NOTIFICATION_DRAW

What users might not realize is that notifications exist for types other than Node alone:

And many of the callbacks that do exist in Nodes don’t have any dedicated methods, but are still quite useful.

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?

_process vs. _physics_process vs. *_input

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

  1. # Infinitely loop, but only execute whenever the Timer fires.
  2. # Allows for recurring operations that don't trigger script logic
  3. # every frame (or even every fixed frame).
  4. while true:
  5. my_method()
  6. $Timer.start()
  7. yield($Timer, "timeout")

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.

While it is possible, to achieve the best performance, one should avoid making input checks during these callbacks. _process and _physics_process will trigger at every opportunity (they do not “rest” by default). In contrast, *_input callbacks will trigger only on frames in which the engine has actually detected the input.

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#

  1. # Called every frame, even when the engine detects no input.
  2. func _process(delta):
  3. if Input.is_action_just_pressed("ui_select"):
  4. print(delta)
  5. # Called during every input event.
  6. func _unhandled_input(event):
  7. match event.get_class():
  8. "InputEventKey":
  9. if Input.is_action_just_pressed("ui_accept"):
  10. print(get_process_delta_time())
  1. public class MyNode : Node
  2. {
  3. // Called every frame, even when the engine detects no input.
  4. public void _Process(float delta)
  5. {
  6. if (Input.IsActionJustPressed("ui_select"))
  7. GD.Print(delta);
  8. }
  9. // Called during every input event. Equally true for _input().
  10. public void _UnhandledInput(InputEvent event)
  11. {
  12. switch (event)
  13. {
  14. case InputEventKey keyEvent:
  15. if (Input.IsActionJustPressed("ui_accept"))
  16. GD.Print(GetProcessDeltaTime());
  17. break;
  18. default:
  19. break;
  20. }
  21. }
  22. }

_init vs. initialization vs. export

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#

  1. # "one" is an "initialized value". These DO NOT trigger the setter.
  2. # If someone set the value as "two" from the Inspector, this would be an
  3. # "exported value". These DO trigger the setter.
  4. export(String) var test = "one" setget set_test
  5. func _init():
  6. # "three" is an "init assignment value".
  7. # These DO NOT trigger the setter, but...
  8. test = "three"
  9. # These DO trigger the setter. Note the `self` prefix.
  10. self.test = "three"
  11. func set_test(value):
  12. test = value
  13. print("Setting: ", test)
  1. public class MyNode : Node
  2. {
  3. private string _test = "one";
  4. // Changing the value from the inspector does trigger the setter in C#.
  5. [Export]
  6. public string Test
  7. {
  8. get { return _test; }
  9. set
  10. {
  11. _test = value;
  12. GD.Print("Setting: " + _test);
  13. }
  14. }
  15. public MyNode()
  16. {
  17. // Triggers the setter as well
  18. Test = "three";
  19. }
  20. }

When instantiating a scene, property values will set up according to the following sequence:

  1. Initial value assignment: instantiation will assign either the initialization value or the init assignment value. Init assignments take priority over initialization values.
  2. 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.

_ready vs. _enter_tree vs. NOTIFICATION_PARENTED

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 _ready. 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 and later _ready calls occur.

If one needs to trigger behavior that occurs as nodes parent to another, regardless of whether it occurs as part of the main/active scene or not, one can use the PARENTED notification. For example, here is a snippet that connects a node’s method to a custom signal on the parent node without failing. Useful on data-centric nodes that one might create at runtime.

GDScript

C#

  1. extends Node
  2. var parent_cache
  3. func connection_check():
  4. return parent.has_user_signal("interacted_with")
  5. func _notification(what):
  6. match what:
  7. NOTIFICATION_PARENTED:
  8. parent_cache = get_parent()
  9. if connection_check():
  10. parent_cache.connect("interacted_with", self, "_on_parent_interacted_with")
  11. NOTIFICATION_UNPARENTED:
  12. if connection_check():
  13. parent_cache.disconnect("interacted_with", self, "_on_parent_interacted_with")
  14. func _on_parent_interacted_with():
  15. print("I'm reacting to my parent's interaction!")
  1. public class MyNode : Node
  2. {
  3. public Node ParentCache = null;
  4. public void ConnectionCheck()
  5. {
  6. return ParentCache.HasUserSignal("InteractedWith");
  7. }
  8. public void _Notification(int what)
  9. {
  10. switch (what)
  11. {
  12. case NOTIFICATION_PARENTED:
  13. ParentCache = GetParent();
  14. if (ConnectionCheck())
  15. ParentCache.Connect("InteractedWith", this, "OnParentInteractedWith");
  16. break;
  17. case NOTIFICATION_UNPARENTED:
  18. if (ConnectionCheck())
  19. ParentCache.Disconnect("InteractedWith", this, "OnParentInteractedWith");
  20. break;
  21. }
  22. }
  23. public void OnParentInteractedWith()
  24. {
  25. GD.Print("I'm reacting to my parent's interaction!");
  26. }
  27. }