summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkobewi <kobewi4e@gmail.com>2022-09-08 18:14:10 +0200
committerkobewi <kobewi4e@gmail.com>2024-03-06 14:49:35 +0100
commiteb0a624902cc3136358bd1b69ae84e575f73a881 (patch)
tree7133506f3d86b31352ac78f2ead2cf0789e311e8
parent9b94c80e9aff2a4f363ae6d8e2bbe837aa5876bc (diff)
downloadredot-engine-eb0a624902cc3136358bd1b69ae84e575f73a881.tar.gz
Allow returning Dictionary after merging
-rw-r--r--core/variant/dictionary.cpp7
-rw-r--r--core/variant/dictionary.h1
-rw-r--r--core/variant/variant_call.cpp1
-rw-r--r--doc/classes/Dictionary.xml18
4 files changed, 27 insertions, 0 deletions
diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp
index 9f65a73c6f..7416101d51 100644
--- a/core/variant/dictionary.cpp
+++ b/core/variant/dictionary.cpp
@@ -249,6 +249,7 @@ void Dictionary::clear() {
}
void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
+ ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
if (p_overwrite || !has(E.key)) {
operator[](E.key) = E.value;
@@ -256,6 +257,12 @@ void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
}
}
+Dictionary Dictionary::merged(const Dictionary &p_dictionary, bool p_overwrite) const {
+ Dictionary ret = duplicate();
+ ret.merge(p_dictionary, p_overwrite);
+ return ret;
+}
+
void Dictionary::_unref() const {
ERR_FAIL_NULL(_p);
if (_p->refcount.unref()) {
diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h
index f94a0da80a..67178ee7b7 100644
--- a/core/variant/dictionary.h
+++ b/core/variant/dictionary.h
@@ -64,6 +64,7 @@ public:
bool is_empty() const;
void clear();
void merge(const Dictionary &p_dictionary, bool p_overwrite = false);
+ Dictionary merged(const Dictionary &p_dictionary, bool p_overwrite = false) const;
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 543ee1135f..060ea007ff 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -2205,6 +2205,7 @@ static void _register_variant_builtin_methods() {
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, merged, sarray("dictionary", "overwrite"), varray(false));
bind_method(Dictionary, has, sarray("key"), varray());
bind_method(Dictionary, has_all, sarray("keys"), varray());
bind_method(Dictionary, find_key, sarray("value"), varray());
diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml
index 7a5e51e4ef..14ce7c894f 100644
--- a/doc/classes/Dictionary.xml
+++ b/doc/classes/Dictionary.xml
@@ -340,6 +340,24 @@
[b]Note:[/b] [method merge] is [i]not[/i] recursive. Nested dictionaries are considered as keys that can be overwritten or not depending on the value of [param overwrite], but they will never be merged together.
</description>
</method>
+ <method name="merged" qualifiers="const">
+ <return type="Dictionary" />
+ <param index="0" name="dictionary" type="Dictionary" />
+ <param index="1" name="overwrite" type="bool" default="false" />
+ <description>
+ Returns a copy of this dictionary merged with the other [param dictionary]. By default, duplicate keys are not copied over, unless [param overwrite] is [code]true[/code]. See also [method merge].
+ This method is useful for quickly making dictionaries with default values:
+ [codeblock]
+ var base = { "fruit": "apple", "vegetable": "potato" }
+ var extra = { "fruit": "orange", "dressing": "vinegar" }
+ # Prints { "fruit": "orange", "vegetable": "potato", "dressing": "vinegar" }
+ print(extra.merged(base))
+ # Prints { "fruit": "apple", "vegetable": "potato", "dressing": "vinegar" }
+ print(extra.merged(base, true))
+ [/codeblock]
+ See also [method merge].
+ </description>
+ </method>
<method name="size" qualifiers="const">
<return type="int" />
<description>