class_nodepath.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/NodePath.xml.
  6. .. _class_NodePath:
  7. NodePath
  8. ========
  9. A pre-parsed scene tree path.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. The **NodePath** built-in :ref:`Variant<class_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 :ref:`Node.get_node<class_Node_method_get_node>`, :ref:`Object.set_indexed<class_Object_method_set_indexed>`, :ref:`Tween.tween_property<class_Tween_method_tween_property>`, etc.) without a hard dependence on the node or property they point to.
  14. A node path is represented as a :ref:`String<class_String>` composed of slash-separated (``/``) node names and colon-separated (``:``) property names (also called "subnames"). Similar to a filesystem path, ``".."`` and ``"."`` are special node names. They refer to the parent node and the current node, respectively.
  15. The following examples are paths relative to the current node:
  16. ::
  17. ^"A" # Points to the direct child A.
  18. ^"A/B" # Points to A's child B.
  19. ^"." # Points to the current node.
  20. ^".." # Points to the parent node.
  21. ^"../C" # Points to the sibling node C.
  22. ^"../.." # Points to the grandparent node.
  23. A leading slash means the path is absolute, and begins from the :ref:`SceneTree<class_SceneTree>`:
  24. ::
  25. ^"/root" # Points to the SceneTree's root Window.
  26. ^"/root/Title" # May point to the main scene's root node named "Title".
  27. ^"/root/Global" # May point to an autoloaded node or scene named "Global".
  28. Despite their name, node paths may also point to a property:
  29. ::
  30. ^":position" # Points to this object's position.
  31. ^":position:x" # Points to this object's position in the x axis.
  32. ^"Camera3D:rotation:y" # Points to the child Camera3D and its y rotation.
  33. ^"/root:size:x" # Points to the root Window and its width.
  34. In some situations, it's possible to omit the leading ``:`` when pointing to an object's property. As an example, this is the case with :ref:`Object.set_indexed<class_Object_method_set_indexed>` and :ref:`Tween.tween_property<class_Tween_method_tween_property>`, as those methods call :ref:`get_as_property_path<class_NodePath_method_get_as_property_path>` under the hood. However, it's generally recommended to keep the ``:`` prefix.
  35. 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.
  36. 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 :ref:`@GDScript.@export_node_path<class_@GDScript_annotation_@export_node_path>`.
  37. See also :ref:`StringName<class_StringName>`, which is a similar type designed for optimized strings.
  38. \ **Note:** In a boolean context, a **NodePath** will evaluate to ``false`` if it is empty (``NodePath("")``). Otherwise, a **NodePath** will always evaluate to ``true``.
  39. .. note::
  40. There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information.
  41. .. rst-class:: classref-introduction-group
  42. Tutorials
  43. ---------
  44. - `2D Role Playing Game (RPG) Demo <https://godotengine.org/asset-library/asset/2729>`__
  45. .. rst-class:: classref-reftable-group
  46. Constructors
  47. ------------
  48. .. table::
  49. :widths: auto
  50. +---------------------------------+----------------------------------------------------------------------------------------------------+
  51. | :ref:`NodePath<class_NodePath>` | :ref:`NodePath<class_NodePath_constructor_NodePath>`\ (\ ) |
  52. +---------------------------------+----------------------------------------------------------------------------------------------------+
  53. | :ref:`NodePath<class_NodePath>` | :ref:`NodePath<class_NodePath_constructor_NodePath>`\ (\ from\: :ref:`NodePath<class_NodePath>`\ ) |
  54. +---------------------------------+----------------------------------------------------------------------------------------------------+
  55. | :ref:`NodePath<class_NodePath>` | :ref:`NodePath<class_NodePath_constructor_NodePath>`\ (\ from\: :ref:`String<class_String>`\ ) |
  56. +---------------------------------+----------------------------------------------------------------------------------------------------+
  57. .. rst-class:: classref-reftable-group
  58. Methods
  59. -------
  60. .. table::
  61. :widths: auto
  62. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`NodePath<class_NodePath>` | :ref:`get_as_property_path<class_NodePath_method_get_as_property_path>`\ (\ ) |const| |
  64. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`StringName<class_StringName>` | :ref:`get_concatenated_names<class_NodePath_method_get_concatenated_names>`\ (\ ) |const| |
  66. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`StringName<class_StringName>` | :ref:`get_concatenated_subnames<class_NodePath_method_get_concatenated_subnames>`\ (\ ) |const| |
  68. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`StringName<class_StringName>` | :ref:`get_name<class_NodePath_method_get_name>`\ (\ idx\: :ref:`int<class_int>`\ ) |const| |
  70. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`int<class_int>` | :ref:`get_name_count<class_NodePath_method_get_name_count>`\ (\ ) |const| |
  72. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`StringName<class_StringName>` | :ref:`get_subname<class_NodePath_method_get_subname>`\ (\ idx\: :ref:`int<class_int>`\ ) |const| |
  74. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`int<class_int>` | :ref:`get_subname_count<class_NodePath_method_get_subname_count>`\ (\ ) |const| |
  76. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`int<class_int>` | :ref:`hash<class_NodePath_method_hash>`\ (\ ) |const| |
  78. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`bool<class_bool>` | :ref:`is_absolute<class_NodePath_method_is_absolute>`\ (\ ) |const| |
  80. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`bool<class_bool>` | :ref:`is_empty<class_NodePath_method_is_empty>`\ (\ ) |const| |
  82. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  83. | :ref:`NodePath<class_NodePath>` | :ref:`slice<class_NodePath_method_slice>`\ (\ begin\: :ref:`int<class_int>`, end\: :ref:`int<class_int>` = 2147483647\ ) |const| |
  84. +-------------------------------------+----------------------------------------------------------------------------------------------------------------------------------+
  85. .. rst-class:: classref-reftable-group
  86. Operators
  87. ---------
  88. .. table::
  89. :widths: auto
  90. +-------------------------+---------------------------------------------------------------------------------------------------------+
  91. | :ref:`bool<class_bool>` | :ref:`operator !=<class_NodePath_operator_neq_NodePath>`\ (\ right\: :ref:`NodePath<class_NodePath>`\ ) |
  92. +-------------------------+---------------------------------------------------------------------------------------------------------+
  93. | :ref:`bool<class_bool>` | :ref:`operator ==<class_NodePath_operator_eq_NodePath>`\ (\ right\: :ref:`NodePath<class_NodePath>`\ ) |
  94. +-------------------------+---------------------------------------------------------------------------------------------------------+
  95. .. rst-class:: classref-section-separator
  96. ----
  97. .. rst-class:: classref-descriptions-group
  98. Constructor Descriptions
  99. ------------------------
  100. .. _class_NodePath_constructor_NodePath:
  101. .. rst-class:: classref-constructor
  102. :ref:`NodePath<class_NodePath>` **NodePath**\ (\ ) :ref:`🔗<class_NodePath_constructor_NodePath>`
  103. Constructs an empty **NodePath**.
  104. .. rst-class:: classref-item-separator
  105. ----
  106. .. rst-class:: classref-constructor
  107. :ref:`NodePath<class_NodePath>` **NodePath**\ (\ from\: :ref:`NodePath<class_NodePath>`\ )
  108. Constructs a **NodePath** as a copy of the given **NodePath**.
  109. .. rst-class:: classref-item-separator
  110. ----
  111. .. rst-class:: classref-constructor
  112. :ref:`NodePath<class_NodePath>` **NodePath**\ (\ from\: :ref:`String<class_String>`\ )
  113. Constructs a **NodePath** from a :ref:`String<class_String>`. The created path is absolute if prefixed with a slash (see :ref:`is_absolute<class_NodePath_method_is_absolute>`).
  114. The "subnames" optionally included after the path to the target node can point to properties, and can also be nested.
  115. The following strings can be valid node paths:
  116. ::
  117. # Points to the Sprite2D node.
  118. "Level/RigidBody2D/Sprite2D"
  119. # Points to the Sprite2D node and its "texture" resource.
  120. # get_node() would retrieve the Sprite2D, while get_node_and_resource()
  121. # would retrieve both the Sprite2D node and the "texture" resource.
  122. "Level/RigidBody2D/Sprite2D:texture"
  123. # Points to the Sprite2D node and its "position" property.
  124. "Level/RigidBody2D/Sprite2D:position"
  125. # Points to the Sprite2D node and the "x" component of its "position" property.
  126. "Level/RigidBody2D/Sprite2D:position:x"
  127. # Points to the RigidBody2D node as an absolute path beginning from the SceneTree.
  128. "/root/Level/RigidBody2D"
  129. \ **Note:** In GDScript, it's also possible to convert a constant string into a node path by prefixing it with ``^``. ``^"path/to/node"`` is equivalent to ``NodePath("path/to/node")``.
  130. .. rst-class:: classref-section-separator
  131. ----
  132. .. rst-class:: classref-descriptions-group
  133. Method Descriptions
  134. -------------------
  135. .. _class_NodePath_method_get_as_property_path:
  136. .. rst-class:: classref-method
  137. :ref:`NodePath<class_NodePath>` **get_as_property_path**\ (\ ) |const| :ref:`🔗<class_NodePath_method_get_as_property_path>`
  138. Returns a copy of this node path with a colon character (``:``) prefixed, transforming it to a pure property path with no node names (relative to the current node).
  139. .. tabs::
  140. .. code-tab:: gdscript
  141. # node_path points to the "x" property of the child node named "position".
  142. var node_path = ^"position:x"
  143. # property_path points to the "position" in the "x" axis of this node.
  144. var property_path = node_path.get_as_property_path()
  145. print(property_path) # Prints ":position:x"
  146. .. code-tab:: csharp
  147. // nodePath points to the "x" property of the child node named "position".
  148. var nodePath = new NodePath("position:x");
  149. // propertyPath points to the "position" in the "x" axis of this node.
  150. NodePath propertyPath = nodePath.GetAsPropertyPath();
  151. GD.Print(propertyPath); // Prints ":position:x".
  152. .. rst-class:: classref-item-separator
  153. ----
  154. .. _class_NodePath_method_get_concatenated_names:
  155. .. rst-class:: classref-method
  156. :ref:`StringName<class_StringName>` **get_concatenated_names**\ (\ ) |const| :ref:`🔗<class_NodePath_method_get_concatenated_names>`
  157. Returns all node names concatenated with a slash character (``/``) as a single :ref:`StringName<class_StringName>`.
  158. .. rst-class:: classref-item-separator
  159. ----
  160. .. _class_NodePath_method_get_concatenated_subnames:
  161. .. rst-class:: classref-method
  162. :ref:`StringName<class_StringName>` **get_concatenated_subnames**\ (\ ) |const| :ref:`🔗<class_NodePath_method_get_concatenated_subnames>`
  163. Returns all property subnames concatenated with a colon character (``:``) as a single :ref:`StringName<class_StringName>`.
  164. .. tabs::
  165. .. code-tab:: gdscript
  166. var node_path = ^"Sprite2D:texture:resource_name"
  167. print(node_path.get_concatenated_subnames()) # Prints "texture:resource_name".
  168. .. code-tab:: csharp
  169. var nodePath = new NodePath("Sprite2D:texture:resource_name");
  170. GD.Print(nodePath.GetConcatenatedSubnames()); // Prints "texture:resource_name".
  171. .. rst-class:: classref-item-separator
  172. ----
  173. .. _class_NodePath_method_get_name:
  174. .. rst-class:: classref-method
  175. :ref:`StringName<class_StringName>` **get_name**\ (\ idx\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_NodePath_method_get_name>`
  176. Returns the node name indicated by ``idx``, starting from 0. If ``idx`` is out of bounds, an error is generated. See also :ref:`get_subname_count<class_NodePath_method_get_subname_count>` and :ref:`get_name_count<class_NodePath_method_get_name_count>`.
  177. .. tabs::
  178. .. code-tab:: gdscript
  179. var sprite_path = NodePath("../RigidBody2D/Sprite2D")
  180. print(sprite_path.get_name(0)) # Prints "..".
  181. print(sprite_path.get_name(1)) # Prints "RigidBody2D".
  182. print(sprite_path.get_name(2)) # Prints "Sprite".
  183. .. code-tab:: csharp
  184. var spritePath = new NodePath("../RigidBody2D/Sprite2D");
  185. GD.Print(spritePath.GetName(0)); // Prints "..".
  186. GD.Print(spritePath.GetName(1)); // Prints "PathFollow2D".
  187. GD.Print(spritePath.GetName(2)); // Prints "Sprite".
  188. .. rst-class:: classref-item-separator
  189. ----
  190. .. _class_NodePath_method_get_name_count:
  191. .. rst-class:: classref-method
  192. :ref:`int<class_int>` **get_name_count**\ (\ ) |const| :ref:`🔗<class_NodePath_method_get_name_count>`
  193. Returns the number of node names in the path. Property subnames are not included.
  194. For example, ``"../RigidBody2D/Sprite2D:texture"`` contains 3 node names.
  195. .. rst-class:: classref-item-separator
  196. ----
  197. .. _class_NodePath_method_get_subname:
  198. .. rst-class:: classref-method
  199. :ref:`StringName<class_StringName>` **get_subname**\ (\ idx\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_NodePath_method_get_subname>`
  200. Returns the property name indicated by ``idx``, starting from 0. If ``idx`` is out of bounds, an error is generated. See also :ref:`get_subname_count<class_NodePath_method_get_subname_count>`.
  201. .. tabs::
  202. .. code-tab:: gdscript
  203. var path_to_name = NodePath("Sprite2D:texture:resource_name")
  204. print(path_to_name.get_subname(0)) # Prints "texture".
  205. print(path_to_name.get_subname(1)) # Prints "resource_name".
  206. .. code-tab:: csharp
  207. var pathToName = new NodePath("Sprite2D:texture:resource_name");
  208. GD.Print(pathToName.GetSubname(0)); // Prints "texture".
  209. GD.Print(pathToName.GetSubname(1)); // Prints "resource_name".
  210. .. rst-class:: classref-item-separator
  211. ----
  212. .. _class_NodePath_method_get_subname_count:
  213. .. rst-class:: classref-method
  214. :ref:`int<class_int>` **get_subname_count**\ (\ ) |const| :ref:`🔗<class_NodePath_method_get_subname_count>`
  215. Returns the number of property names ("subnames") in the path. Each subname in the node path is listed after a colon character (``:``).
  216. For example, ``"Level/RigidBody2D/Sprite2D:texture:resource_name"`` contains 2 subnames.
  217. .. rst-class:: classref-item-separator
  218. ----
  219. .. _class_NodePath_method_hash:
  220. .. rst-class:: classref-method
  221. :ref:`int<class_int>` **hash**\ (\ ) |const| :ref:`🔗<class_NodePath_method_hash>`
  222. Returns the 32-bit hash value representing the node path's contents.
  223. \ **Note:** Node paths with equal hash values are *not* guaranteed to be the same, as a result of hash collisions. Node paths with different hash values are guaranteed to be different.
  224. .. rst-class:: classref-item-separator
  225. ----
  226. .. _class_NodePath_method_is_absolute:
  227. .. rst-class:: classref-method
  228. :ref:`bool<class_bool>` **is_absolute**\ (\ ) |const| :ref:`🔗<class_NodePath_method_is_absolute>`
  229. Returns ``true`` if the node path is absolute. Unlike a relative path, an absolute path is represented by a leading slash character (``/``) and always begins from the :ref:`SceneTree<class_SceneTree>`. It can be used to reliably access nodes from the root node (e.g. ``"/root/Global"`` if an autoload named "Global" exists).
  230. .. rst-class:: classref-item-separator
  231. ----
  232. .. _class_NodePath_method_is_empty:
  233. .. rst-class:: classref-method
  234. :ref:`bool<class_bool>` **is_empty**\ (\ ) |const| :ref:`🔗<class_NodePath_method_is_empty>`
  235. Returns ``true`` if the node path has been constructed from an empty :ref:`String<class_String>` (``""``).
  236. .. rst-class:: classref-item-separator
  237. ----
  238. .. _class_NodePath_method_slice:
  239. .. rst-class:: classref-method
  240. :ref:`NodePath<class_NodePath>` **slice**\ (\ begin\: :ref:`int<class_int>`, end\: :ref:`int<class_int>` = 2147483647\ ) |const| :ref:`🔗<class_NodePath_method_slice>`
  241. Returns the slice of the **NodePath**, from ``begin`` (inclusive) to ``end`` (exclusive), as a new **NodePath**.
  242. The absolute value of ``begin`` and ``end`` will be clamped to the sum of :ref:`get_name_count<class_NodePath_method_get_name_count>` and :ref:`get_subname_count<class_NodePath_method_get_subname_count>`, so the default value for ``end`` makes it slice to the end of the **NodePath** by default (i.e. ``path.slice(1)`` is a shorthand for ``path.slice(1, path.get_name_count() + path.get_subname_count())``).
  243. If either ``begin`` or ``end`` are negative, they will be relative to the end of the **NodePath** (i.e. ``path.slice(0, -2)`` is a shorthand for ``path.slice(0, path.get_name_count() + path.get_subname_count() - 2)``).
  244. .. rst-class:: classref-section-separator
  245. ----
  246. .. rst-class:: classref-descriptions-group
  247. Operator Descriptions
  248. ---------------------
  249. .. _class_NodePath_operator_neq_NodePath:
  250. .. rst-class:: classref-operator
  251. :ref:`bool<class_bool>` **operator !=**\ (\ right\: :ref:`NodePath<class_NodePath>`\ ) :ref:`🔗<class_NodePath_operator_neq_NodePath>`
  252. Returns ``true`` if two node paths are not equal.
  253. .. rst-class:: classref-item-separator
  254. ----
  255. .. _class_NodePath_operator_eq_NodePath:
  256. .. rst-class:: classref-operator
  257. :ref:`bool<class_bool>` **operator ==**\ (\ right\: :ref:`NodePath<class_NodePath>`\ ) :ref:`🔗<class_NodePath_operator_eq_NodePath>`
  258. Returns ``true`` if two node paths are equal, that is, they are composed of the same node names and subnames in the same order.
  259. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  260. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  261. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  262. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  263. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  264. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  265. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  266. .. |void| replace:: :abbr:`void (No return value.)`