Nodes

Nodes are the building blocks in Godot.

Refs: Node - Node2D - Node3D

Creating and Removing Nodes

Instancing

A node added to a scene by instancing and adding as a child:

model = slot_data.item_data.model.instantiate()
add_child(model)

Add and remove node from scene tree

A node needs to be linked to the scene tree to be visible and interactable.

add_child(MyNode)

remove_child(MyNode)

Removing from the scene tree doesn't remove it from memory!

A node in the scene tree is an active node. A node that is removed from the scene tree, but still kept in memory is an orphaned node.

Removing node from memory

A node is removed by freeing it:

get_node("NodeName").free()

Deletes the node immediately.

$NodeName.queue_free()

Deletes the node at the end of the frame. This is the recommended way!

Only change the visibility

To hide or show a node simply set its visible property.

self.visible = false
self.visible = true

The node is still processed and continues to act in the game!

To stop processing, you need to set the process mode:

# Disable:
var old_mode = self.process_mode
self.process_mode = ProcessMode.PROCESS_MODE_DISABLED
# Re-enable:
self.process_mode = old_mode

Quick delete

The quick way to delete an object from the scene is:

get_parent().remove_child(self) # first orphan self, making it not part of the scene
queue_free()                    # queue self for memory release.

Getting nodes

# Get the class type as a string, ignores custom "class_name"
var my_node_name: string = node.get_class()
# Check the type with the "is" operator, this uses the class_name
if my_node is MyType:
    print("Yes, it is MyType!")
# Get the custom class name set in the script
var my_class_name: string = node.get_script().get_global_name()

Get child nodes

var children: Array[Node] = node.get_children()