可覆盖函数

Godot 的 Node 类提供了虚函数,你可以通过覆盖虚函数来在每帧或发生特定事件时更新节点,比如进入场景树时。

本文档中会展示你会经常用到的那些。

参见

这些函数在引擎内部会依赖 Godot 的底层通知系统。要了解相关学习,请参阅 Godot 通知

Two functions allow you to initialize and get nodes besides the class’s constructor: _enter_tree() and _ready().

节点进入场景树时就会被激活,引擎会调用其 _enter_tree() 方法。该节点的子项可能还不是活动场景的一部分。因为你可以从场景树中移除节点然后重新添加,在一个节点的生命期中,这个函数可能会被调用多次。

Most of the time, you’ll use _ready() instead. This function is called only once in a node’s lifetime, after _enter_tree(). _ready() ensures that all children have entered the scene tree first, so you can safely call get_node() on them.

参见

要学习更多关于节点引用的知识,请阅读 节点与场景实例

Another related callback is _exit_tree(), which the engine calls every time a node is about to exit the scene tree. This can be when you call Node.remove_child() or when you free a node.

GDScriptC#

  1. # Called every time the node enters the scene tree.
  2. func _enter_tree():
  3. pass
  4. # Called when both the node and its children have entered the scene tree.
  5. func _ready():
  6. pass
  7. # Called when the node is about to leave the scene tree, after all its
  8. # children received the _exit_tree() callback.
  9. func _exit_tree():
  10. pass
  1. // Called every time the node enters the scene tree.
  2. public override void _EnterTree()
  3. {
  4. base._EnterTree();
  5. }
  6. // Called when both the node and its children have entered the scene tree.
  7. public override void _Ready()
  8. {
  9. base._Ready();
  10. }
  11. // Called when the node is about to leave the scene tree, after all its
  12. // children.
  13. public override void _ExitTree()
  14. {
  15. base._ExitTree();
  16. }

虚函数 _process()_physics_process() 可以分别用来对节点进行每帧和每物理帧的更新。要了解更多信息,请阅读专门的文档:空闲处理与物理处理

GDScriptC#

  1. # Called every frame.
  2. func _process(delta):
  3. pass
  4. # Called every physics frame.
  5. func _physics_process(delta):
  6. pass
  1. public override void _Process(double delta)
  2. {
  3. // Called every frame.
  4. base._Process(delta);
  5. }
  6. public override void _PhysicsProcess(double delta)
  7. {
  8. // Called every physics frame.
  9. base._PhysicsProcess(delta);
  10. }

Two more essential built-in node callback functions are Node._unhandled_input() and Node._input(), which you use to both receive and process individual input events. The _unhandled_input() method receives every key press, mouse click, etc. that have not been handled already in an _input() callback or in a user interface component. You want to use it for gameplay input in general. The _input() callback allows you to intercept and process input events before _unhandled_input() gets them.

要学习更多关于 Godot 中输入的内容,请参阅输入章节

GDScriptC#

  1. # Called once for every event.
  2. func _unhandled_input(event):
  3. pass
  4. # Called once for every event before _unhandled_input(), allowing you to
  5. # consume some events.
  6. func _input(event):
  7. pass
  1. // Called once for every event.
  2. public override void _UnhandledInput(InputEvent @event)
  3. {
  4. base._UnhandledInput(@event);
  5. }
  6. // Called once for every event before _UnhandledInput(), allowing you to
  7. // consume some events.
  8. public override void _Input(InputEvent @event)
  9. {
  10. base._Input(@event);
  11. }

There are some more overridable functions like Node._get_configuration_warnings(). Specialized node types provide more callbacks like CanvasItem._draw() to draw programmatically or Control._gui_input() to handle clicks and input on UI elements.

Previous Next


© 版权所有 2014-present Juan Linietsky, Ariel Manzur and the Godot community (CC BY 3.0). Revision b1c660f7.

Built with Sphinx using a theme provided by Read the Docs.