Nodes
Table of Contents
- Overrideable functions (
_ready(),_process(), …) - Create and delete a node
Node.get_node()Node.add_to_group()- Where should I check for user input?
_process()or_physics_process()? - Which class should I use for script-only nodes?
Node.get_viewport_rect()
Overrideable functions (_ready(), _process(), …) discussioin
func _enter_tree():
# When the node enters the _Scene Tree_, it becomes active
# and this function is called. Children nodes have not entered
# the active scene yet. In general, it's better to use _ready()
# for most cases.
pass
func _ready():
# This function is called after _enter_tree, but it ensures
# that all children nodes have also entered the _Scene Tree_,
# and became active.
pass
func _exit_tree():
# When the node exits the _Scene Tree_, this function is called.
# Children nodes have all exited the _Scene Tree_ at this point
# and all became inactive.
pass
func _process(delta):
# This function is called every frame.
pass
func _physics_process(delta):
# This is called every physics frame.
pass- Adding nodes to
SceneTree - Child nodes are always added after their parent node.
_enter_tree()is called before adding child nodes- a left node is added,
_enter_tree()will be called. And then_ready()call follows. - Once all child nodes' finish calling their
_ready(), the non-left nodes'_ready()will be called. - Once all nodes have been added to the
SceneTree,
- Processing
_processing(),_physics_process()are called every frame. Same as other engines._input()is called only there are some input events.- Use
_unhandled_input()to catch input events which are not processed by other components, like buttons.
Create and delete a node howto
var s
func _ready():
s = Sprite.new() # Create a new sprite!
add_child(s) # Add it as a child of this node.
func _someaction():
s.free() # Immediately removes the node from the scene and frees it.
func _someaction():
s.queue_free() # Queues the Node for deletion at the end of the current Frame.Node.get_node() reference
Node get_node ( NodePath path ) const
pathcan be either relative or absolute(in the scene tree).null instanceif the path does not exist.
/root
/root/Character
/root/Character/Sword
/root/Character/Backpack/Dagger
/root/MyGame
/root/Swamp/Alligator
/root/Swamp/Mosquito
/root/Swamp/Goblin
If the current node is Character, following calls are valid:
get_node("Sword")
get_node("Backpack/Dagger")
get_node("../Swamp/Alligator")
get_node("/root/MyGame")
Since get_node() is really frequently used, it has a shorthand $. $<nodepath> returns the node at the relative path from this node, or returns null if the node is not found. For example, $AnimatedSprite, or double-quoted, like $"/root/MyGame".
Node.add_to_group() reference
- It's useful to organize nodes.
- A node can be in any number of groups.
func _on_discovered(): # This is a purely illustrative function.
get_tree().call_group("enemies", "player_was_discovered")Where should I check for user input? _process() or _physics_process()? discussion
- Generally, use
_process(). - If your input is directly altering a
RigidBody, use_physics_process()
Which class should I use for script-only nodes? discussion
It is not explicitly mentioned, but most of tutorials use just Node class for script-only nodes, regardless of 2D or 3D.
Node.get_viewport_rect() reference
Rect2 get_viewport_rect ( ) const
Get the viewport’s boundaries as a Rect2.