Skip to content

Commit acf38b2

Browse files
committed
Merge pull request #76560 from aaronfranke/node-set-string-name
Change Node `set_name` to use StringName, slightly improves performance
2 parents bec3856 + a404b66 commit acf38b2

File tree

6 files changed

+69
-17
lines changed

6 files changed

+69
-17
lines changed

misc/extension_api_validation/4.4-stable.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,10 @@ Validate extension JSON: API was removed: classes/RenderingServer/methods/instan
9090
Validate extension JSON: API was removed: classes/RenderingServer/methods/instance_reset_physics_interpolation
9191

9292
Functionality moved out of server.
93+
94+
95+
GH-76560
96+
--------
97+
Validate extension JSON: Error: Field 'classes/Node/methods/set_name/arguments/0': type changed value in new API, from "String" to "StringName".
98+
99+
Change Node `set_name` to use StringName to improve performance. Compatibility method registered.

modules/mono/editor/bindings_generator.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,11 +2732,7 @@ Error BindingsGenerator::_generate_cs_property(const BindingsGenerator::TypeInte
27322732
if (getter && setter) {
27332733
const ArgumentInterface &setter_first_arg = setter->arguments.back()->get();
27342734
if (getter->return_type.cname != setter_first_arg.type.cname) {
2735-
// Special case for Node::set_name
2736-
bool whitelisted = getter->return_type.cname == name_cache.type_StringName &&
2737-
setter_first_arg.type.cname == name_cache.type_String;
2738-
2739-
ERR_FAIL_COND_V_MSG(!whitelisted, ERR_BUG,
2735+
ERR_FAIL_V_MSG(ERR_BUG,
27402736
"Return type from getter doesn't match first argument of setter for property: '" +
27412737
p_itype.name + "." + String(p_iprop.cname) + "'.");
27422738
}

scene/main/node.compat.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************/
2+
/* node.compat.inc */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
void Node::_set_name_bind_compat_76560(const String &p_name) {
34+
set_name(p_name);
35+
}
36+
37+
void Node::_bind_compatibility_methods() {
38+
ClassDB::bind_compatibility_method(D_METHOD("set_name", "name"), &Node::_set_name_bind_compat_76560);
39+
}
40+
41+
#endif

scene/main/node.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "node.h"
32+
#include "node.compat.inc"
3233

3334
#include "core/config/project_settings.h"
3435
#include "core/io/resource_loader.h"
@@ -1552,17 +1553,23 @@ void Node::_set_name_nocheck(const StringName &p_name) {
15521553
data.name = p_name;
15531554
}
15541555

1555-
void Node::set_name(const String &p_name) {
1556+
void Node::set_name(const StringName &p_name) {
15561557
ERR_FAIL_COND_MSG(data.inside_tree && !Thread::is_main_thread(), "Changing the name to nodes inside the SceneTree is only allowed from the main thread. Use `set_name.call_deferred(new_name)`.");
1557-
String name = p_name.validate_node_name();
1558-
1559-
ERR_FAIL_COND(name.is_empty());
1558+
const StringName old_name = data.name;
1559+
{
1560+
const String input_name_str = String(p_name);
1561+
ERR_FAIL_COND(input_name_str.is_empty());
1562+
const String validated_node_name_string = input_name_str.validate_node_name();
1563+
if (input_name_str == validated_node_name_string) {
1564+
data.name = p_name;
1565+
} else {
1566+
data.name = StringName(validated_node_name_string);
1567+
}
1568+
}
15601569

15611570
if (data.unique_name_in_owner && data.owner) {
15621571
_release_unique_name_in_owner();
15631572
}
1564-
String old_name = data.name;
1565-
data.name = name;
15661573

15671574
if (data.parent) {
15681575
data.parent->_validate_child_name(this, true);

scene/main/node.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,11 @@ class Node : public Object {
410410
GDVIRTUAL0RC(RID, _get_focused_accessibility_element)
411411
GDVIRTUAL1RC(String, _get_accessibility_container_name, const Node *)
412412

413+
#ifndef DISABLE_DEPRECATED
414+
void _set_name_bind_compat_76560(const String &p_name);
415+
static void _bind_compatibility_methods();
416+
#endif
417+
413418
public:
414419
enum {
415420
// You can make your own, but don't use the same numbers as other notifications in other nodes.
@@ -473,7 +478,7 @@ class Node : public Object {
473478

474479
StringName get_name() const;
475480
String get_description() const;
476-
void set_name(const String &p_name);
481+
void set_name(const StringName &p_name);
477482

478483
InternalMode get_internal_mode() const;
479484

tests/core/object/test_class_db.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,7 @@ void validate_property(const Context &p_context, const ExposedClass &p_class, co
359359
if (getter && setter) {
360360
const ArgumentData &setter_first_arg = setter->arguments.back()->get();
361361
if (getter->return_type.name != setter_first_arg.type.name) {
362-
// Special case for Node::set_name
363-
bool whitelisted = getter->return_type.name == p_context.names_cache.string_name_type &&
364-
setter_first_arg.type.name == p_context.names_cache.string_type;
365-
366-
TEST_FAIL_COND(!whitelisted,
362+
TEST_FAIL(
367363
"Return type from getter doesn't match first argument of setter, for property: '", p_class.name, ".", String(p_prop.name), "'.");
368364
}
369365
}

0 commit comments

Comments
 (0)