From bf0112f4a5eb54cf0383f0e753ac197a71ae7dcb Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Thu, 22 Jun 2023 14:56:26 -0500 Subject: [PATCH] Add a constructor in GDScript for NodePath from StringName --- core/variant/variant.cpp | 4 ++++ core/variant/variant_construct.cpp | 1 + doc/classes/NodePath.xml | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index 24b30112bd7a..4507fdd7c6e5 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -390,6 +390,7 @@ bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) { case NODE_PATH: { static const Type valid[] = { STRING, + STRING_NAME, NIL }; @@ -733,6 +734,7 @@ bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type case NODE_PATH: { static const Type valid[] = { STRING, + STRING_NAME, NIL }; @@ -2096,6 +2098,8 @@ Variant::operator NodePath() const { return *reinterpret_cast(_data._mem); } else if (type == STRING) { return NodePath(operator String()); + } else if (type == STRING_NAME) { + return NodePath::from_string_name(operator StringName()); } else { return NodePath(); } diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp index 1edae407c249..4562e189ae85 100644 --- a/core/variant/variant_construct.cpp +++ b/core/variant/variant_construct.cpp @@ -180,6 +180,7 @@ void Variant::_register_variant_constructors() { add_constructor>(sarray()); add_constructor>(sarray("from")); add_constructor>(sarray("from")); + add_constructor>(sarray("from")); add_constructor>(sarray()); add_constructor>(sarray("from")); diff --git a/doc/classes/NodePath.xml b/doc/classes/NodePath.xml index 952b89e30d14..2f05365fed6c 100644 --- a/doc/classes/NodePath.xml +++ b/doc/classes/NodePath.xml @@ -79,6 +79,13 @@ [b]Note:[/b] In GDScript, it's also possible to convert a constant string into a node path by prefixing it with [code]^[/code]. [code]^"path/to/node"[/code] is equivalent to [code]NodePath("path/to/node")[/code]. + + + + + Constructs a NodePath from a [StringName]. This method will be faster than constructing from a String if the StringName does not contain any slashes or colons. + +