2017-09-12 20:42:36 +00:00
<?xml version="1.0" encoding="UTF-8" ?>
2023-07-06 08:08:05 +00:00
<class name= "NodePath" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "../class.xsd" >
2017-09-12 20:42:36 +00:00
<brief_description >
2023-04-27 23:35:33 +00:00
A pre-parsed scene tree path.
2017-09-12 20:42:36 +00:00
</brief_description>
<description >
2023-08-28 19:32:37 +00:00
A pre-parsed relative or absolute path in a scene tree, for use with [method Node.get_node] and similar functions. It can reference a node, a resource within a node, or a property of a node or resource. For example, [code]"Path2D/PathFollow2D/Sprite2D:texture:size"[/code] would refer to the [code]size[/code] property of the [code]texture[/code] resource on the node named [code]"Sprite2D"[/code], which is a child of the other named nodes in the path.
2022-03-13 00:04:51 +00:00
You will usually just pass a string to [method Node.get_node] and it will be automatically converted, but you may occasionally want to parse a path ahead of time with [NodePath] or the literal syntax [code]^"path"[/code]. Exporting a [NodePath] variable will give you a node selection widget in the properties panel of the editor, which can often be useful.
2019-06-26 13:15:11 +00:00
A [NodePath] is composed of a list of slash-separated node names (like a filesystem path) and an optional colon-separated list of "subnames" which can be resources or properties.
2019-11-13 18:08:39 +00:00
Some examples of NodePaths include the following:
[codeblock]
# No leading slash means it is relative to the current node.
2022-03-13 00:04:51 +00:00
^"A" # Immediate child A
^"A/B" # A's child B
^"." # The current node.
^".." # The parent node.
^"../C" # A sibling node C.
2023-06-25 08:25:22 +00:00
^"../.." # The grandparent node.
2019-11-13 18:08:39 +00:00
# A leading slash means it is absolute from the SceneTree.
2022-03-13 00:04:51 +00:00
^"/root" # Equivalent to get_tree().get_root().
^"/root/Main" # If your main scene's root node were named "Main".
^"/root/MyAutoload" # If you have an autoloaded node or scene.
2019-11-13 18:08:39 +00:00
[/codeblock]
2022-03-13 00:04:51 +00:00
See also [StringName], which is a similar concept for general-purpose string interning.
2021-06-21 19:09:00 +00:00
[b]Note:[/b] In the editor, [NodePath] properties are automatically updated when moving, renaming or deleting a node in the scene tree, but they are never updated at runtime.
2023-04-27 23:35:33 +00:00
[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].
2017-09-12 20:42:36 +00:00
</description>
<tutorials >
2020-10-01 08:34:47 +00:00
<link title= "2D Role Playing Game Demo" > https://godotengine.org/asset-library/asset/520</link>
2017-09-12 20:42:36 +00:00
</tutorials>
2021-09-21 02:49:02 +00:00
<constructors >
<constructor name= "NodePath" >
2021-07-30 13:28:05 +00:00
<return type= "NodePath" />
2020-11-09 16:46:03 +00:00
<description >
Constructs an empty [NodePath].
</description>
2021-09-21 02:49:02 +00:00
</constructor>
<constructor name= "NodePath" >
2021-07-30 13:28:05 +00:00
<return type= "NodePath" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "from" type= "NodePath" />
2020-11-09 16:46:03 +00:00
<description >
2022-03-13 00:04:51 +00:00
Constructs a [NodePath] as a copy of the given [NodePath]. [code]NodePath("example")[/code] is equivalent to [code]^"example"[/code].
2020-11-09 16:46:03 +00:00
</description>
2021-09-21 02:49:02 +00:00
</constructor>
<constructor name= "NodePath" >
2021-07-30 13:28:05 +00:00
<return type= "NodePath" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "from" type= "String" />
2017-09-12 20:42:36 +00:00
<description >
2020-03-30 16:22:57 +00:00
Creates a NodePath from a string, e.g. [code]"Path2D/PathFollow2D/Sprite2D:texture:size"[/code]. A path is absolute if it starts with a slash. Absolute paths are only valid in the global scene tree, not within individual scenes. In a relative path, [code]"."[/code] and [code]".."[/code] indicate the current node and its parent.
2019-06-26 13:15:11 +00:00
The "subnames" optionally included after the path to the target node can point to resources or properties, and can also be nested.
Examples of valid NodePaths (assuming that those nodes exist and have the referenced resources or properties):
[codeblock]
2020-10-31 17:54:17 +00:00
# Points to the Sprite2D node.
2020-03-30 16:22:57 +00:00
"Path2D/PathFollow2D/Sprite2D"
# Points to the Sprite2D node and its "texture" resource.
# get_node() would retrieve "Sprite2D", while get_node_and_resource()
# would retrieve both the Sprite2D node and the "texture" resource.
"Path2D/PathFollow2D/Sprite2D:texture"
# Points to the Sprite2D node and its "position" property.
"Path2D/PathFollow2D/Sprite2D:position"
# Points to the Sprite2D node and the "x" component of its "position" property.
"Path2D/PathFollow2D/Sprite2D:position:x"
2019-06-21 23:04:47 +00:00
# Absolute path (from "root")
2019-06-26 13:15:11 +00:00
"/root/Level/Path2D"
[/codeblock]
2017-09-12 20:42:36 +00:00
</description>
2021-09-21 02:49:02 +00:00
</constructor>
</constructors>
<methods >
2021-03-18 13:44:42 +00:00
<method name= "get_as_property_path" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "NodePath" />
2017-11-24 08:16:27 +00:00
<description >
2021-03-18 11:04:28 +00:00
Returns a node path with a colon character ([code]:[/code]) prepended, transforming it to a pure property path with no node name (defaults to resolving from the current node).
2020-10-31 17:54:17 +00:00
[codeblocks]
[gdscript]
# This will be parsed as a node path to the "x" property in the "position" node.
2019-06-26 13:15:11 +00:00
var node_path = NodePath("position:x")
2020-10-31 17:54:17 +00:00
# This will be parsed as a node path to the "x" component of the "position" property in the current node.
2019-06-26 13:15:11 +00:00
var property_path = node_path.get_as_property_path()
print(property_path) # :position:x
2020-10-31 17:54:17 +00:00
[/gdscript]
[csharp]
// This will be parsed as a node path to the "x" property in the "position" node.
var nodePath = new NodePath("position:x");
// This will be parsed as a node path to the "x" component of the "position" property in the current node.
NodePath propertyPath = nodePath.GetAsPropertyPath();
GD.Print(propertyPath); // :position:x
[/csharp]
[/codeblocks]
2017-11-24 08:16:27 +00:00
</description>
</method>
2022-06-27 08:06:50 +00:00
<method name= "get_concatenated_names" qualifiers= "const" >
<return type= "StringName" />
<description >
Returns all paths concatenated with a slash character ([code]/[/code]) as separator without subnames.
</description>
</method>
2021-03-18 13:44:42 +00:00
<method name= "get_concatenated_subnames" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "StringName" />
2017-11-24 08:16:27 +00:00
<description >
2019-06-26 13:15:11 +00:00
Returns all subnames concatenated with a colon character ([code]:[/code]) as separator, i.e. the right side of the first colon in a node path.
2020-10-31 17:54:17 +00:00
[codeblocks]
[gdscript]
2023-08-09 09:20:30 +00:00
var node_path = NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path")
print(node_path.get_concatenated_subnames()) # texture:load_path
2020-10-31 17:54:17 +00:00
[/gdscript]
[csharp]
2023-08-09 09:20:30 +00:00
var nodePath = new NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path");
GD.Print(nodePath.GetConcatenatedSubnames()); // texture:load_path
2020-10-31 17:54:17 +00:00
[/csharp]
[/codeblocks]
2017-11-24 08:16:27 +00:00
</description>
</method>
2021-03-18 13:44:42 +00:00
<method name= "get_name" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "StringName" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "idx" type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2022-08-12 13:36:48 +00:00
Gets the node name indicated by [param idx] (0 to [method get_name_count] - 1).
2020-10-31 17:54:17 +00:00
[codeblocks]
[gdscript]
2020-03-30 16:22:57 +00:00
var node_path = NodePath("Path2D/PathFollow2D/Sprite2D")
2019-06-26 13:15:11 +00:00
print(node_path.get_name(0)) # Path2D
print(node_path.get_name(1)) # PathFollow2D
2023-11-22 20:07:17 +00:00
print(node_path.get_name(2)) # Sprite2D
2020-10-31 17:54:17 +00:00
[/gdscript]
[csharp]
var nodePath = new NodePath("Path2D/PathFollow2D/Sprite2D");
GD.Print(nodePath.GetName(0)); // Path2D
GD.Print(nodePath.GetName(1)); // PathFollow2D
2023-11-22 20:07:17 +00:00
GD.Print(nodePath.GetName(2)); // Sprite2D
2020-10-31 17:54:17 +00:00
[/csharp]
[/codeblocks]
2017-09-12 20:42:36 +00:00
</description>
</method>
2021-03-18 13:44:42 +00:00
<method name= "get_name_count" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Gets the number of node names which make up the path. Subnames (see [method get_subname_count]) are not included.
2020-03-30 16:22:57 +00:00
For example, [code]"Path2D/PathFollow2D/Sprite2D"[/code] has 3 names.
2017-09-12 20:42:36 +00:00
</description>
</method>
2021-03-18 13:44:42 +00:00
<method name= "get_subname" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "StringName" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "idx" type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2023-03-26 07:54:37 +00:00
Gets the resource or property name indicated by [param idx] (0 to [method get_subname_count] - 1).
2020-10-31 17:54:17 +00:00
[codeblocks]
[gdscript]
2020-03-30 16:22:57 +00:00
var node_path = NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path")
2019-06-26 13:15:11 +00:00
print(node_path.get_subname(0)) # texture
print(node_path.get_subname(1)) # load_path
2020-10-31 17:54:17 +00:00
[/gdscript]
[csharp]
var nodePath = new NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path");
GD.Print(nodePath.GetSubname(0)); // texture
GD.Print(nodePath.GetSubname(1)); // load_path
[/csharp]
[/codeblocks]
2017-09-12 20:42:36 +00:00
</description>
</method>
2021-03-18 13:44:42 +00:00
<method name= "get_subname_count" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "int" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-21 23:04:47 +00:00
Gets the number of resource or property names ("subnames") in the path. Each subname is listed after a colon character ([code]:[/code]) in the node path.
2020-03-30 16:22:57 +00:00
For example, [code]"Path2D/PathFollow2D/Sprite2D:texture:load_path"[/code] has 2 subnames.
2017-09-12 20:42:36 +00:00
</description>
</method>
2022-06-06 09:19:56 +00:00
<method name= "hash" qualifiers= "const" >
<return type= "int" />
<description >
Returns the 32-bit hash value representing the [NodePath]'s contents.
</description>
</method>
2021-03-18 13:44:42 +00:00
<method name= "is_absolute" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "bool" />
2017-09-12 20:42:36 +00:00
<description >
2019-06-26 13:15:11 +00:00
Returns [code]true[/code] if the node path is absolute (as opposed to relative), which means that it starts with a slash character ([code]/[/code]). Absolute node paths can be used to access the root node ([code]"/root"[/code]) or autoloads (e.g. [code]"/global"[/code] if a "global" autoload was registered).
2017-09-12 20:42:36 +00:00
</description>
</method>
2021-03-18 13:44:42 +00:00
<method name= "is_empty" qualifiers= "const" >
2021-07-30 13:28:05 +00:00
<return type= "bool" />
2017-09-12 20:42:36 +00:00
<description >
2019-05-24 02:15:43 +00:00
Returns [code]true[/code] if the node path is empty.
2017-09-12 20:42:36 +00:00
</description>
</method>
2021-09-21 02:49:02 +00:00
</methods>
<operators >
<operator name= "operator !=" >
2021-07-30 13:28:05 +00:00
<return type= "bool" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "right" type= "NodePath" />
2020-11-10 13:16:20 +00:00
<description >
2022-11-21 02:48:36 +00:00
Returns [code]true[/code] if two node paths are not equal.
2020-11-10 13:16:20 +00:00
</description>
2021-09-21 02:49:02 +00:00
</operator>
<operator name= "operator ==" >
2021-07-30 13:28:05 +00:00
<return type= "bool" />
2022-08-06 18:11:48 +00:00
<param index= "0" name= "right" type= "NodePath" />
2020-11-10 13:16:20 +00:00
<description >
2022-11-21 02:48:36 +00:00
Returns [code]true[/code] if two node paths are equal, i.e. all node names in the path are the same and in the same order.
2020-11-10 13:16:20 +00:00
</description>
2021-09-21 02:49:02 +00:00
</operator>
</operators>
2017-09-12 20:42:36 +00:00
</class>