Nodes and scene instances
This guide explains how to get nodes, create nodes, add them as a child, and instantiate scenes from code.
Getting nodes
Node.get_node() method. For this to work, the child node must be present in the scene tree. Getting it in the parent node’s _ready() function guarantees that.
If, for example, you have a scene tree like this, and you want to get a reference to the Sprite and Camera2D nodes to access them in your script.
To do so, you can use the following code.
GDScript C#
var spritevar camera2dfunc _ready(): sprite = get_node("Sprite") camera2d = get_node("Camera2D")
private Sprite _sprite;private Camera2D _camera2d;public override void _Ready(){ base._Ready(); _sprite = GetNode<Sprite>("Sprite"); _camera2d = GetNode<Camera2D>("Camera2D");}
Note that you get nodes using their name, not their type. Above, “Sprite” and “Camera2D” are the nodes’ names in the scene.
get_node("Skin") in the script.
Node paths
get_node() function supports paths, a bit like when working with a file browser. Add a slash to separate nodes.
Take the following example scene, with the script attached to the UserInterface node.
To get the Tween node, you would use the following code.
GDScript C#
var tweenfunc _ready(): tween = get_node("ShieldBar/Tween")
private Tween _tween;public override void _Ready(){ base._Ready(); _tween = GetNode<Tween>("ShieldBar/Tween");}
Note As with file paths, you can use “..” to get a parent node. The best practice is to avoid doing that though not to break encapsulation. You can also start the path with a forward slash to make it absolute, in which case your topmost node would be “/root”, the application’s predefined root viewport.
Syntactic sugar
onready keyword before a member variable makes it initialize right before the _ready() callback.
onready var sprite = get_node("Sprite")
get_node(): the dollar sign, “$”. You place it before the name or path of the node you want to get.
onready var sprite = $Spriteonready var tween = $ShieldBar/Tween
Creating nodes
new() method like for any other class-based datatype.
add_child() to add it as a child of the node to which you attached the script.
GDScript C#
var spritefunc _ready(): var sprite = Sprite.new() # Create a new Sprite. add_child(sprite) # Add it as a child of this node.
private Sprite _sprite;public override void _Ready(){ base._Ready(); _sprite = new Sprite(); // Create a new Sprite. AddChild(_sprite); // Add it as a child of this node.}
queue_free() method. Doing so queues the node for deletion at the end of the current frame after it has finished processing. At that point, the engine removes the node from the scene and frees the object in memory.
GDScript C#
sprite.queue_free()
_sprite.QueueFree();
sprite.queue_free(), the remote scene tree looks like this.
After the engine freed the node, the remote scene tree doesn’t display the sprite anymore.
free() to immediately destroy the node. You should do this with care as any reference to it will instantly become null. We recommend using queue_free() unless you know what you’re doing.
When you free a node, it also frees all its children. Thanks to this, to delete an entire branch of the scene tree, you only have to free the topmost parent node.
Instancing scenes
Scenes are templates from which you can create as many reproductions as you’d like. This operation is called instancing, and doing it from code happens in two steps:
- Loading the scene from the hard drive.
- PackedScene resource.
GDScript C#
var scene = load("res://MyScene.tscn")
Preloading the scene can improve the user’s experience as the load operation happens when the compiler reads the script and not at runtime. This feature is only available with GDScript. GDScriptvar scene = GD.Load<PackedScene>("res://MyScene.tscn");
var scene = preload("res://MyScene.tscn")
sceneis a packed scene resource, not a node. To create the actual node, you need to call PackedScene.instance(). It returns a tree of nodes that you can as a child of your current node. GDScript C#var instance = scene.instance()add_child(instance)
The advantage of this two-step process is you can keep a packed scene loaded and create new instances on the fly. For example, to quickly instance several enemies or bullets.var instance = scene.Instance();AddChild(instance);
