module
Godot::NodeContext
Overview
=========================================================================== NodeContext: Active Execution Context for Ergonomic Node Lookups
Manages the active Godot::Node context on the calling thread/fiber,
allowing bare/unary operators like ~("$Path") and ~("%Unique")
to resolve relative to self without explicit receiver notation.
Performance & Zero-Cost Guarantees:
- Tracking uses a thread-local pointer assignment (
@[ThreadLocal]). - Zero heap allocations (0.0 B/op).
- Single machine instruction latency (~0.4 ns in release builds).
- 100% thread-safe across OS threads and multi-threaded worker pools.
Defined in:
libgodot/node_context.crClass Method Summary
-
.current : Node | Nil
Returns the currently executing
Godot::Node, if any. -
.current=(node : Node | Nil)
Sets the currently executing
Godot::Node. -
.current? : Node | Nil
Nilable alias for
NodeContext.current. -
.resolve_active_node(path : String) : Node
Resolves a node path against the active node context.
-
.resolve_active_node_as(type : T.class, preferred_name : String | Nil = nil) : T forall T
Resolves a typed node against the active node context.
-
.resolve_active_node_or_nil(type : T.class, preferred_name : String | Nil = nil) : T | Nil forall T
Resolves a node against the active node context or returns
nil(get_node_or_nullparity). -
.resolve_active_node_path_as(path : String, type : T.class) forall T
Resolves a typed node at a specific path relative to the active node context.
-
.resolve_active_regex(regex : Regex) : Node | Nil
Resolves a node against the active node context matching a regex pattern.
-
.resolve_active_regex_all(regex : Regex) : Array(Node)
Resolves all nodes against the active node context matching a regex pattern.
-
.scope(node : Node | Nil, &)
Scopes execution of a block to the specified node context, restoring previous context upon exit even if an exception is raised.
-
.scope(obj : Object, &)
Scopes execution of a block if the object is a
Node, or executes directly.
Class Method Detail
Returns the currently executing Godot::Node, if any.
Used internally by unary operators like ~"Path" to resolve relative paths
without requiring an explicit receiver reference.
Sets the currently executing Godot::Node.
Prefer using NodeContext.scope instead of setting this directly to ensure
that the previous context is always restored even if an exception occurs.
Resolves a node path against the active node context.
Raises Godot::NodeNotFoundError if no node context is active or if the node
cannot be found at the given relative or unique path.
Supported Syntax:
- Relative child:
~"Sprite2D"or~"Visuals/MeshInstance3D" - Scene Unique Node:
~"%HealthBar" - Absolute tree path:
~"/root/Main/Player" - Parent navigation:
~"../Sibling"
Resolves a typed node against the active node context.
Search order:
- Direct child by class name (e.g.,
"Sprite2D"forSprite2D) - Scene Unique Node by class name (e.g.,
"%Sprite2D") - First child in
childrenthat matches typeT(or inherits fromTin native ClassDB)
Raises Godot::NodeNotFoundError if no matching node is found.
Example:
cam = NodeContext.resolve_active_node_as(Camera3D)
cam.fov = 75.0_f32
Resolves a node against the active node context or returns nil (get_node_or_null parity).
Handles Class types and nilable union types like (Sprite2D | Nil), returning typed T?.
Search order:
- Direct child by class name
- Scene Unique Node by class name (
"%TypeName") - First child in
childrenthat matches typeT(or inherits fromTin native ClassDB)
Returns nil if no matching child node exists or if no node context is active.
Example:
if weapon = NodeContext.resolve_active_node_or_nil(WeaponSlot?)
weapon.equip(sword)
end
Resolves a typed node at a specific path relative to the active node context.
Handles nilable types (e.g., MyNodeType?) by returning nil if missing.
Example:
sprite = NodeContext.resolve_active_node_path_as("Visuals/Sprite2D", Sprite2D)
opt_cam = NodeContext.resolve_active_node_path_as("Camera3D", Camera3D?)
Resolves a node against the active node context matching a regex pattern.
Resolves all nodes against the active node context matching a regex pattern.
Scopes execution of a block to the specified node context, restoring previous context upon exit even if an exception is raised.
All virtual methods (_ready, _process, _physics_process, etc.) are
automatically wrapped in this scope by _godot_call_virtual.
Example:
NodeContext.scope(player_node) do
sprite = ~"Sprite2D" # Resolves relative to player_node
sprite.modulate = Godot::Color.new(1.0, 0.0, 0.0)
end
Scopes execution of a block if the object is a Node, or executes directly.
Allows safe, uniform scoping over arbitrary Godot::Object instances.