godot_notifications.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. .. _doc_godot_notifications:
  2. Godot notifications
  3. ===================
  4. Every Object in Godot implements a
  5. :ref:`_notification <class_Object_private_method__notification>` method. Its purpose is to
  6. allow the Object to respond to a variety of engine-level callbacks that may
  7. relate to it. For example, if the engine tells a
  8. :ref:`CanvasItem <class_CanvasItem>` to "draw", it will call
  9. ``_notification(NOTIFICATION_DRAW)``.
  10. Some of these notifications, like draw, are useful to override in scripts. So
  11. much so that Godot exposes many of them with dedicated functions:
  12. - ``_ready()``: ``NOTIFICATION_READY``
  13. - ``_enter_tree()``: ``NOTIFICATION_ENTER_TREE``
  14. - ``_exit_tree()``: ``NOTIFICATION_EXIT_TREE``
  15. - ``_process(delta)``: ``NOTIFICATION_PROCESS``
  16. - ``_physics_process(delta)``: ``NOTIFICATION_PHYSICS_PROCESS``
  17. - ``_draw()``: ``NOTIFICATION_DRAW``
  18. What users might *not* realize is that notifications exist for types other
  19. than Node alone, for example:
  20. - :ref:`Object::NOTIFICATION_POSTINITIALIZE <class_Object_constant_NOTIFICATION_POSTINITIALIZE>`:
  21. a callback that triggers during object initialization. Not accessible to scripts.
  22. - :ref:`Object::NOTIFICATION_PREDELETE <class_Object_constant_NOTIFICATION_PREDELETE>`:
  23. a callback that triggers before the engine deletes an Object, i.e. a
  24. "destructor".
  25. And many of the callbacks that *do* exist in Nodes don't have any dedicated
  26. methods, but are still quite useful.
  27. - :ref:`Node::NOTIFICATION_PARENTED <class_Node_constant_NOTIFICATION_PARENTED>`:
  28. a callback that triggers anytime one adds a child node to another node.
  29. - :ref:`Node::NOTIFICATION_UNPARENTED <class_Node_constant_NOTIFICATION_UNPARENTED>`:
  30. a callback that triggers anytime one removes a child node from another
  31. node.
  32. One can access all these custom notifications from the universal
  33. ``_notification()`` method.
  34. .. note::
  35. Methods in the documentation labeled as "virtual" are also intended to be
  36. overridden by scripts.
  37. A classic example is the
  38. :ref:`_init <class_Object_private_method__init>` method in Object. While it has no
  39. ``NOTIFICATION_*`` equivalent, the engine still calls the method. Most languages
  40. (except C#) rely on it as a constructor.
  41. So, in which situation should one use each of these notifications or
  42. virtual functions?
  43. _process vs. _physics_process vs. \*_input
  44. ------------------------------------------
  45. Use ``_process()`` when one needs a framerate-dependent delta time between
  46. frames. If code that updates object data needs to update as often as
  47. possible, this is the right place. Recurring logic checks and data caching
  48. often execute here, but it comes down to the frequency at which one needs
  49. the evaluations to update. If they don't need to execute every frame, then
  50. implementing a Timer-timeout loop is another option.
  51. .. tabs::
  52. .. code-tab:: gdscript GDScript
  53. # Allows for recurring operations that don't trigger script logic
  54. # every frame (or even every fixed frame).
  55. func _ready():
  56. var timer = Timer.new()
  57. timer.autostart = true
  58. timer.wait_time = 0.5
  59. add_child(timer)
  60. timer.timeout.connect(func():
  61. print("This block runs every 0.5 seconds")
  62. )
  63. .. code-tab:: csharp
  64. using Godot;
  65. public partial class MyNode : Node
  66. {
  67. // Allows for recurring operations that don't trigger script logic
  68. // every frame (or even every fixed frame).
  69. public override void _Ready()
  70. {
  71. var timer = new Timer();
  72. timer.Autostart = true;
  73. timer.WaitTime = 0.5;
  74. AddChild(timer);
  75. timer.Timeout += () => GD.Print("This block runs every 0.5 seconds");
  76. }
  77. }
  78. .. code-tab:: cpp C++
  79. using namespace godot;
  80. class MyNode : public Node {
  81. GDCLASS(MyNode, Node)
  82. public:
  83. // Allows for recurring operations that don't trigger script logic
  84. // every frame (or even every fixed frame).
  85. virtual void _ready() override {
  86. Timer *timer = memnew(Timer);
  87. timer->set_autostart(true);
  88. timer->set_wait_time(0.5);
  89. add_child(timer);
  90. timer->connect("timeout", callable_mp(this, &MyNode::run));
  91. }
  92. void run() {
  93. UtilityFunctions::print("This block runs every 0.5 seconds.");
  94. }
  95. };
  96. Use ``_physics_process()`` when one needs a framerate-independent delta time
  97. between frames. If code needs consistent updates over time, regardless
  98. of how fast or slow time advances, this is the right place.
  99. Recurring kinematic and object transform operations should execute here.
  100. While it is possible, to achieve the best performance, one should avoid
  101. making input checks during these callbacks. ``_process()`` and
  102. ``_physics_process()`` will trigger at every opportunity (they do not "rest" by
  103. default). In contrast, ``*_input()`` callbacks will trigger only on frames in
  104. which the engine has actually detected the input.
  105. One can check for input actions within the input callbacks just the same.
  106. If one wants to use delta time, one can fetch it from the related
  107. delta time methods as needed.
  108. .. tabs::
  109. .. code-tab:: gdscript GDScript
  110. # Called every frame, even when the engine detects no input.
  111. func _process(delta):
  112. if Input.is_action_just_pressed("ui_select"):
  113. print(delta)
  114. # Called during every input event.
  115. func _unhandled_input(event):
  116. match event.get_class():
  117. "InputEventKey":
  118. if Input.is_action_just_pressed("ui_accept"):
  119. print(get_process_delta_time())
  120. .. code-tab:: csharp
  121. using Godot;
  122. public partial class MyNode : Node
  123. {
  124. // Called every frame, even when the engine detects no input.
  125. public void _Process(double delta)
  126. {
  127. if (Input.IsActionJustPressed("ui_select"))
  128. GD.Print(delta);
  129. }
  130. // Called during every input event. Equally true for _input().
  131. public void _UnhandledInput(InputEvent @event)
  132. {
  133. switch (@event)
  134. {
  135. case InputEventKey:
  136. if (Input.IsActionJustPressed("ui_accept"))
  137. GD.Print(GetProcessDeltaTime());
  138. break;
  139. }
  140. }
  141. }
  142. .. code-tab:: cpp C++
  143. using namespace godot;
  144. class MyNode : public Node {
  145. GDCLASS(MyNode, Node)
  146. public:
  147. // Called every frame, even when the engine detects no input.
  148. virtual void _process(double p_delta) override {
  149. if (Input::get_singleton->is_action_just_pressed("ui_select")) {
  150. UtilityFunctions::print(p_delta);
  151. }
  152. }
  153. // Called during every input event. Equally true for _input().
  154. virtual void _unhandled_input(const Ref<InputEvent> &p_event) override {
  155. Ref<InputEventKey> key_event = event;
  156. if (key_event.is_valid() && Input::get_singleton->is_action_just_pressed("ui_accept")) {
  157. UtilityFunctions::print(get_process_delta_time());
  158. }
  159. }
  160. };
  161. _init vs. initialization vs. export
  162. -----------------------------------
  163. If the script initializes its own node subtree, without a scene,
  164. that code should execute in ``_init()``. Other property or SceneTree-independent
  165. initializations should also run here.
  166. .. note::
  167. The C# equivalent to GDScript's ``_init()`` method is the constructor.
  168. ``_init()`` triggers before ``_enter_tree()`` or ``_ready()``, but after a script
  169. creates and initializes its properties. When instantiating a scene, property
  170. values will set up according to the following sequence:
  171. 1. **Initial value assignment:** the property is assigned its initialization value,
  172. or its default value if one is not specified. If a setter exists, it is not used.
  173. 2. **``_init()`` assignment:** the property's value is replaced by any assignments
  174. made in ``_init()``, triggering the setter.
  175. 3. **Exported value assignment:** an exported property's value is again replaced by
  176. any value set in the Inspector, triggering the setter.
  177. .. tabs::
  178. .. code-tab:: gdscript GDScript
  179. # test is initialized to "one", without triggering the setter.
  180. @export var test: String = "one":
  181. set(value):
  182. test = value + "!"
  183. func _init():
  184. # Triggers the setter, changing test's value from "one" to "two!".
  185. test = "two"
  186. # If someone sets test to "three" from the Inspector, it would trigger
  187. # the setter, changing test's value from "two!" to "three!".
  188. .. code-tab:: csharp
  189. using Godot;
  190. public partial class MyNode : Node
  191. {
  192. private string _test = "one";
  193. [Export]
  194. public string Test
  195. {
  196. get { return _test; }
  197. set { _test = $"{value}!"; }
  198. }
  199. public MyNode()
  200. {
  201. // Triggers the setter, changing _test's value from "one" to "two!".
  202. Test = "two";
  203. }
  204. // If someone sets Test to "three" in the Inspector, it would trigger
  205. // the setter, changing _test's value from "two!" to "three!".
  206. }
  207. .. code-tab:: cpp C++
  208. using namespace godot;
  209. class MyNode : public Node {
  210. GDCLASS(MyNode, Node)
  211. String test = "one";
  212. protected:
  213. static void _bind_methods() {
  214. ClassDB::bind_method(D_METHOD("get_test"), &MyNode::get_test);
  215. ClassDB::bind_method(D_METHOD("set_test", "test"), &MyNode::set_test);
  216. ADD_PROPERTY(PropertyInfo(Variant::STRING, "test"), "set_test", "get_test");
  217. }
  218. public:
  219. String get_test() { return test; }
  220. void set_test(String p_test) { return test = p_test; }
  221. MyNode() {
  222. // Triggers the setter, changing _test's value from "one" to "two!".
  223. set_test("two");
  224. }
  225. // If someone sets test to "three" in the Inspector, it would trigger
  226. // the setter, changing test's value from "two!" to "three!".
  227. };
  228. As a result, instantiating a script versus a scene may affect both the
  229. initialization *and* the number of times the engine calls the setter.
  230. _ready vs. _enter_tree vs. NOTIFICATION_PARENTED
  231. ------------------------------------------------
  232. When instantiating a scene connected to the first executed scene, Godot will
  233. instantiate nodes down the tree (making ``_init()`` calls) and build the tree
  234. going downwards from the root. This causes ``_enter_tree()`` calls to cascade
  235. down the tree. Once the tree is complete, leaf nodes call ``_ready``. A node
  236. will call this method once all child nodes have finished calling theirs. This
  237. then causes a reverse cascade going up back to the tree's root.
  238. When instantiating a script or a standalone scene, nodes are not
  239. added to the SceneTree upon creation, so no ``_enter_tree()`` callbacks
  240. trigger. Instead, only the ``_init()`` call occurs. When the scene is added
  241. to the SceneTree, the ``_enter_tree()`` and ``_ready()`` calls occur.
  242. If one needs to trigger behavior that occurs as nodes parent to another,
  243. regardless of whether it occurs as part of the main/active scene or not, one
  244. can use the :ref:`PARENTED <class_Node_constant_NOTIFICATION_PARENTED>` notification.
  245. For example, here is a snippet that connects a node's method to
  246. a custom signal on the parent node without failing. Useful on data-centric
  247. nodes that one might create at runtime.
  248. .. tabs::
  249. .. code-tab:: gdscript GDScript
  250. extends Node
  251. var parent_cache
  252. func connection_check():
  253. return parent_cache.has_user_signal("interacted_with")
  254. func _notification(what):
  255. match what:
  256. NOTIFICATION_PARENTED:
  257. parent_cache = get_parent()
  258. if connection_check():
  259. parent_cache.interacted_with.connect(_on_parent_interacted_with)
  260. NOTIFICATION_UNPARENTED:
  261. if connection_check():
  262. parent_cache.interacted_with.disconnect(_on_parent_interacted_with)
  263. func _on_parent_interacted_with():
  264. print("I'm reacting to my parent's interaction!")
  265. .. code-tab:: csharp
  266. using Godot;
  267. public partial class MyNode : Node
  268. {
  269. private Node _parentCache;
  270. public bool ConnectionCheck()
  271. {
  272. return _parentCache.HasUserSignal("InteractedWith");
  273. }
  274. public void _Notification(int what)
  275. {
  276. switch (what)
  277. {
  278. case NotificationParented:
  279. _parentCache = GetParent();
  280. if (ConnectionCheck())
  281. {
  282. _parentCache.Connect("InteractedWith", Callable.From(OnParentInteractedWith));
  283. }
  284. break;
  285. case NotificationUnparented:
  286. if (ConnectionCheck())
  287. {
  288. _parentCache.Disconnect("InteractedWith", Callable.From(OnParentInteractedWith));
  289. }
  290. break;
  291. }
  292. }
  293. private void OnParentInteractedWith()
  294. {
  295. GD.Print("I'm reacting to my parent's interaction!");
  296. }
  297. }
  298. .. code-tab:: cpp C++
  299. using namespace godot;
  300. class MyNode : public Node {
  301. GDCLASS(MyNode, Node)
  302. Node *parent_cache = nullptr;
  303. void on_parent_interacted_with() {
  304. UtilityFunctions::print("I'm reacting to my parent's interaction!");
  305. }
  306. public:
  307. void connection_check() {
  308. return parent_cache->has_user_signal("interacted_with");
  309. }
  310. void _notification(int p_what) {
  311. switch (p_what) {
  312. case NOTIFICATION_PARENTED:
  313. parent_cache = get_parent();
  314. if (connection_check()) {
  315. parent_cache->connect("interacted_with", callable_mp(this, &MyNode::on_parent_interacted_with));
  316. }
  317. break;
  318. case NOTIFICATION_UNPARENTED:
  319. if (connection_check()) {
  320. parent_cache->disconnect("interacted_with", callable_mp(this, &MyNode::on_parent_interacted_with));
  321. }
  322. break;
  323. }
  324. }
  325. };