Node
⚠️ Experimental - Pre-Release
Inheritance Hierarchy
Node is the base class for all nodes in Perro. Every node in your scene inherits from Node, providing core functionality like hierarchy management and script attachment.
Create nodes at runtime using new NodeType(). See DynNode for information about type-erased node references.
Methods
get_node(name: string) → DynNode
Searches this node's children for a node with the matching name. Returns a DynNode (any type of node) that can be cast to a more concrete type. Panics if not found.
Note: Since this returns DynNode, you can:
- Use
get_type()to check the type, then cast withas - Directly cast when calling:
self.get_node("child") asNode2D
// Basic usage - returns DynNodevar child = self.get_node("player") // Check type and castif child.get_type() == NodeType.Node2D { var child2d = child as Node2D child2d.transform.position.x = 100} // Direct cast when callingvar sprite = self.get_node("sprite") as Sprite2Dsprite.texture = my_textureget_type() → NodeType
Returns the type of this node as a NodeType enum. Use this to check what type a DynNode is before casting.
var child = self.get_node("sprite")if child.get_type() == NodeType.Sprite2D { var sprite = child as Sprite2D // Use sprite...}get_parent() → DynNode
Returns the parent node of this node as a DynNode. Panics if this node has no parent(i.e., it's a root node).
// Get parent and cast to specific typevar parent = self.get_parent() as Node2DConsole.print("Parent position: " + parent.transform.position.x)add_child(node: DynNode)
Adds a child node (as a DynNode) to this node. The child node becomes part of this node's hierarchy.
var new_sprite = new Sprite2D()self.add_child(new_sprite)remove()
Deletes this node and all of its children from the entire scene. The node and all its descendants are permanently removed and cannot be accessed afterward.
// Delete a child node and all its children from the scenevar child = self.get_node("enemy")child.remove() // Delete self and all children from the sceneself.remove()clear_children()
Deletes all child nodes from this node. All children are permanently removed from the scene they stop rendering and no longer execute logic.
// Delete all children from a nodeself.clear_children() // Useful for resetting a container node@script Container extends Node2D fn reset() { self.clear_children() // Add new children... }Fields
name: string
The name of the node. Used for identification and finding nodes by name.
@script MyNode extends Node fn init() { self.name = "PlayerNode" Console.print("Node name: " + self.name) }script_path: string?
Optional path to the script file attached to this node. null if no script is attached.
@script MyNode extends Node fn init() { if self.script_path != null { Console.print("Script: " + self.script_path) } }Using `self`
In Pup scripts, use self to refer to the current node instance. All methods and fields are accessed on self:
@script Player extends Sprite2D fn init() { // Access fields self.name = "Player" // Call methods on self var child = self.get_node("weapon") var parent = self.get_parent() self.add_child(new_node) }