Node Referencing
Via script
-
There are several ways to access a Node's path, such as:
var child_node = $ChildNode
var child_node = get_node("ChildNode")
var grandchild_node = $ChildNode/GrandchildNode
var grandchild_node = get_node("ChildNode/GrandchildNode")
var parent_node = get_parent()
var parent_node = get_node("..")
var grandparent_node = get_parent().get_parent()
var grandparent_node = get_node("../..")
var sibling_node = get_parent().get_node("SiblingNode")
var sibling_node = get_node("../SiblingNode")
Name Unique
-
It's possible to access the Node without a direct path using the 'Access as Scene Unique Name' feature. By right-clicking the Node and enabling 'Access as Scene Unique Name', the Node can be accessed from anywhere in the SceneTree using:
-
get_node("%_node_name")
-
Node Groups
-
Use groups to transmit signals and data without making Connections. Information is available in the 'Node' and 'SceneTree' documentation.
-
Add a Node to a Group:
add_to_group("_group_name_") -
Remove a Node from a Group:
remove_from_group("_group_name_") -
Get an array of all Groups the Node belongs to:
get_node(_node_path_).get_groups() -
Check if a Group exists:
bool has_group(name: StringName) -
Check if a Node is in a Group:
get_node(_node_path_).is_in_group("_group_name_") -
Get an array of all Nodes in a Group:
Array[Node] get_nodes_in_group(group: StringName) -
Change the value of a 'variable / property' of all Group members: (there is a variant with 'Flags' as extra inputs)
void set_group(group: StringName, property: String, value: Variant) -
Call a 'method / function' on all Group members: (variant with 'Flags' as extra inputs exists)
void call_group(group: StringName, method: StringName, ...) -
"Notify" all Group members: (variant with 'Flags' as extra inputs exists)
void notify_group(group: StringName, notification: int)
Instantiate
Instantiate a Node from a pre-existing file
-
Creation via
export():
@export var pre_node : NodePath
new_node = pre_node.instantiate()
add_child(new_node)
-
Creation via 'preload' or 'load':
const pre_node = preload("NodePath") |~or~| const node = load("NodePath")
new_node = pre_node.instance()
add_child(new_node)
Reparent
if !get_parent().is_ancestor_of(body):
body.call_deferred("reparent", get_node("../Entities"))
Remove
-
Using
_node_.queue_free()will delete the Node in the next frame, keeping it alive until then. -
Using
_parentNode_.remove_child(_node_)will remove the Node from all SceneTrees in the game, leaving it orphaned but not deleted. You can check if the Node is inside a SceneTree using_Node_.is_inside_tree().
Check Is Valid
-
Check if a Node still exists or has been deleted (
is_instance_valid()) -
The code below may result in strange errors:
@onready var node = $Node
if node:
(...)
-
The code below works better, without errors:
@onready var node = $Node
if is_instance_valid(node):
(...)
Check Node type
-
Using
entity is Node2Dperforms an inheritance-based check. -
Using
entity.is_class("Node2D"):-
Returns true if the object inherits from the given class. See also get_class().
-
var sprite2d = Sprite2D.new() -
sprite2d.is_class("Sprite2D") # Returns true -
sprite2d.is_class("Node") # Returns true -
sprite2d.is_class("Node3D") # Returns false
-
-