pandemonium_engine_docs/03_usage/15_scripting/04_overridable_functions.md

99 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2023-01-12 20:49:14 +01:00
2024-04-30 20:17:58 +02:00
# Overridable functions
2024-03-16 20:56:52 +01:00
Pandemonium's Node class provides virtual functions you can override to update nodes
every frame or on specific events, like when they enter the scene tree.
This document presents the ones you'll use most often.
2023-01-12 20:55:57 +01:00
See also:
2024-03-16 20:56:52 +01:00
Under the hood, these functions rely on Pandemonium's low-level
notifications system. To learn more about it, see
2024-03-16 20:56:52 +01:00
`doc_pandemonium_notifications`.
Two functions allow you to initialize and get nodes, besides the class's
2023-01-12 20:57:31 +01:00
constructor: `enter_tree()` and `ready()`.
When the node enters the Scene Tree, it becomes active and the engine calls its
2023-01-12 20:57:31 +01:00
`enter_tree()` method. That node's children may not be part of the active scene yet. As
you can remove and re-add nodes to the scene tree, this function may be called
multiple times throughout a node's lifetime.
2023-01-12 20:57:31 +01:00
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
2023-01-12 19:43:03 +01:00
have entered the scene tree first, so you can safely call `get_node()` on it.
2023-01-12 20:55:57 +01:00
See also:
To learn more about getting node references, read
2023-01-12 19:29:11 +01:00
`doc_nodes_and_scene_instances`.
2023-01-12 20:57:31 +01:00
Another related callback is `exit_tree()`, which the engine calls every time
2023-01-12 19:29:11 +01:00
a node exits the scene tree. This can be when you call `Node.remove_child()
2023-01-12 20:47:54 +01:00
( Node_method_remove_child )` or when you free a node.
2023-01-12 18:31:02 +01:00
gdscript GDScript
2023-01-12 18:31:02 +01:00
```
2024-05-04 14:08:00 +02:00
# 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
2023-01-12 18:31:02 +01:00
```
2023-01-12 20:57:31 +01:00
The two virtual methods `process()` and `physics_process()` allow you to
update the node, every frame and every physics frame respectively. For more
information, read the dedicated documentation:
2023-01-12 19:29:11 +01:00
`doc_idle_and_physics_processing`.
2023-01-12 18:31:02 +01:00
gdscript GDScript
2023-01-12 18:31:02 +01:00
```
2024-05-04 14:08:00 +02:00
# Called every frame, as often as possible.
func _process(delta):
pass
2024-05-04 14:08:00 +02:00
# Called every physics frame.
func _physics_process(delta):
pass
2023-01-12 18:31:02 +01:00
```
Two more essential built-in node callback functions are
2023-01-12 19:30:47 +01:00
`Node._unhandled_input()` and
`Node._input()`, which you use to both receive
2023-01-12 20:57:31 +01:00
and process individual input events. The `unhandled_input()` method receives
every key press, mouse click, etc. that have not been handled already in an
2023-01-12 20:57:31 +01:00
`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.
2024-04-30 20:17:58 +02:00
To learn more about inputs in Pandemonium, see the `Input section ( toc-learn-features-inputs )`.
2023-01-12 18:31:02 +01:00
gdscript GDScript
2023-01-12 18:31:02 +01:00
```
2024-05-04 14:08:00 +02:00
# 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
2023-01-12 18:31:02 +01:00
```
There are some more overridable functions like
2023-01-12 19:29:11 +01:00
`Node._get_configuration_warning()
2023-01-12 20:47:54 +01:00
( Node_method__get_configuration_warning )`. Specialized node types provide
2023-01-12 19:30:47 +01:00
more callbacks like `CanvasItem._draw()` to
2023-01-12 19:29:11 +01:00
draw programmatically or `Control._gui_input()
2023-01-12 20:47:54 +01:00
( Control_method__gui_input )` to handle clicks and input on UI elements.