Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Funciones de auxiliares

La clase Node de Godot proporciona funciones virtuales que puedes sobrescribir para actualizar nodos en cada fotograma o en eventos específicos, como cuando entran en el árbol de escena.

Este documento presenta las funciones que utilizarás con mayor frecuencia.

Ver también

En el fondo, estas funciones se basan en el sistema de notificaciones de bajo nivel de Godot. Para obtener más información al respecto, consulta Notificaciones en Godot.

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

Cuando el nodo entra en el árbol de escena, se vuelve activo y el motor llama a su método _enter_tree(). Los hijos de ese nodo pueden no ser parte de la escena activa aún. Dado que puedes quitar y volver a agregar nodos al árbol de escena, esta función puede ser llamada varias veces a lo largo de la vida de un nodo.

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.

Ver también

Para obtener más información sobre cómo obtener referencias de nodos, lee Nodos y instancias de escenas.

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

Los dos métodos virtuales _process() y _physics_process() te permiten actualizar el nodo, cada cuadro y cada cuadro de físicas, respectivamente. Para obtener más información, lee la documentación dedicada: Procesamiento en Reposo y Físico.

# Called every frame.
func _process(delta):
    pass

# Called every physics frame.
func _physics_process(delta):
    pass

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.

Para obtener más información sobre las entradas (inputs) en Godot, consulta la sección Entrada en la documentación.

# 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

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.