Skip to content

EntityPath

A pre-parsed scene tree path.

The EntityPath 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 Entity.get_entity(), Object.set_indexed(), 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 (/) 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.

The following examples are paths relative to the current node:

A leading slash means the path is absolute, and begins from the Stage:

Despite their name, node paths may also point to a property:

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 Object.set_indexed() and Tween.tween_property(), as those methods call get_as_property_path() under the hood. However, it’s generally recommended to keep the : prefix.

Entity 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 EntityPath type, as strings are automatically converted to the type when necessary. There are still times when defining node paths is useful. For example, exported EntityPath 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 @export_node_path.

See also StringName, which is a similar type designed for optimized strings.

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

Returns Method
EntityPath get_as_property_path() const
StringName get_concatenated_names() const
StringName get_concatenated_subnames() const
StringName get_name(idx: int) const
int get_name_count() const
StringName get_subname(idx: int) const
int get_subname_count() const
int hash() const
bool is_absolute() const
bool is_empty() const
EntityPath slice(begin: int, end: int = 2147483647) const

EntityPath EntityPath()

Constructs an empty EntityPath.

EntityPath EntityPath(from: EntityPath)

Constructs a EntityPath as a copy of the given EntityPath.

EntityPath EntityPath(from: String)

Constructs a EntityPath from a String. The created path is absolute if prefixed with a slash (see is_absolute()).

The “subnames” optionally included after the path to the target node can point to properties, and can also be nested.

The following strings can be valid node paths:

bool operator !=(right: EntityPath)

Returns true if two node paths are not equal.

bool operator ==(right: EntityPath)

Returns true if two node paths are equal, that is, they are composed of the same node names and subnames in the same order.

get_as_property_path

EntityPath get_as_property_path() const

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).

// nodePath points to the "x" property of the child node named "position".
var nodePath = new EntityPath("position:x");
// propertyPath points to the "position" in the "x" axis of this node.
EntityPath propertyPath = nodePath.GetAsPropertyPath();
Orb.Print(propertyPath); // Prints ":position:x"

get_concatenated_names

StringName get_concatenated_names() const

Returns all node names concatenated with a slash character (/) as a single StringName.

get_concatenated_subnames

StringName get_concatenated_subnames() const

Returns all property subnames concatenated with a colon character (:) as a single StringName.

var nodePath = new EntityPath("Sprite2D:texture:resource_name");
Orb.Print(nodePath.GetConcatenatedSubnames()); // Prints "texture:resource_name"

get_name

StringName get_name(idx: int) const

Returns the node name indicated by idx, starting from 0. If idx is out of bounds, an error is generated. See also get_subname_count() and get_name_count().

var spritePath = new EntityPath("../RigidBody2D/Sprite2D");
Orb.Print(spritePath.GetName(0)); // Prints ".."
Orb.Print(spritePath.GetName(1)); // Prints "PathFollow2D"
Orb.Print(spritePath.GetName(2)); // Prints "Sprite"

get_name_count

int get_name_count() const

Returns the number of node names in the path. Property subnames are not included.

For example, "../RigidBody2D/Sprite2D:texture" contains 3 node names.

get_subname

StringName get_subname(idx: int) const

Returns the property name indicated by idx, starting from 0. If idx is out of bounds, an error is generated. See also get_subname_count().

var pathToName = new EntityPath("Sprite2D:texture:resource_name");
Orb.Print(pathToName.GetSubname(0)); // Prints "texture"
Orb.Print(pathToName.GetSubname(1)); // Prints "resource_name"

get_subname_count

int get_subname_count() const

Returns the number of property names (“subnames”) in the path. Each subname in the node path is listed after a colon character (:).

For example, "Level/RigidBody2D/Sprite2D:texture:resource_name" contains 2 subnames.

hash

int hash() const

Returns the 32-bit hash value representing the node path’s contents.

Note: Entity paths with equal hash values are not guaranteed to be the same, as a result of hash collisions. Entity paths with different hash values are guaranteed to be different.

is_absolute

bool is_absolute() const

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 Stage. It can be used to reliably access nodes from the root node (e.g. "/root/Global" if an autoload named “Global” exists).

is_empty

bool is_empty() const

Returns true if the node path has been constructed from an empty String ("").

slice

EntityPath slice(begin: int, end: int = 2147483647) const

Returns the slice of the EntityPath, from begin (inclusive) to end (exclusive), as a new EntityPath.

The absolute value of begin and end will be clamped to the sum of get_name_count() and get_subname_count(), so the default value for end makes it slice to the end of the EntityPath by default (i.e. path.slice(1) is a shorthand for path.slice(1, path.get_name_count() + path.get_subname_count())).

If either begin or end are negative, they will be relative to the end of the EntityPath (i.e. path.slice(0, -2) is a shorthand for path.slice(0, path.get_name_count() + path.get_subname_count() - 2)).