Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

NodePath

预先解析的场景树路径。

Description

A pre-parsed relative or absolute path in a scene tree, for use with 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, "Path2D/PathFollow2D/Sprite2D:texture:size" would refer to the size property of the texture resource on the node named "Sprite2D", which is a child of the other named nodes in the path.

You will usually just pass a string to 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 ^"path". Exporting a NodePath variable will give you a node selection widget in the properties panel of the editor, which can often be useful.

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.

Some examples of NodePaths include the following:

# No leading slash means it is relative to the current node.
^"A" # Immediate child A
^"A/B" # A's child B
^"." # The current node.
^".." # The parent node.
^"../C" # A sibling node C.
^"../.." # The grandparent node.
# A leading slash means it is absolute from the SceneTree.
^"/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.

See also StringName, which is a similar concept for general-purpose string interning.

Note: 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.

Note: In a boolean context, a NodePath will evaluate to false if it is empty (NodePath("")). Otherwise, a NodePath will always evaluate to true.

备注

There are notable differences when using this API with C#. See C# API 与 GDScript 的差异 for more information.

Tutorials

Constructors

NodePath

NodePath ( )

NodePath

NodePath ( NodePath from )

NodePath

NodePath ( String from )

Methods

NodePath

get_as_property_path ( ) const

StringName

get_concatenated_names ( ) const

StringName

get_concatenated_subnames ( ) const

StringName

get_name ( int idx ) const

int

get_name_count ( ) const

StringName

get_subname ( int idx ) const

int

get_subname_count ( ) const

int

hash ( ) const

bool

is_absolute ( ) const

bool

is_empty ( ) const

Operators

bool

operator != ( NodePath right )

bool

operator == ( NodePath right )


Constructor Descriptions

NodePath NodePath ( )

构造空的 NodePath


NodePath NodePath ( NodePath from )

构造给定 NodePath 的副本。NodePath("example") 等价于 ^"example"


NodePath NodePath ( String from )

从一个字符串,例如 "Path2D/PathFollow2D/Sprite2D:texture:size",创建一个 NodePath。如果路径以斜杠开头,则该路径是绝对路径。绝对路径仅在全局场景树中有效,在单个场景中无效。在相对路径中,"."".." 表示当前节点及其父节点。

在到目标节点的路径后可以选择包含“子名称”,它可以指向资源或属性,也可以被嵌套。

有效 NodePath 的示例(假设这些节点存在,并具有引用的资源或属性):

# 指向 Sprite2D 节点。
"Path2D/PathFollow2D/Sprite2D"
# 指向 Sprite2D 节点及其“纹理(texture)”资源。
# get_node() 将检索“Sprite2D”,而 get_node_and_resource()
# 将同时检索该 Sprite2D 节点和其“纹理(texture)”资源。
"Path2D/PathFollow2D/Sprite2D:texture"
# 指向 Sprite2D 节点及其“位置(position)”属性。
"Path2D/PathFollow2D/Sprite2D:position"
# 指向 Sprite2D 节点及其“位置(position)”属性的“x”分量。
"Path2D/PathFollow2D/Sprite2D:position:x"
# 绝对路径(从 “root” 开始)
/root/Level/Path2D

Method Descriptions

NodePath get_as_property_path ( ) const

返回前面带有冒号字符(:)的节点路径,将其转换为没有节点名称的纯属性路径(默认为从当前节点解析)。

# 这将被解析为一个到“position”节点中“x”属性的节点路径。
var node_path = NodePath("position:x")
# 这将被解析为一个到当前节点中“position”属性的“x”分量的节点路径。
var property_path = node_path.get_as_property_path()
print(property_path) # :position:x

StringName get_concatenated_names ( ) const

返回所有以斜杠字符(/)作为分隔符连接的且不带子名称的路径。


StringName get_concatenated_subnames ( )