The #[derive(NodeTreeView)] macro provides a typed, ergonomic way to access specific nodes within a spawned Godot scene using node paths. This avoids manual and fragile GodotNodeHandle lookups.
Defining a View
Define a struct and derive NodeTreeView. Use the #[node("<path>")] attribute to map fields to specific nodes. Field types should be GodotNodeHandle or Option<GodotNodeHandle>.
#[derive(NodeTreeView)]
pub struct CharacterNodes {
#[node("AnimatedSprite2D")]
pub animated_sprite: GodotNodeHandle,
#[node("VisibleOnScreenNotifier2D")]
pub visibility_notifier: GodotNodeHandle,
}
Using the View
To use the view, obtain a handle to the root node of the scene (e.g., via GodotAccess) and call CharacterNodes::from_node(root_handle).
fn new_character_initialize(
entities: Query<&GodotNodeHandle, Added<Character>>,
mut godot: GodotAccess,
) {
for handle in &entities {
let character = godot.get::<RigidBody2D>(*handle);
let character_nodes = CharacterNodes::from_node(character).unwrap();
// character_nodes.animated_sprite is now accessible
}
}
Path Patterns
Node paths in the #[node] attribute support wildcards:
/root/*/HUD/CurrentLevel: Matches any single node name where * appears./root/Level*/HUD/CurrentLevel: Matches node names starting with "Level".*/HUD/CurrentLevel: Matches relative to the base node.
Generated Path Constants
The macro automatically generates public string constants for each field in the format <UPPERCASE_FIELD_NAME>_PATH inside the struct's impl block. For example, CharacterNodes::ANIMATED_SPRITE_PATH will equal "AnimatedSprite2D".
#[derive(NodeTreeView)]
pub struct CharacterNodes {
#[node("AnimatedSprite2D")]
pub animated_sprite: GodotNodeHandle,
#[node("VisibleOnScreenNotifier2D")]
pub visibility_notifier: GodotNodeHandle,
}
// Generated impl:
impl CharacterNodes {
pub const ANIMATED_SPRITE_PATH: &'static str = "AnimatedSprite2D";
pub const VISIBILITY_NOTIFIER_PATH: &'static str = "VisibleOnScreenNotifier2D";
}