NodePath
A pre-parsed scene tree path.
NodePath.swift:31class NodePath
The NodePath
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 getNode(path:)
, setIndexed(propertyPath:value:)
, tweenProperty(object:property:finalVal:duration:)
, 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 SceneTree
:
Despite their name, node paths may also point to a property:
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.
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 [annotation @GDScript.@export_node_path].
See also StringName
, which is a similar type designed for optimized strings.
Citizens in SwiftGodot
Conformances
protocol ContentVariantRepresentable
Some of Godot’s builtin classes use ContentType for storage. This needs to be public because it affects their initialization, but SwiftGodot users should never need to conform their types to
ContentVariantRepresentable
.protocol Copyable
A type whose values can be implicitly or explicitly copied.
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Escapable
protocol ExpressibleByExtendedGraphemeClusterLiteral
A type that can be initialized with a string literal containing a single extended grapheme cluster.
protocol ExpressibleByStringInterpolation
A type that can be initialized by string interpolation with a string literal that includes expressions.
protocol ExpressibleByStringLiteral
A type that can be initialized with a string literal.
protocol ExpressibleByUnicodeScalarLiteral
A type that can be initialized with a string literal containing a single Unicode scalar value.
protocol LosslessStringConvertible
A type that can be represented as a string in a lossless, unambiguous way.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Typealiases
Type members
init(
) Constructs an empty
NodePath
.init(String
) LosslessStringConvertible conformance
init(alreadyOwnedContent: ContentType
) init(content: ContentType
) init(from: NodePath
) init(from: String
) Constructs a
NodePath
from aString
. The created path is absolute if prefixed with a slash (seeisAbsolute
).init(stringLiteral: String
) ExpressibleByStringLiteral conformance
static var godotType: Variant.GType
static let zero: ContentType
static func != (lhs: NodePath, rhs: NodePath
) -> Bool Returns
true
if two node paths are not equal.static func == (lhs: NodePath, rhs: NodePath
) -> Bool Returns
true
if two node paths are equal, that is, they are composed of the same node names and subnames in the same order.
Instance members
var content: ContentType
var description: String
Produces a string representation of this NodePath
func getAsPropertyPath(
) -> NodePath 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).func getConcatenatedNames(
) -> StringName Returns all node names concatenated with a slash character (
/
) as a singleStringName
.func getConcatenatedSubnames(
) -> StringName Returns all property subnames concatenated with a colon character (
:
) as a singleStringName
.func getName(idx: Int64
) -> StringName Returns the node name indicated by
idx
, starting from 0. Ifidx
is out of bounds, an error is generated. See alsogetSubnameCount
andgetNameCount
.func getNameCount(
) -> Int64 Returns the number of node names in the path. Property subnames are not included.
func getSubname(idx: Int64
) -> StringName Returns the property name indicated by
idx
, starting from 0. Ifidx
is out of bounds, an error is generated. See alsogetSubnameCount
.func getSubnameCount(
) -> Int64 Returns the number of property names (“subnames”) in the path. Each subname in the node path is listed after a colon character (
:
).func hash(
) -> Int64 Returns the 32-bit hash value representing the node path’s contents.
func isAbsolute(
) -> Bool 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 theSceneTree
. It can be used to reliably access nodes from the root node (e.g."/root/Global"
if an autoload named “Global” exists).func isEmpty(
) -> Bool Returns
true
if the node path has been constructed from an emptyString
(""
).func slice(begin: Int64, end: Int64
) -> NodePath Returns the slice of the
NodePath
, frombegin
(inclusive) toend
(exclusive), as a newNodePath
.
Type features
init?(Variant
) init(extendedGraphemeClusterLiteral: Self.StringLiteralType
) init(stringInterpolation: DefaultStringInterpolation
) Creates a new instance from an interpolated string literal.
init(unicodeScalarLiteral: Self.ExtendedGraphemeClusterLiteralType
) static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant.
static func makeOrUnwrap(Variant
) -> Self? Unwraps an object from a variant. This is useful when you want one method to call that will return the unwrapped Variant, regardless of whether it is a SwiftGodot.Object or not.
static func != (lhs: Self, rhs: Self
) -> Bool Returns a Boolean value indicating whether two values are not equal.