Class NodePath
- Namespace
- Godot
- Assembly
- GodotSharp.dll
A pre-parsed relative or absolute path in a scene tree,
for use with GetNode(NodePath) and similar functions.
It can reference a node, a resource within a node, or a property
of a node or resource.
For instance, "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 GetNode(NodePath)
and it will be automatically converted, but you may occasionally
want to parse a path ahead of time with NodePath.
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.
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.
public sealed class NodePath : IDisposable, IEquatable<NodePath>
- Inheritance
-
NodePath
- Implements
- Inherited Members
Examples
Some examples of NodePaths include the following:
// No leading slash means it is relative to the current node.
new NodePath("A"); // Immediate child A.
new NodePath("A/B"); // A's child B.
new NodePath("."); // The current node.
new NodePath(".."); // The parent node.
new NodePath("../C"); // A sibling node C.
// A leading slash means it is absolute from the SceneTree.
new NodePath("/root"); // Equivalent to GetTree().Root
new NodePath("/root/Main"); // If your main scene's root node were named "Main".
new NodePath("/root/MyAutoload"); // If you have an autoloaded node or scene.
Constructors
NodePath()
Constructs an empty NodePath.
public NodePath()
NodePath(string)
Constructs a NodePath from a string path
,
e.g.: "Path2D/PathFollow2D/Sprite2D:texture:size"
.
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, "."
and ".."
indicate
the current node and its parent.
The "subnames" optionally included after the path to the target
node can point to resources or properties, and can also be nested.
public NodePath(string path)
Parameters
path
stringA string that represents a path in a scene tree.
Examples
Examples of valid NodePaths (assuming that those nodes exist and have the referenced resources or properties):
// Points to the Sprite2D node.
"Path2D/PathFollow2D/Sprite2D"
// Points to the Sprite2D node and its "texture" resource.
// GetNode() would retrieve "Sprite2D", while GetNodeAndResource()
// 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"
// Absolute path (from "root")
"/root/Level/Path2D"
Properties
IsEmpty
Returns true if the node path is empty.
public bool IsEmpty { get; }
Property Value
Methods
Dispose()
Disposes of this NodePath.
public void Dispose()
Dispose(bool)
public void Dispose(bool disposing)
Parameters
disposing
bool
Equals(NodePath)
public bool Equals(NodePath other)
Parameters
other
NodePath
Returns
Equals(object)
public override bool Equals(object obj)
Parameters
obj
object
Returns
~NodePath()
protected ~NodePath()
GetAsPropertyPath()
Returns a node path with a colon character (:
) prepended,
transforming it to a pure property path with no node name (defaults
to resolving from the current node).
public NodePath GetAsPropertyPath()
Returns
Examples
// 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
GetConcatenatedNames()
Returns all names concatenated with a slash character (/
).
public string GetConcatenatedNames()
Returns
- string
The names concatenated with
/
.
Examples
var nodepath = new NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path");
GD.Print(nodepath.GetConcatenatedNames()); // Path2D/PathFollow2D/Sprite2D
GetConcatenatedSubNames()
Returns all subnames concatenated with a colon character (:
)
as separator, i.e. the right side of the first colon in a node path.
public string GetConcatenatedSubNames()
Returns
- string
The subnames concatenated with
:
.
Examples
var nodepath = new NodePath("Path2D/PathFollow2D/Sprite2D:texture:load_path");
GD.Print(nodepath.GetConcatenatedSubnames()); // texture:load_path
GetHashCode()
public override int GetHashCode()
Returns
GetName(int)
Gets the node name indicated by idx
(0 to GetNameCount()).
public string GetName(int idx)
Parameters
idx
intThe name index.
Returns
- string
The name at the given index
idx
.
Examples
var nodePath = new NodePath("Path2D/PathFollow2D/Sprite2D");
GD.Print(nodePath.GetName(0)); // Path2D
GD.Print(nodePath.GetName(1)); // PathFollow2D
GD.Print(nodePath.GetName(2)); // Sprite
GetNameCount()
Gets the number of node names which make up the path.
Subnames (see GetSubNameCount()) are not included.
For example, "Path2D/PathFollow2D/Sprite2D"
has 3 names.
public int GetNameCount()
Returns
- int
The number of node names which make up the path.
GetSubName(int)
Gets the resource or property name indicated by idx
(0 to GetSubNameCount()).
public string GetSubName(int idx)
Parameters
idx
intThe subname index.
Returns
- string
The subname at the given index
idx
.
GetSubNameCount()
Gets the number of resource or property names ("subnames") in the path.
Each subname is listed after a colon character (:
) in the node path.
For example, "Path2D/PathFollow2D/Sprite2D:texture:load_path"
has 2 subnames.
public int GetSubNameCount()
Returns
- int
The number of subnames in the path.
IsAbsolute()
Returns true if the node path is absolute (as opposed to relative),
which means that it starts with a slash character (/
). Absolute node paths can
be used to access the root node ("/root"
) or autoloads (e.g. "/global"
if a "global" autoload was registered).
public bool IsAbsolute()
Returns
ToString()
Converts this NodePath to a string.
public override string ToString()
Returns
Operators
operator ==(NodePath, NodePath)
public static bool operator ==(NodePath left, NodePath right)
Parameters
Returns
implicit operator string(NodePath)
Converts this NodePath to a string.
public static implicit operator string(NodePath from)
Parameters
Returns
implicit operator NodePath(string)
Converts a string to a NodePath.
public static implicit operator NodePath(string from)
Parameters
from
stringThe string to convert.
Returns
operator !=(NodePath, NodePath)
public static bool operator !=(NodePath left, NodePath right)