DynNode
⚠️ Experimental - Pre-Release
DynNode is a purely internal type used for runtime representation of nodes when the exact type is unknown. It's essentially a type-erased reference to any node type that can be cast at runtime to access the specific features it needs.
By default, DynNode behaves like the base Node type - it provides access to basic node functionality like hierarchy management and name access. However, it's not a "real" type that you can instantiate directly. Instead, it's used as a return type for methods that need to work with any node type.
What is DynNode?
DynNode is a type-safe way to represent "any node" when you don't know the specific node type at compile time. Methods like get_node() andget_parent() return DynNode because they can return any type of node.
Think of DynNode as a container that holds a reference to a node, but doesn't expose the specific node type's methods and fields until you cast it.
Using DynNode
When you receive a DynNode, you have two options:
1. Check the type, then cast
Use get_type() to check what type the node is, then cast it:
var child: DynNode = self.get_node("sprite")if child.get_type() == NodeType.Sprite2D { var sprite = child as Sprite2D sprite.texture = Texture.load("res://sprite.png")}2. Direct cast when calling
If you know the type, you can cast directly:
var sprite = self.get_node("sprite") as Sprite2Dsprite.texture = Texture.load("res://sprite.png")Available Methods
Since DynNode behaves like the base Node type, you can use all the methods from Node:
get_type() →NodeType- Get the actual node typeget_node(name: string) →DynNode- Get a child nodeget_parent() →DynNode- Get the parent nodeadd_child(node:DynNode)- Add a child noderemove()- Delete this node and all its children from the entire sceneclear_children()- Delete all child nodes from the scene- Access to
nameandscript_pathfields
Important Notes
- DynNode is not a real node type - You cannot create nodes with type DynNode
- It's a type-erased reference - It represents any node type, but doesn't expose type-specific features
- Cast to access specific features - Use the
askeyword to cast to specific node types - Type checking is safe - Use
get_type()to verify the type before casting
Example Usage
@script GameManager extends Node fn init() { // get_node returns DynNode var child: DynNode = self.get_node("player") // Check type before using if child.get_type() == NodeType.Sprite2D { var player = child as Sprite2D player.texture = Texture.load("res://player.png") } // Or cast directly if you're sure of the type var weapon = self.get_node("weapon") as Sprite2D weapon.transform.position.x = 10.0 }Related
- Node- Base node class that DynNode behaves like
- NodeType- Enum returned by
get_type()for type checking - Variables & Types- Learn about type casting with the
askeyword
