Struct NodePath
pub struct NodePath { /* private fields */ }
Expand description
A pre-parsed scene tree path.
§Null bytes
Note that Godot ignores any bytes after a null-byte. This means that for instance "hello, world!"
and "hello, world!\0 ignored by Godot"
will be treated as the same string if converted to a NodePath
.
§All string types
Intended use case | String type |
---|---|
General purpose | GString |
Interned names | StringName |
Scene-node paths | NodePath |
§Godot docs
Implementations§
§impl NodePath
impl NodePath
pub fn get_name(&self, index: usize) -> StringName
pub fn get_name(&self, index: usize) -> StringName
Returns the node name at position index
.
If you want to get a property name instead, check out get_subname()
.
§Example
let path = NodePath::from("../RigidBody2D/Sprite2D");
godot_print!("{}", path.get_name(0)); // ".."
godot_print!("{}", path.get_name(1)); // "RigidBody2D"
godot_print!("{}", path.get_name(2)); // "Sprite"
§Panics
In Debug mode, if index
is out of bounds. In Release, a Godot error is generated and the result is unspecified (but safe).
pub fn get_subname(&self, index: usize) -> StringName
pub fn get_subname(&self, index: usize) -> StringName
Returns the node subname (property) at position index
.
If you want to get a node name instead, check out get_name()
.
§Example
let path = NodePath::from("Sprite2D:texture:resource_name");
godot_print!("{}", path.get_subname(0)); // "texture"
godot_print!("{}", path.get_subname(1)); // "resource_name"
§Panics
In Debug mode, if index
is out of bounds. In Release, a Godot error is generated and the result is unspecified (but safe).
pub fn get_name_count(&self) -> usize
pub fn get_name_count(&self) -> usize
Returns the number of node names in the path. Property subnames are not included.
pub fn get_subname_count(&self) -> usize
pub fn get_subname_count(&self) -> usize
Returns the number of property names (“subnames”) in the path. Each subname in the node path is listed after a colon character (:
).
pub fn get_total_count(&self) -> usize
pub fn get_total_count(&self) -> usize
Returns the total number of names + subnames.
This method does not exist in Godot and is provided in Rust for convenience.
pub fn subpath(&self, begin: i32, exclusive_end: i32) -> NodePath
Available on since_api="4.3"
only.
pub fn subpath(&self, begin: i32, exclusive_end: i32) -> NodePath
since_api="4.3"
only.Returns the range begin..exclusive_end
as a new NodePath
.
The absolute value of begin
and exclusive_end
will be clamped to get_total_count()
.
So, to express “until the end”, you can simply pass a large value for exclusive_end
, such as i32::MAX
.
If either begin
or exclusive_end
are negative, they will be relative to the end of the NodePath
.
For example, path.subpath(0, -2)
is a shorthand for path.subpath(0, path.get_total_count() - 2)
.
Godot equivalent: slice
§Compatibility
The slice()
behavior for Godot <= 4.3 is unintuitive, see #100954. godot-rust
automatically changes this to the fixed version for Godot 4.4+, even when used in older versions. So, the behavior is always the same.
pub fn arg<T>(&self) -> impl AsArg<T>
pub fn arg<T>(&self) -> impl AsArg<T>
Use as argument for an impl AsArg<GString|StringName>
parameter.
This is a convenient way to convert arguments of similar string types.
§Example
PackedStringArray
can insert elements using AsArg<GString>
, so let’s pass a NodePath
:
let node_path = NodePath::from("Node2D/Label");
let mut array = PackedStringArray::new();
array.push(node_path.arg());
§Generic bounds
The bounds are implementation-defined and may change at any time. Do not use this function in a generic context requiring T
– use the From
trait or ParamType
in that case.
§impl NodePath
impl NodePath
pub fn is_absolute(&self) -> bool
pub fn get_concatenated_names(&self) -> StringName
pub fn get_concatenated_subnames(&self) -> StringName
pub fn get_as_property_path(&self) -> NodePath
pub fn is_empty(&self) -> bool
Trait Implementations§
§impl Export for NodePath
impl Export for NodePath
§fn export_hint() -> PropertyHintInfo
fn export_hint() -> PropertyHintInfo
§impl From<&NodePath> for StringName
impl From<&NodePath> for StringName
§fn from(path: &NodePath) -> StringName
fn from(path: &NodePath) -> StringName
§impl From<&StringName> for NodePath
impl From<&StringName> for NodePath
§fn from(string_name: &StringName) -> NodePath
fn from(string_name: &StringName) -> NodePath
§impl From<NodePath> for StringName
impl From<NodePath> for StringName
§fn from(path: NodePath) -> StringName
fn from(path: NodePath) -> StringName
Converts this NodePath
to a StringName
.
This is identical to StringName::from(&path)
, and as such there is no performance benefit.
§impl From<StringName> for NodePath
impl From<StringName> for NodePath
§fn from(string_name: StringName) -> NodePath
fn from(string_name: StringName) -> NodePath
Converts this StringName
to a NodePath
.
This is identical to NodePath::from(&string_name)
, and as such there is no performance benefit.
§impl FromGodot for NodePath
impl FromGodot for NodePath
§fn try_from_godot(
via: <NodePath as GodotConvert>::Via,
) -> Result<NodePath, ConvertError>
fn try_from_godot( via: <NodePath as GodotConvert>::Via, ) -> Result<NodePath, ConvertError>
Err
on failure.§fn from_godot(via: Self::Via) -> Self
fn from_godot(via: Self::Via) -> Self
§fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
Variant
, returning Err
on failure.§fn from_variant(variant: &Variant) -> Self
fn from_variant(variant: &Variant) -> Self
§impl GodotConvert for NodePath
impl GodotConvert for NodePath
§impl ParamType for NodePath
impl ParamType for NodePath
§fn owned_to_arg<'v>(self) -> <NodePath as ParamType>::Arg<'v>
fn owned_to_arg<'v>(self) -> <NodePath as ParamType>::Arg<'v>
impl AsArg<T>
. Read more§impl ToGodot for NodePath
impl ToGodot for NodePath
§impl Var for NodePath
impl Var for NodePath
fn get_property(&self) -> <NodePath as GodotConvert>::Via
fn set_property(&mut self, value: <NodePath as GodotConvert>::Via)
§fn var_hint() -> PropertyHintInfo
fn var_hint() -> PropertyHintInfo
GodotType::property_info
, e.g. for enums/newtypes.