overridable_functions.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. .. _doc_overridable_functions:
  2. Overridable functions
  3. =====================
  4. Godot's Node class provides virtual functions you can override to update nodes
  5. every frame or on specific events, like when they enter the scene tree.
  6. This document presents the ones you'll use most often.
  7. .. seealso:: Under the hood, these functions rely on Godot's low-level
  8. notifications system. To learn more about it, see
  9. :ref:`doc_godot_notifications`.
  10. Two functions allow you to initialize and get nodes, besides the class's
  11. constructor: ``_enter_tree()`` and ``_ready()``.
  12. When the node enters the Scene Tree, it becomes active and the engine calls its
  13. ``_enter_tree()`` method. That node's children may not be part of the active scene yet. As
  14. you can remove and re-add nodes to the scene tree, this function may be called
  15. multiple times throughout a node's lifetime.
  16. Most of the time, you'll use ``_ready()`` instead. This function is called only
  17. once in a node's lifetime, after ``_enter_tree()``. ``_ready()`` ensures that all children
  18. have entered the scene tree first, so you can safely call ``get_node()`` on it.
  19. .. seealso:: To learn more about getting node references, read
  20. :ref:`doc_nodes_and_scene_instances`.
  21. Another related callback is ``_exit_tree()``, which the engine calls every time
  22. a node exits the scene tree. This can be when you call :ref:`Node.remove_child()
  23. <class_Node_method_remove_child>` or when you free a node.
  24. .. tabs::
  25. .. code-tab:: gdscript GDScript
  26. # Called every time the node enters the scene tree.
  27. func _enter_tree():
  28. pass
  29. # Called when both the node and its children have entered the scene tree.
  30. func _ready():
  31. pass
  32. # Called when the node is about to leave the scene tree, after all its
  33. # children received the _exit_tree() callback.
  34. func _exit_tree():
  35. pass
  36. .. code-tab:: csharp
  37. // Called every time the node enters the scene tree.
  38. public override void _EnterTree()
  39. {
  40. base._EnterTree();
  41. }
  42. // Called when both the node and its children have entered the scene tree.
  43. public override void _Ready()
  44. {
  45. base._Ready();
  46. }
  47. // Called when the node is about to leave the scene tree, after all its
  48. // children.
  49. public override void _ExitTree()
  50. {
  51. base._ExitTree();
  52. }
  53. The two virtual methods ``_process()`` and ``_physics_process()`` allow you to
  54. update the node, every frame and every physics frame respectively. For more
  55. information, read the dedicated documentation:
  56. :ref:`doc_idle_and_physics_processing`.
  57. .. tabs::
  58. .. code-tab:: gdscript GDScript
  59. # Called every frame, as often as possible.
  60. func _process(delta):
  61. pass
  62. # Called every physics frame.
  63. func _physics_process(delta):
  64. pass
  65. .. code-tab:: csharp
  66. public override void _Process(float delta)
  67. {
  68. // Called every frame, as often as possible.
  69. base._Process(delta);
  70. }
  71. public override void _PhysicsProcess(float delta)
  72. {
  73. // Called every physics frame.
  74. base._PhysicsProcess(delta);
  75. }
  76. Two more essential built-in node callback functions are
  77. :ref:`Node._unhandled_input() <class_Node_method__unhandled_input>` and
  78. :ref:`Node._input() <class_Node_method__input>`, which you use to both receive
  79. and process individual input events. The ``_unhandled_input()`` method receives
  80. every key press, mouse click, etc. that have not been handled already in an
  81. ``_input()`` callback or in a user interface component. You want to use it for
  82. gameplay input in general. The ``_input()`` callback allows you to intercept and
  83. process input events before ``_unhandled_input()`` gets them.
  84. To learn more about inputs in Godot, see the :ref:`Input section <toc-learn-features-inputs>`.
  85. .. tabs::
  86. .. code-tab:: gdscript GDScript
  87. # Called once for every event.
  88. func _unhandled_input(event):
  89. pass
  90. # Called once for every event, before _unhandled_input(), allowing you to
  91. # consume some events.
  92. func _input(event):
  93. pass
  94. .. code-tab:: csharp
  95. // Called once for every event.
  96. public override void _UnhandledInput(InputEvent @event)
  97. {
  98. base._UnhandledInput(event);
  99. }
  100. // Called once for every event, before _unhandled_input(), allowing you to
  101. // consume some events.
  102. public override void _Input(InputEvent @event)
  103. {
  104. base._Input(event);
  105. }
  106. There are some more overridable functions like
  107. :ref:`Node._get_configuration_warning()
  108. <class_Node_method__get_configuration_warning>`. Specialized node types provide
  109. more callbacks like :ref:`CanvasItem._draw() <class_CanvasItem_method__draw>` to
  110. draw programmatically or :ref:`Control._gui_input()
  111. <class_Control_method__gui_input>` to handle clicks and input on UI elements.