diff options
author | kobewi <kobewi4e@gmail.com> | 2022-04-04 18:46:55 +0200 |
---|---|---|
committer | kobewi <kobewi4e@gmail.com> | 2022-05-31 21:05:56 +0200 |
commit | a0915e6dee4e54563a98ca6adb8968dbdcea1af1 (patch) | |
tree | 391fcde7f2a1fb252a3dcd7c8ae00ec259220437 | |
parent | 3aa83a023591bb510ce898b68882466f59ae1ceb (diff) |
Add Dictionary.merge()
-rw-r--r-- | core/variant/dictionary.cpp | 8 | ||||
-rw-r--r-- | core/variant/dictionary.h | 1 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 1 | ||||
-rw-r--r-- | doc/classes/Dictionary.xml | 8 |
4 files changed, 18 insertions, 0 deletions
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<Variant, Variant> &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()); diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index 8ee09ba8f8..2f336ab73e 100644 --- a/doc/classes/Dictionary.xml +++ b/doc/classes/Dictionary.xml @@ -291,6 +291,14 @@ Returns the list of keys in the [Dictionary]. </description> </method> + <method name="merge"> + <return type="void" /> + <argument index="0" name="dictionary" type="Dictionary" /> + <argument index="1" name="overwrite" type="bool" default="false" /> + <description> + Adds elements from [code]dictionary[/code] to this [Dictionary]. By default, duplicate keys will not be copied over, unless [code]overwrite[/code] is [code]true[/code]. + </description> + </method> <method name="size" qualifiers="const"> <return type="int" /> <description> |