Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
可複寫函式¶
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.
# Called every time the node enters the scene tree.
func _enter_tree():
pass
# Called when both the node and its children have entered the scene tree.
func _ready():
pass
# Called when the node is about to leave the scene tree, after all its
# children received the _exit_tree() callback.
func _exit_tree():
pass
// Called every time the node enters the scene tree.
public override void _EnterTree()
{
base._EnterTree();
}
// Called when both the node and its children have entered the scene tree.
public override void _Ready()
{
base._Ready();
}
// Called when the node is about to leave the scene tree, after all its
// children.
public override void _ExitTree()
{
base._ExitTree();
}
虛函式 _process()
和 _physics_process()
可以分別用來對節點進行每影格和每物理影格的更新。要瞭解更多資訊,請閱讀專門的文件:空閒處理與物理處理。
# Called every frame.
func _process(delta):
pass
# Called every physics frame.
func _physics_process(delta):
pass
public override void _Process(double delta)
{
// Called every frame.
base._Process(delta);
}
public override void _PhysicsProcess(double delta)
{
// Called every physics frame.
base._PhysicsProcess(delta);
}
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 中輸入的內容,請參閱:ref:輸入章節 <toc-learn-features-inputs>。
# Called once for every event.
func _unhandled_input(event):
pass
# Called once for every event before _unhandled_input(), allowing you to
# consume some events.
func _input(event):
pass
// Called once for every event.
public override void _UnhandledInput(InputEvent @event)
{
base._UnhandledInput(@event);
}
// Called once for every event before _UnhandledInput(), allowing you to
// consume some events.
public override void _Input(InputEvent @event)
{
base._Input(@event);
}
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.