summaryrefslogtreecommitdiff
path: root/core/string
diff options
context:
space:
mode:
authorreduz <reduzio@gmail.com>2022-04-16 12:23:32 +0200
committerreduz <reduzio@gmail.com>2022-04-25 12:19:17 +0200
commit8580f377a3d7d72c319e9e5489bcbe1678ad704b (patch)
tree10f6ac97697013f8c0e5ef9f3004498b0e33aaec /core/string
parentf4b0c7a1ea8d86c1dfd96478ca12ad1360903d9d (diff)
Implement Scene Unique Nodes
Implements https://github.com/godotengine/godot-proposals/issues/4096 * Nodes can be marked unique to the scene in the editor (or via code). * Unique nodes can be accessed via the **%** prefix at any point in the path. From that point in the path (depending on whether the scene of the path is), the unique node will be fetched. * Implementation is very optimal, as these nodes are cached.
Diffstat (limited to 'core/string')
-rw-r--r--core/string/string_name.cpp1
-rw-r--r--core/string/string_name.h13
-rw-r--r--core/string/ustring.cpp3
3 files changed, 16 insertions, 1 deletions
diff --git a/core/string/string_name.cpp b/core/string/string_name.cpp
index 2e941b8037..9c4fc4e1b7 100644
--- a/core/string/string_name.cpp
+++ b/core/string/string_name.cpp
@@ -247,6 +247,7 @@ StringName::StringName(const char *p_name, bool p_static) {
_data->cname = nullptr;
_data->next = _table[idx];
_data->prev = nullptr;
+
#ifdef DEBUG_ENABLED
if (unlikely(debug_stringname)) {
// Keep in memory, force static.
diff --git a/core/string/string_name.h b/core/string/string_name.h
index f4233854ac..ff4c41af94 100644
--- a/core/string/string_name.h
+++ b/core/string/string_name.h
@@ -35,6 +35,8 @@
#include "core/string/ustring.h"
#include "core/templates/safe_refcount.h"
+#define UNIQUE_NODE_PREFIX "%"
+
class Main;
struct StaticCString {
@@ -100,6 +102,17 @@ public:
bool operator==(const String &p_name) const;
bool operator==(const char *p_name) const;
bool operator!=(const String &p_name) const;
+
+ _FORCE_INLINE_ bool is_node_unique_name() const {
+ if (!_data) {
+ return false;
+ }
+ if (_data->cname != nullptr) {
+ return (char32_t)_data->cname[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
+ } else {
+ return (char32_t)_data->name[0] == (char32_t)UNIQUE_NODE_PREFIX[0];
+ }
+ }
_FORCE_INLINE_ bool operator<(const StringName &p_name) const {
return _data < p_name._data;
}
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp
index 7cfd34b53e..7199121932 100644
--- a/core/string/ustring.cpp
+++ b/core/string/ustring.cpp
@@ -35,6 +35,7 @@
#include "core/math/math_funcs.h"
#include "core/os/memory.h"
#include "core/string/print_string.h"
+#include "core/string/string_name.h"
#include "core/string/translation.h"
#include "core/string/ucaps.h"
#include "core/variant/variant.h"
@@ -4357,7 +4358,7 @@ String String::property_name_encode() const {
}
// Changes made to the set of invalid characters must also be reflected in the String documentation.
-const String String::invalid_node_name_characters = ". : @ / \"";
+const String String::invalid_node_name_characters = ". : @ / \" " UNIQUE_NODE_PREFIX;
String String::validate_node_name() const {
Vector<String> chars = String::invalid_node_name_characters.split(" ");