Nodes

Table of Contents

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
  1. Child nodes are always added after their parent node.
  2. _enter_tree() is called before adding child nodes
  3. a left node is added, _enter_tree() will be called. And then _ready() call follows.
  4. Once all child nodes' finish calling their _ready(), the non-left nodes' _ready() will be called.
  5. Once all nodes have been added to the SceneTree,
Processing
  1. _processing(), _physics_process() are called every frame. Same as other engines.
  2. _input() is called only there are some input events.
  3. 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
/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

func _ready():
    add_to_group("enemies")
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

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.