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.

알림(Notifications)

Godot의 모든 객체는 _notification 메소드를 구현합니다. 그 목적은 개체가 관련될 수 있는 다양한 엔진 수준 콜백에 응답할 수 있도록 하는 것입니다. 예를 들어, 엔진이 :ref:`CanvasItem <class_CanvasItem>`에 "그리기"를 지시하면 ``_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

사용자가 깨닫지 못할 수 있는 것은 노드 이외의 유형에 대한 알림이 있다는 것입니다. 예를 들면 다음과 같습니다.

그리고 노드에 존재하는 많은 콜백에는 전용 메서드가 없지만 여전히 매우 유용합니다.

The universal _notification() method provides access to all these custom notifications.

참고

"가상"으로 표시된 문서의 메소드도 스크립트에 의해 재정의되도록 의도되었습니다.

전형적인 예는 Object의 _init 메서드입니다. 해당하는 ``NOTIFICATION_*``가 없지만 엔진은 여전히 메서드를 호출합니다. C#을 제외한 대부분의 언어는 이를 생성자로 사용합니다.

So, when should you use each of these notifications or virtual functions?

_프로세스 대 _물리_프로세스 대 *_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")
    )

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())

_init vs. 초기화 vs. 내보내기

스크립트가 씬 없이 자체 노드 하위 트리를 초기화하는 경우 해당 코드는 ``_init()``에서 실행되어야 합니다. 다른 속성이나 SceneTree 독립적 초기화도 여기에서 실행되어야 합니다.

참고

GDScript의 _init() 메서드에 해당하는 C#은 생성자입니다.

_init()``는 ``_enter_tree() 또는 _ready() 이전에 트리거되지만 스크립트가 해당 속성을 생성하고 초기화한 후에 트리거됩니다. 씬을 인스턴스화할 때 속성 값은 다음 순서에 따라 설정됩니다.

  1. 초기 값 할당: 속성에 초기화 값이 할당되거나, 지정되지 않은 경우 기본값이 할당됩니다. setter가 존재하는 경우에는 사용되지 않습니다.

  2. _init() 할당: 속성 값은 ``_init()``에서 수행된 할당으로 대체되어 setter를 트리거합니다.

  3. 내보낸 값 할당: 내보낸 속성의 값은 인스펙터에 설정된 값으로 다시 대체되어 setter를 트리거합니다.

# 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!".

결과적으로 스크립트와 씬을 인스턴스화하면 초기화 엔진이 setter를 호출하는 횟수 모두에 영향을 미칠 수 있습니다.

_ready 대 _enter_tree 대 NOTIFICATION_PARENTED

처음 실행된 씬에 연결된 씬을 인스턴스화할 때, Godot는 트리 아래로 노드를 인스턴스화하고(_init() 호출 만들기) 루트에서 아래쪽으로 트리를 구축합니다. 이로 인해 _enter_tree() 호출이 트리 아래로 계단식으로 내려갑니다. 트리가 완성되면 리프 노드는 ``_ready``를 호출합니다. 모든 자식 노드가 호출을 완료하면 노드는 이 메서드를 호출합니다. 그러면 트리의 루트로 다시 올라가는 역방향 계단식 현상이 발생합니다.

스크립트 또는 독립형 씬을 인스턴스화하는 경우 노드는 생성 시 SceneTree에 추가되지 않으므로 _enter_tree() 콜백이 트리거되지 않습니다. 대신 _init() 호출만 발생합니다. 씬이 SceneTree에 추가되면 _enter_tree()_ready() 호출이 발생합니다.

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!")