← Back to Nodes
NodeType
⚠️ Experimental - Pre-Release
NodeType is an enum that represents the type of a node at runtime. It is returned by the get_type() method on nodes andDynNode references.
Use NodeType to check what type a DynNode is before casting it to a more specific node type. This enables safe runtime type checking.
Getting NodeType
Call get_type() on any node or DynNode to get its NodeType:
// Get type from a DynNodevar child: DynNode = self.get_node("player")var node_type = child.get_type() // Get type from a specific nodevar sprite: Sprite2D = new Sprite2D()var sprite_type = sprite.get_type() // Returns NodeType.Sprite2DUsing NodeType for Type Checking
The primary use of NodeType is to safely check the type of aDynNode before casting:
var child: DynNode = self.get_node("sprite") // Check type before castingif child.get_type() == NodeType.Sprite2D { var sprite = child as Sprite2D sprite.texture = Texture.load("res://sprite.png")} // Check for multiple typesif child.get_type() == NodeType.Sprite2D || child.get_type() == NodeType.Shape2D { // Handle 2D visual nodes}Available NodeType Values
NodeType includes values for all node types in the engine:
Base Types:
NodeType.NodeNodeType.Node2DNodeType.Node3DNodeType.UINode
2D Nodes:
NodeType.Sprite2DNodeType.Camera2DNodeType.Area2DNodeType.Shape2DNodeType.CollisionShape2D
3D Nodes:
NodeType.Camera3DNodeType.MeshInstance3DNodeType.DirectionalLight3DNodeType.OmniLight3DNodeType.SpotLight3D
Example: Safe Type Checking
@script GameManager extends Node fn init() { // Get a child 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") player.transform.position.x = 100 } else if child.get_type() == NodeType.Area2D { var area = child as Area2D Console.print("Found area node") } else { Console.print("Unknown node type: " + child.get_type()) } }Important Notes
- NodeType is an enum - It's not a class you can instantiate
- Returned by get_type() - Use
get_type()to get the NodeType of any node - Use for type checking - Compare NodeType values to safely check node types before casting
- Works with DynNode - Essential for safely working with DynNode references
Related
- Node- Base node class with
get_type()method - DynNode- Type-erased node reference that requires type checking
- Variables & Types- Learn about type casting with the
askeyword
