virtualx-engine/doc/classes/NodePath.xml

220 lines
10 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="UTF-8" ?>
<class name="NodePath" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
A pre-parsed scene tree path.
</brief_description>
<description>
2023-08-02 13:13:30 +02:00
The [NodePath] built-in [Variant] type represents a path to a node or property in a hierarchy of nodes. It is designed to be efficiently passed into many built-in methods (such as [method Node.get_node], [method Object.set_indexed], [method Tween.tween_property], etc.) without a hard dependence on the node or property they point to.
A node path is represented as a [String] composed of slash-separated ([code]/[/code]) node names and colon-separated ([code]:[/code]) property names (also called "subnames"). Similar to a filesystem path, [code]".."[/code] and [code]"."[/code] are special node names. They refer to the parent node and the current node, respectively.
The following examples are paths relative to the current node:
[codeblock]
2023-08-02 13:13:30 +02:00
^"A" # Points to the direct child A.
^"A/B" # Points to A's child B.
^"." # Points to the current node.
^".." # Points to the parent node.
^"../C" # Points to the sibling node C.
^"../.." # Points to the grandparent node.
[/codeblock]
2023-08-02 13:13:30 +02:00
A leading slash means the path is absolute, and begins from the [SceneTree]:
[codeblock]
^"/root" # Points to the SceneTree's root Window.
^"/root/Title" # May point to the main scene's root node named "Title".
^"/root/Global" # May point to an autoloaded node or scene named "Global".
[/codeblock]
Despite their name, node paths may also point to a property:
[codeblock]
^"position" # Points to this object's position.
^"position:x" # Points to this object's position in the x axis.
^"Camera3D:rotation:y" # Points to the child Camera3D and its y rotation.
^"/root:size:x" # Points to the root Window and its width.
[/codeblock]
Node paths cannot check whether they are valid and may point to nodes or properties that do not exist. Their meaning depends entirely on the context in which they're used.
You usually do not have to worry about the [NodePath] type, as strings are automatically converted to the type when necessary. There are still times when defining node paths is useful. For example, exported [NodePath] properties allow you to easily select any node within the currently edited scene. They are also automatically updated when moving, renaming or deleting nodes in the scene tree editor. See also [annotation @GDScript.@export_node_path].
See also [StringName], which is a similar type designed for optimised strings.
[b]Note:[/b] In a boolean context, a [NodePath] will evaluate to [code]false[/code] if it is empty ([code]NodePath("")[/code]). Otherwise, a [NodePath] will always evaluate to [code]true[/code].
</description>
<tutorials>
<link title="2D Role Playing Game Demo">https://godotengine.org/asset-library/asset/520</link>
</tutorials>
<constructors>
<constructor name="NodePath">
<return type="NodePath" />
<description>
Constructs an empty [NodePath].
</description>
</constructor>
<constructor name="NodePath">
<return type="NodePath" />
<param index="0" name="from" type="NodePath" />
<description>
2023-08-02 13:13:30 +02:00
Constructs a [NodePath] as a copy of the given [NodePath].
</description>
</constructor>
<constructor name="NodePath">
<return type="NodePath" />
<param index="0" name="from" type="String" />
<description>
2023-08-02 13:13:30 +02:00
Constructs a [NodePath] from a [String]. The created path is absolute if prefixed with a slash (see [method is_absolute]).
The "subnames" optionally included after the path to the target node can point to properties, and can also be nested.
Examples of strings that could be node paths:
[codeblock]
# Points to the Sprite2D node.
2023-08-02 13:13:30 +02:00
"Level/RigidBody2D/Sprite2D"
# Points to the Sprite2D node and its "texture" resource.
2023-08-02 13:13:30 +02:00
# get_node() would retrieve the Sprite2D, while get_node_and_resource()
# would retrieve both the Sprite2D node and the "texture" resource.
2023-08-02 13:13:30 +02:00
"Level/RigidBody2D/Sprite2D:texture"
# Points to the Sprite2D node and its "position" property.
2023-08-02 13:13:30 +02:00
"Level/RigidBody2D/Sprite2D:position"
# Points to the Sprite2D node and the "x" component of its "position" property.
2023-08-02 13:13:30 +02:00
"Level/RigidBody2D/Sprite2D:position:x"
# Points to the RigidBody2D node as an absolute path beginning from the SceneTree.
"/root/Level/RigidBody2D"
[/codeblock]
2023-08-02 13:13:30 +02:00
[b]Note:[/b] In GDScript, it's also possible to convert a constant string into a node path by prefixing it with [code]^[/code]. [code]^"path/to/node"[/code] is equivalent to [code]NodePath("path/to/node")[/code].
</description>
</constructor>
</constructors>
<methods>
<method name="get_as_property_path" qualifiers="const">
<return type="NodePath" />
2017-11-24 09:16:27 +01:00
<description>
2023-08-02 13:13:30 +02:00
Returns a copy of this node path with a colon character ([code]:[/code]) prefixed, transforming it to a pure property path with no node names (relative to the current node).
[codeblocks]
[gdscript]
2023-08-02 13:13:30 +02:00
# node_path points to the "x" property of the child node named "position".
var node_path = ^"position:x"
# property_path points to the "position" in the "x" axis of this node.
var property_path = node_path.get_as_property_path()
2023-08-02 13:13:30 +02:00
print(property_path) # Prints ":position:x"
[/gdscript]
[csharp]
2023-08-02 13:13:30 +02:00
// nodePath points to the "x" property of the child node named "position".
var nodePath = new NodePath("position:x");
2023-08-02 13:13:30 +02:00
// propertyPath points to the "position" in the "x" axis of this node.
NodePath propertyPath = nodePath.GetAsPropertyPath();
2023-08-02 13:13:30 +02:00
GD.Print(propertyPath); // Prints ":position:x".
[/csharp]
[/codeblocks]
2017-11-24 09:16:27 +01:00
</description>
</method>
2022-06-27 10:06:50 +02:00
<method name="get_concatenated_names" qualifiers="const">
<return type="StringName" />
<description>
2023-08-02 13:13:30 +02:00
Returns all node names concatenated with a slash character ([code]/[/code]) as a single [StringName].
2022-06-27 10:06:50 +02:00
</description>
</method>
<method name="get_concatenated_subnames" qualifiers="const">
<return type="StringName" />
2017-11-24 09:16:27 +01:00
<description>
2023-08-02 13:13:30 +02:00
Returns all property subnames concatenated with a colon character ([code]:[/code]) as a single [StringName].
[codeblocks]
[gdscript]
2023-08-02 13:13:30 +02:00
var node_path = ^"Sprite2D:texture:resource_name"
print(node_path.get_concatenated_subnames()) # Prints "texture:resource_name".
[/gdscript]
[csharp]
2023-08-02 13:13:30 +02:00
var nodePath = new NodePath("Sprite2D:texture:resource_name");
GD.Print(nodePath.GetConcatenatedSubnames()); // Prints "texture:resource_name".
[/csharp]
[/codeblocks]
2017-11-24 09:16:27 +01:00
</description>
</method>
<method name="get_name" qualifiers="const">
<return type="StringName" />
<param index="0" name="idx" type="int" />
<description>
2023-08-02 13:13:30 +02:00
Returns the node name indicated by [param idx], starting from 0. If [param idx] is out of bounds, an error is generated. See also [method get_subname_count] and [method get_name_count].
[codeblocks]
[gdscript]
2023-08-02 13:13:30 +02:00
var sprite_path = NodePath("../RigidBody2D/Sprite2D")
print(sprite_path.get_name(0)) # Prints "..".
print(sprite_path.get_name(1)) # Prints "RigidBody2D".
print(sprite_path.get_name(2)) # Prints "Sprite".
[/gdscript]
[csharp]
2023-08-02 13:13:30 +02:00
var spritePath = new NodePath("../RigidBody2D/Sprite2D");
GD.Print(spritePath.GetName(0)); // Prints "..".
GD.Print(spritePath.GetName(1)); // Prints "PathFollow2D".
GD.Print(spritePath.GetName(2)); // Prints "Sprite".
[/csharp]
[/codeblocks]
</description>
</method>
<method name="get_name_count" qualifiers="const">
<return type="int" />
<description>
2023-08-02 13:13:30 +02:00
Returns the number of node names in the path. Property subnames are not included.
For example, [code]"../RigidBody2D/Sprite2D:texture"[/code] contains 3 node names.
</description>
</method>
<method name="get_subname" qualifiers="const">
<return type="StringName" />
<param index="0" name="idx" type="int" />
<description>
2023-08-02 13:13:30 +02:00
Returns the property name indicated by [param idx], starting from 0. If [param idx] is out of bounds, an error is generated. See also [method get_subname_count].
[codeblocks]
[gdscript]
2023-08-02 13:13:30 +02:00
var path_to_name = NodePath("Sprite2D:texture:resource_name")
print(path_to_name.get_subname(0)) # Prints "texture".
print(path_to_name.get_subname(1)) # Prints "resource_name".
[/gdscript]
[csharp]
2023-08-02 13:13:30 +02:00
var pathToName = new NodePath("Sprite2D:texture:resource_name");
GD.Print(pathToName.GetSubname(0)); // Prints "texture".
GD.Print(pathToName.GetSubname(1)); // Prints "resource_name".
[/csharp]
[/codeblocks]
</description>
</method>
<method name="get_subname_count" qualifiers="const">
<return type="int" />
<description>
2023-08-02 13:13:30 +02:00
Returns the number of property names ("subnames") in the path. Each subname in the node path is listed after a colon character ([code]:[/code]).
For example, [code]"Level/RigidBody2D/Sprite2D:texture:resource_name"[/code] contains 2 subnames.
</description>
</method>
<method name="hash" qualifiers="const">
<return type="int" />
<description>
2023-08-02 13:13:30 +02:00
Returns the 32-bit hash value representing the node path's contents.
[b]Note:[/b] Node paths with equal hash values are [i]not[/i] guaranteed to be the same, as a result of hash collisions. Node paths with different hash values are guaranteed to be different.
</description>
</method>
<method name="is_absolute" qualifiers="const">
<return type="bool" />
<description>
2023-08-02 13:13:30 +02:00
Returns [code]true[/code] if the node path is absolute. Unlike a relative path, an absolute path is represented by a leading slash character ([code]/[/code]) and always begins from the [SceneTree]. It can be used to reliably access nodes from the root node (e.g. [code]"/root/Global"[/code] if an autoload named "Global" exists).
</description>
</method>
<method name="is_empty" qualifiers="const">
<return type="bool" />
<description>
2023-08-02 13:13:30 +02:00
Returns [code]true[/code] if the node path has been constructed from an empty [String] ([code]""[/code]).
</description>
</method>
</methods>
<operators>
<operator name="operator !=">
<return type="bool" />
<param index="0" name="right" type="NodePath" />
<description>
2022-11-21 03:48:36 +01:00
Returns [code]true[/code] if two node paths are not equal.
</description>
</operator>
<operator name="operator ==">
<return type="bool" />
<param index="0" name="right" type="NodePath" />
<description>
2023-08-02 13:13:30 +02:00
Returns [code]true[/code] if two node paths are equal, that is, they are composed of the same node names and subnames in the same order.
</description>
</operator>
</operators>
</class>