Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Уведомления Godot
Каждый Object в Godot реализует метод _notification. Его цель - позволить объекту реагировать на различные обратные вызовы на уровне движка, которые могут иметь к нему отношение. Например, если движок сообщает CanvasItem "draw" (рисовать), то он вызовет _notification(NOTIFICATION_DRAW).
Некоторые из этих уведомлений, например рисование (draw), полезно переопределить в скриптах. Настолько, что Godot предоставляет многие из них специальные функции:
_ready():NOTIFICATION_READY_enter_tree():NOTIFICATION_ENTER_TREE_exit_tree():NOTIFICATION_EXIT_TREE_process(delta):NOTIFICATION_PROCESS_physics_process(delta):NOTIFICATION_PHYSICS_PROCESS_draw():NOTIFICATION_DRAW
Что пользователи могут не осознавать, так это то, что уведомления существуют и для других типов, не только для Node, например:
Object::NOTIFICATION_POSTINITIALIZE: обратный вызов, который срабатывает при инициализации объекта. Не доступен для скриптов.
Object::NOTIFICATION_PREDELETE: обратный вызов, который срабатывает до того, как движок удалит Object, т.е. 'деструктор'.
И многие обратные вызовы, которые действительно существуют в Node (узлах), не имеют специальных методов, но по-прежнему весьма полезны.
Node::NOTIFICATION_PARENTED: a callback that triggers anytime you add a child node to another node.
Node::NOTIFICATION_UNPARENTED: a callback that triggers anytime you remove a child node from another node.
The universal _notification() method provides access to all these custom
notifications.
Примечание
Методы в документации, помеченные как "виртуальные" (virtual), также должны быть переопределены скриптами.
Классическим примером является метод _init в Object. Хотя у него нет эквивалента NOTIFICATION_*, движок по-прежнему вызывает метод. Большинство языков (кроме C#) полагаются на него, как на конструктор.
So, when should you use each of these notifications or virtual functions?
_process против _physics_process против *_input
Use _process() when you need a framerate-dependent delta time 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 how often
the evaluations need to update. If they don't need to execute every frame, then
implementing a Timer-timeout loop is another option.
# Allows for recurring operations that don't trigger script logic
# every frame (or even every fixed frame).
func _ready():
var timer = Timer.new()
timer.autostart = true
timer.wait_time = 0.5
add_child(timer)
timer.timeout.connect(func():
print("This block runs every 0.5 seconds")
)
using Godot;
public partial class MyNode : Node
{
// Allows for recurring operations that don't trigger script logic
// every frame (or even every fixed frame).
public override void _Ready()
{
var timer = new Timer();
timer.Autostart = true;
timer.WaitTime = 0.5;
AddChild(timer);
timer.Timeout += () => GD.Print("This block runs every 0.5 seconds");
}
}
using namespace godot;
class MyNode : public Node {
GDCLASS(MyNode, Node)
public:
// Allows for recurring operations that don't trigger script logic
// every frame (or even every fixed frame).
virtual void _ready() override {
Timer *timer = memnew(Timer);
timer->set_autostart(true);
timer->set_wait_time(0.5);
add_child(timer);
timer->connect("timeout", callable_mp(this, &MyNode::run));
}
void run() {
UtilityFunctions::print("This block runs every 0.5 seconds.");
}
};
Use _physics_process() when you need a framerate-independent delta time
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, you 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.
You can check for input actions within the input callbacks just the same. If you want to use delta time, you can fetch it from the related delta time methods as needed.
# Called every frame, even when the engine detects no input.
func _process(delta):
if Input.is_action_just_pressed("ui_select"):
print(delta)
# Called during every input event.
func _unhandled_input(event):
match event.get_class():
"InputEventKey":
if Input.is_action_just_pressed("ui_accept"):
print(get_process_delta_time())
using Godot;
public partial class MyNode : Node
{
// Called every frame, even when the engine detects no input.
public void _Process(double delta)
{
if (Input.IsActionJustPressed("ui_select"))
{
GD.Print(delta);
}
}
// Called during every input event. Equally true for _input().
public void _UnhandledInput(InputEvent @event)
{
switch (@event)
{
case InputEventKey:
if (Input.IsActionJustPressed("ui_accept"))
{
GD.Print(GetProcessDeltaTime());
}
break;
}
}
}
using namespace godot;
class MyNode : public Node {
GDCLASS(MyNode, Node)
public:
// Called every frame, even when the engine detects no input.
virtual void _process(double p_delta) override {
if (Input::get_singleton->is_action_just_pressed("ui_select")) {
UtilityFunctions::print(p_delta);
}
}
// Called during every input event. Equally true for _input().
virtual void _unhandled_input(const Ref<InputEvent> &p_event) override {
Ref<InputEventKey> key_event = event;
if (key_event.is_valid() && Input::get_singleton->is_action_just_pressed("ui_accept")) {
UtilityFunctions::print(get_process_delta_time());
}
}
};
_init против initialization против export
Если скрипт инициализирует своё собственное поддерево узлов, без сцены, то этот код должен выполняться _init(). Другие свойства или независимые от SceneTree инициализации должны также запускаться в _init().
Примечание
Конструктор в C#, является эквивалентом метода GDScript _init().
_init() срабатывает до _enter_tree() или _ready(), но после скрипт создает и инициализирует его свойства. При инстанцировании сцены значения свойств будут установлены в соответствии со следующей последовательностью:
Присвоение начального значения: при создании экземпляра будет назначено либо значение инициализации, либо значение по умолчанию, если не указано другое. Если сеттер существует, то он не будет использован.
_init()assignment (назначение): значение свойства заменяется любыми назначениями, сделанными в_init(), что запускает сеттер.Присвоение экспортируемого значения: значение экспортируемого свойства снова заменяется любым значением, установленным в инспекторе, вызывая сеттер.
# test is initialized to "one", without triggering the setter.
@export var test: String = "one":
set(value):
test = value + "!"
func _init():
# Triggers the setter, changing test's value from "one" to "two!".
test = "two"
# If you set test to "three" from the Inspector, it would trigger
# the setter, changing test's value from "two!" to "three!".
using Godot;
public partial class MyNode : Node
{
private string _test = "one";
[Export]
public string Test
{
get { return _test; }
set { _test = $"{value}!"; }
}
public MyNode()
{
// Triggers the setter, changing _test's value from "one" to "two!".
Test = "two";
}
// If you set Test to "three" in the Inspector, it would trigger
// the setter, changing _test's value from "two!" to "three!".
}
using namespace godot;
class MyNode : public Node {
GDCLASS(MyNode, Node)
String test = "one";
protected:
static void _bind_methods() {
ClassDB::bind_method(D_METHOD("get_test"), &MyNode::get_test);
ClassDB::bind_method(D_METHOD("set_test", "test"), &MyNode::set_test);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "test"), "set_test", "get_test");
}
public:
String get_test() { return test; }
void set_test(String p_test) { test = p_test + "!"; }
MyNode() {
// Triggers the setter, changing _test's value from "one" to "two!".
set_test("two");
}
// If you set test to "three" in the Inspector, it would trigger
// the setter, changing test's value from "two!" to "three!".
};
В результате создание экземпляра скрипта вместо сцены повлияет как на инициализацию, так и на количество вызовов сеттера движком.
_ready против _enter_tree против NOTIFICATION_PARENTED
При создании экземпляра сцены, связанной с первой выполненной сценой, Godot будет создавать экземпляры узлов вниз по дереву (выполняя вызовы _init) и строить дерево, идущее вниз от корня. Это вызывает каскадирование вызовов _enter_tree по дереву. Когда дерево готово, листовые узлы вызывают _ready. Узел вызовет этот метод, как только все дочерние узлы закончат свои вызовы. Затем это вызывает обратный каскад, возвращающийся к корню дерева.
При создании скрипта или автономной сцены узлы не добавляются в SceneTree при создании, поэтому обратные вызовы _enter_tree не запускаются. Вместо этого выполняются только вызовы _init. При добавлении сцены в SceneTree происходит вызов _enter_tree() и _enter_tree().
If you need to trigger behavior that occurs as nodes parent to another, regardless of whether it occurs as part of the main/active scene or not, you 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 potentially created at runtime.
extends Node
var parent_cache
func connection_check():
return parent_cache.has_user_signal("interacted_with")
func _notification(what):
match what:
NOTIFICATION_PARENTED:
parent_cache = get_parent()
if connection_check():
parent_cache.interacted_with.connect(_on_parent_interacted_with)
NOTIFICATION_UNPARENTED:
if connection_check():
parent_cache.interacted_with.disconnect(_on_parent_interacted_with)
func _on_parent_interacted_with():
print("I'm reacting to my parent's interaction!")
using Godot;
public partial class MyNode : Node
{
private Node _parentCache;
public bool ConnectionCheck()
{
return _parentCache.HasUserSignal("InteractedWith");
}
public override void _Notification(int what)
{
switch ((long)what)
{
case NotificationParented:
_parentCache = GetParent();
if (ConnectionCheck())
{
_parentCache.Connect("InteractedWith", Callable.From(OnParentInteractedWith));
}
break;
case NotificationUnparented:
if (ConnectionCheck())
{
_parentCache.Disconnect("InteractedWith", Callable.From(OnParentInteractedWith));
}
break;
}
}
private void OnParentInteractedWith()
{
GD.Print("I'm reacting to my parent's interaction!");
}
}
using namespace godot;
class MyNode : public Node {
GDCLASS(MyNode, Node)
Node *parent_cache = nullptr;
void on_parent_interacted_with() {
UtilityFunctions::print("I'm reacting to my parent's interaction!");
}
public:
void connection_check() {
return parent_cache->has_user_signal("interacted_with");
}
void _notification(int p_what) {
switch (p_what) {
case NOTIFICATION_PARENTED:
parent_cache = get_parent();
if (connection_check()) {
parent_cache->connect("interacted_with", callable_mp(this, &MyNode::on_parent_interacted_with));
}
break;
case NOTIFICATION_UNPARENTED:
if (connection_check()) {
parent_cache->disconnect("interacted_with", callable_mp(this, &MyNode::on_parent_interacted_with));
}
break;
}
}
};