From a0915e6dee4e54563a98ca6adb8968dbdcea1af1 Mon Sep 17 00:00:00 2001 From: kobewi Date: Mon, 4 Apr 2022 18:46:55 +0200 Subject: Add Dictionary.merge() --- core/variant/dictionary.cpp | 8 ++++++++ core/variant/dictionary.h | 1 + core/variant/variant_call.cpp | 1 + 3 files changed, 10 insertions(+) (limited to 'core') diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index bda8c93a79..822021f440 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -269,6 +269,14 @@ void Dictionary::clear() { _p->variant_map.clear(); } +void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) { + for (const KeyValue &E : p_dictionary._p->variant_map) { + if (p_overwrite || !has(E.key)) { + this->operator[](E.key) = E.value; + } + } +} + void Dictionary::_unref() const { ERR_FAIL_COND(!_p); if (_p->refcount.unref()) { diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index 1224a4ff6f..2632893e8d 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -62,6 +62,7 @@ public: int size() const; bool is_empty() const; void clear(); + void merge(const Dictionary &p_dictionary, bool p_overwrite = false); bool has(const Variant &p_key) const; bool has_all(const Array &p_keys) const; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index a3568a8d6a..2e77a4b2f8 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -1808,6 +1808,7 @@ static void _register_variant_builtin_methods() { bind_method(Dictionary, size, sarray(), varray()); bind_method(Dictionary, is_empty, sarray(), varray()); bind_method(Dictionary, clear, sarray(), varray()); + bind_method(Dictionary, merge, sarray("dictionary", "overwrite"), varray(false)); bind_method(Dictionary, has, sarray("key"), varray()); bind_method(Dictionary, has_all, sarray("keys"), varray()); bind_method(Dictionary, erase, sarray("key"), varray()); -- cgit v1.2.3