Skip to content

Commit

Permalink
Add a constructor in GDScript for NodePath from StringName
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronfranke committed Jun 26, 2024
1 parent c925b10 commit c82bfb7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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
};

Expand Down Expand Up @@ -2103,6 +2105,8 @@ Variant::operator NodePath() const {
return *reinterpret_cast<const NodePath *>(_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();
}
Expand Down
1 change: 1 addition & 0 deletions core/variant/variant_construct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ void Variant::_register_variant_constructors() {
add_constructor<VariantConstructNoArgs<NodePath>>(sarray());
add_constructor<VariantConstructor<NodePath, NodePath>>(sarray("from"));
add_constructor<VariantConstructor<NodePath, String>>(sarray("from"));
add_constructor<VariantConstructor<NodePath, StringName>>(sarray("from"));

add_constructor<VariantConstructNoArgs<::RID>>(sarray());
add_constructor<VariantConstructor<::RID, ::RID>>(sarray("from"));
Expand Down
7 changes: 7 additions & 0 deletions doc/classes/NodePath.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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].
</description>
</constructor>
<constructor name="NodePath">
<return type="NodePath" />
<param index="0" name="from" type="StringName" />
<description>
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.
</description>
</constructor>
</constructors>
<methods>
<method name="from_string_name" qualifiers="static">
Expand Down

0 comments on commit c82bfb7

Please sign in to comment.