diff --git a/core/io/json.cpp b/core/io/json.cpp index 5823afbdcd..6b4c2a0a72 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -55,7 +55,7 @@ String JSON::_make_indent(const String &p_indent, int p_size) { return indent_text; } -String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set &p_markers, bool p_full_precision) { +String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set &p_markers, bool p_full_precision) { String colon = ":"; String end_statement = ""; @@ -529,7 +529,7 @@ Error JSON::_parse_string(const String &p_json, Variant &r_ret, String &r_err_st } String JSON::stringify(const Variant &p_var, const String &p_indent, bool p_sort_keys, bool p_full_precision) { - Set markers; + Set markers; return _stringify(p_var, p_indent, 0, p_sort_keys, markers, p_full_precision); } diff --git a/core/io/json.h b/core/io/json.h index f20c97f540..3e1f3e6506 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -70,7 +70,7 @@ class JSON : public RefCounted { static const char *tk_name[]; static String _make_indent(const String &p_indent, int p_size); - static String _stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set &p_markers, bool p_full_precision = false); + static String _stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, Set &p_markers, bool p_full_precision = false); static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str); static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); diff --git a/core/templates/list.h b/core/templates/list.h index afbed998c2..7e7df4a25a 100644 --- a/core/templates/list.h +++ b/core/templates/list.h @@ -736,8 +736,8 @@ public: memdelete_arr(aux_buffer); } - const void *id() const { - return (void *)_data; + int64_t id() const { + return reinterpret_cast(_data); } /** diff --git a/core/templates/ordered_hash_map.h b/core/templates/ordered_hash_map.h index 4996b88190..c4eb22c95d 100644 --- a/core/templates/ordered_hash_map.h +++ b/core/templates/ordered_hash_map.h @@ -272,7 +272,7 @@ public: inline bool is_empty() const { return list.is_empty(); } inline int size() const { return list.size(); } - const void *id() const { + int64_t id() const { return list.id(); } diff --git a/core/templates/vector.h b/core/templates/vector.h index 98982c80d3..0bb03b74e1 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -257,6 +257,10 @@ public: return ConstIterator(ptr() + size()); } + _FORCE_INLINE_ int64_t id() const { + return reinterpret_cast(ptr()); + } + _FORCE_INLINE_ Vector() {} _FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); } diff --git a/core/variant/array.cpp b/core/variant/array.cpp index 69a0fff1a1..fcf9cfd7e9 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -651,8 +651,8 @@ Variant Array::max() const { return maxval; } -const void *Array::id() const { - return _p->array.ptr(); +int64_t Array::id() const { + return _p->array.id(); } Array::Array(const Array &p_from, uint32_t p_type, const StringName &p_class_name, const Variant &p_script) { diff --git a/core/variant/array.h b/core/variant/array.h index bd39b8e0b1..7dcea43c21 100644 --- a/core/variant/array.h +++ b/core/variant/array.h @@ -117,7 +117,7 @@ public: Variant min() const; Variant max() const; - const void *id() const; + int64_t id() const; bool typed_assign(const Array &p_other); void set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script); diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index 24d21386a7..0d9caf5531 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -349,7 +349,7 @@ void Dictionary::operator=(const Dictionary &p_dictionary) { _ref(p_dictionary); } -const void *Dictionary::id() const { +int64_t Dictionary::id() const { return _p->variant_map.id(); } diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index f8a2a7573f..7285c24c1a 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -84,7 +84,7 @@ public: Dictionary duplicate(bool p_deep = false) const; Dictionary recursive_duplicate(bool p_deep, int recursion_count) const; - const void *id() const; + int64_t id() const; Dictionary(const Dictionary &p_from); Dictionary(); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index c3fe443456..9c2e22cf60 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1794,6 +1794,7 @@ static void _register_variant_builtin_methods() { bind_method(Dictionary, clear, sarray(), varray()); bind_method(Dictionary, has, sarray("key"), varray()); bind_method(Dictionary, has_all, sarray("keys"), varray()); + bind_method(Dictionary, id, sarray(), varray()); bind_method(Dictionary, erase, sarray("key"), varray()); bind_method(Dictionary, hash, sarray(), varray()); bind_method(Dictionary, keys, sarray(), varray()); @@ -1811,6 +1812,7 @@ static void _register_variant_builtin_methods() { bind_method(Array, push_front, sarray("value"), varray()); bind_method(Array, append, sarray("value"), varray()); bind_method(Array, append_array, sarray("array"), varray()); + bind_method(Array, id, sarray(), varray()); bind_method(Array, resize, sarray("size"), varray()); bind_method(Array, insert, sarray("position", "value"), varray()); bind_method(Array, remove, sarray("position"), varray()); diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index 275b217247..55ed498a69 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -303,6 +303,15 @@ [b]Note:[/b] Arrays with equal contents can still produce different hashes. Only the exact same arrays will produce the same hashed integer value. + + + + Returns a number that uniquely identifies the piece of memory backing this array. + [b]Note:[/b] + This is useful in cases where there's a need to check if two variables refer to the same array, like this: [code]a.id() == b.id()[/code] + When both arrays are [b]the same[/b], `==` will give the same result and as quickly. However, when both arrays are [b]not the same[/b], `==` will perform a deep comparison of their contents to still be able to tell if they [b]are equal[/b] at least. + + diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index d661da5dd0..41afd90007 100644 --- a/doc/classes/Dictionary.xml +++ b/doc/classes/Dictionary.xml @@ -278,6 +278,15 @@ [b]Note:[/b] Dictionaries with the same keys/values but in a different order will have a different hash. + + + + Returns a number that uniquely identifies the piece of memory backing this dictionary. + [b]Note:[/b] + This is useful in cases where there's a need to check if two variables refer to the same dictionary, like this: [code]a.id() == b.id()[/code] + When both dictionaries are [b]the same[/b], `==` will give the same result and as quickly. However, when both dictionaries are [b]not the same[/b], `==` will perform a deep comparison of their contents to still be able to tell if they [b]are equal[/b] at least. + +