Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/string/node_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,27 @@ NodePath NodePath::simplified() const {
return np;
}

NodePath NodePath::from_string_name(const StringName &p_string_name) {
const int size = p_string_name.length();
if (size == 0) {
return NodePath();
}
// Check if p_string_name contains a slash or colon. If so, we need to parse it.
const char32_t *chars = p_string_name.get_data();
for (int i = 0; i < size; i++) {
if (unlikely(chars[i] == '/' || chars[i] == ':')) {
return NodePath(String(p_string_name));
}
}
// If there is no colon or slash, the desired NodePath has one StringName.
// Therefore we can avoid all the String parsing and StringName re-creation.
Vector<StringName> path;
path.push_back(p_string_name);
NodePath ret = NodePath(path, false);
ret.data->concatenated_path = p_string_name;
return ret;
}

NodePath::NodePath(const Vector<StringName> &p_path, bool p_absolute) {
if (p_path.is_empty() && !p_absolute) {
return;
Expand Down
2 changes: 2 additions & 0 deletions core/string/node_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ class [[nodiscard]] NodePath {
void simplify();
NodePath simplified() const;

static NodePath from_string_name(const StringName &p_string_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do like factory methods more than constructors, but we currently don't do this in the codebase much.
Perhaps we should go over this idea quickly in a core meeting to run it through the rest of the core team.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't? I thought we do. The existing cases in the codebase (will be helpful information during the meeting):

ArgumentDoc::from_dict
Basis::from_euler
Basis::from_scale
Color::from_hsv
Color::from_ok_hsl
Color::from_ok_hsv
Color::from_rgba8
Color::from_rgbe9995
Color::from_string
ConstantDoc::from_dict
DocData::ClassDoc::from_dict
EnumDoc::from_dict
GLTFCamera::from_dictionary
GLTFCamera::from_node
GLTFLight::from_dictionary
GLTFLight::from_node
GLTFPhysicsBody::from_dictionary
GLTFPhysicsBody::from_node
GLTFPhysicsShape::from_dictionary
GLTFPhysicsShape::from_node
GLTFPhysicsShape::from_resource
GLTFSkin::from_dictionary
GodotPosition::from_lsp
MethodDoc::from_dict
MethodInfo::from_dict
PropertyDoc::from_dict
PropertyInfo::from_dict
PropertyTweener::from_current
Quaternion::from_euler
RID::from_uint64
ScriptClassInfoUpdate::from_file_info
ThemeItemDoc::from_dict
TutorialDoc::from_dict
Vector2::from_angle

Though, most of the GLTF ones came from me, so I'm biased. 😛


NodePath(const Vector<StringName> &p_path, bool p_absolute);
NodePath(const Vector<StringName> &p_path, const Vector<StringName> &p_subpath, bool p_absolute);
NodePath(const NodePath &p_path);
Expand Down
1 change: 1 addition & 0 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2283,6 +2283,7 @@ static void _register_variant_builtin_methods_misc() {
bind_method(NodePath, slice, sarray("begin", "end"), varray(INT_MAX));
bind_method(NodePath, get_as_property_path, sarray(), varray());
bind_method(NodePath, is_empty, sarray(), varray());
bind_static_method(NodePath, from_string_name, sarray("string_name"), varray());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need to expose this function. From GDScript anyway the performance benefits are probably not that important (unless you want to test this?).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API available through GDScript is the same as what's available through GDExtension. Also, I don't see a reason to prevent GDScript from using this method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's often brought up that we want to keep the GDScript API surface small and simple.
GDExtension is a good point. Personally I'd love to be able to expose methods to just GDExtension (and not GDScript when it's not useful there), but this is currently impossible, I suppose.


/* Callable */

Expand Down
7 changes: 7 additions & 0 deletions doc/classes/NodePath.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@
</constructor>
</constructors>
<methods>
<method name="from_string_name" qualifiers="static">
<return type="NodePath" />
<param index="0" name="string_name" 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>
</method>
Comment on lines +84 to +90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion, it makes no sense to expose identical constructor and static method. We can't make constructor in core for technical reasons, but in bindings we can choose what we will expose. However, I wonder if we will get the same problem with constructor in godot-cpp?

Copy link
Member Author

@aaronfranke aaronfranke Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the static method is clearer, safer, and ensures we have a consistent API between C++ and GDScript. If both is not desired, then I will remove the constructor that you suggested (the second commit in this PR - glad I kept it separate 😛). EDIT: Removed, I have it saved locally if we need it back though.

<method name="get_as_property_path" qualifiers="const">
<return type="NodePath" />
<description>
Expand Down
69 changes: 69 additions & 0 deletions tests/core/string/test_node_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,75 @@ TEST_CASE("[NodePath] Empty path") {
"The node path should be considered empty.");
}

TEST_CASE("[NodePath] From StringName") {
const StringName sname = StringName("Path2D");

// Test the case where the StringName has no slash or colon and uses the StringName directly.
const NodePath node_path_sname = NodePath::from_string_name(sname);
const NodePath node_path_string = NodePath("Path2D");

CHECK_MESSAGE(
!node_path_sname.is_absolute(),
"The StringName version should not be absolute.");

CHECK_MESSAGE(
!node_path_string.is_absolute(),
"The String version should not be absolute.");

CHECK_MESSAGE(
node_path_sname.get_name(0) == sname,
"The StringName version should have the expected StringName as the first name.");

CHECK_MESSAGE(
node_path_string.get_name(0) == sname,
"The String version should have the expected StringName as the first name.");

CHECK_MESSAGE(
node_path_sname.get_concatenated_names() == sname,
"The StringName version should give the expected StringName.");

CHECK_MESSAGE(
node_path_string.get_concatenated_names() == sname,
"The String version should give the expected StringName.");

CHECK_MESSAGE(
node_path_string.get_concatenated_names() == node_path_sname.get_concatenated_names(),
"The String and StringName versions should give the same result.");

// Test the case where the StringName has a slash or colon and uses String parsing.
const NodePath node_path_abs_sname = NodePath::from_string_name("/Path2D");
const NodePath node_path_abs_string = NodePath("/Path2D");

CHECK_MESSAGE(
node_path_abs_sname.is_absolute(),
"The StringName version should be absolute.");

CHECK_MESSAGE(
node_path_abs_string.is_absolute(),
"The String version should be absolute.");

CHECK_MESSAGE(
node_path_abs_sname.get_name(0) == sname,
"The StringName version should have the expected StringName as the first name.");

CHECK_MESSAGE(
node_path_abs_string.get_name(0) == sname,
"The String version should have the expected StringName as the first name.");

// Absolute paths do not include a leading slash in the concatenated names.
CHECK_MESSAGE(
node_path_abs_sname.get_concatenated_names() == sname,
"The StringName version should give the expected StringName.");

CHECK_MESSAGE(
node_path_abs_string.get_concatenated_names() == sname,
"The String version should give the expected StringName.");

CHECK_MESSAGE(
node_path_abs_string.get_concatenated_names() == node_path_abs_sname.get_concatenated_names(),
"The String and StringName versions should give the same result.");
}

TEST_CASE("[NodePath] Slice") {
const NodePath node_path_relative = NodePath("Parent/Child:prop:subprop");
const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop");
Expand Down