summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorm <malcolm.g.howard@gmail.com>2018-07-30 00:04:32 -0400
committerm <malcolm.g.howard@gmail.com>2018-11-19 14:03:13 -0500
commitbf1867aaab8d34ee2524f5b703214bb1c2684eb7 (patch)
tree8a7bb25d39118e5f933d815e3533f6ddaace4eb6
parent451e5fd0511bc2c17a66fc73a0de9a5169109517 (diff)
Added Python-like .get() method to Dictionary in GDScript #20488
Added .get() method to Dictionary class in GDScript to return the value if the key exists, or return Null if the key does not exist.
-rw-r--r--core/dictionary.cpp9
-rw-r--r--core/dictionary.h1
-rw-r--r--core/variant_call.cpp2
-rw-r--r--doc/classes/Dictionary.xml11
4 files changed, 23 insertions, 0 deletions
diff --git a/core/dictionary.cpp b/core/dictionary.cpp
index ccbdff3816..6a3ab82879 100644
--- a/core/dictionary.cpp
+++ b/core/dictionary.cpp
@@ -112,6 +112,15 @@ Variant Dictionary::get_valid(const Variant &p_key) const {
return E.get();
}
+Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
+ const Variant *result = getptr(p_key);
+ if (!result) {
+ return p_default;
+ }
+
+ return *result;
+}
+
int Dictionary::size() const {
return _p->variant_map.size();
diff --git a/core/dictionary.h b/core/dictionary.h
index d3b98c2f63..b77cc55254 100644
--- a/core/dictionary.h
+++ b/core/dictionary.h
@@ -58,6 +58,7 @@ public:
Variant *getptr(const Variant &p_key);
Variant get_valid(const Variant &p_key) const;
+ Variant get(const Variant &p_key, const Variant &p_default) const;
int size() const;
bool empty() const;
diff --git a/core/variant_call.cpp b/core/variant_call.cpp
index 8693a584f2..18b4977feb 100644
--- a/core/variant_call.cpp
+++ b/core/variant_call.cpp
@@ -486,6 +486,7 @@ struct _VariantCall {
VCALL_LOCALMEM0R(Dictionary, keys);
VCALL_LOCALMEM0R(Dictionary, values);
VCALL_LOCALMEM1R(Dictionary, duplicate);
+ VCALL_LOCALMEM2R(Dictionary, get);
VCALL_LOCALMEM2(Array, set);
VCALL_LOCALMEM1R(Array, get);
@@ -1679,6 +1680,7 @@ void register_variant_methods() {
ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, keys, varray());
ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, values, varray());
ADDFUNC1R(DICTIONARY, DICTIONARY, Dictionary, duplicate, BOOL, "deep", varray(false));
+ ADDFUNC2R(DICTIONARY, NIL, Dictionary, get, NIL, "key", NIL, "default", varray(Variant()));
ADDFUNC0R(ARRAY, INT, Array, size, varray());
ADDFUNC0R(ARRAY, BOOL, Array, empty, varray());
diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml
index 06c996e13e..cc58c56404 100644
--- a/doc/classes/Dictionary.xml
+++ b/doc/classes/Dictionary.xml
@@ -87,6 +87,17 @@
Return the list of values in the [code]Dictionary[/code].
</description>
</method>
+ <method name="get">
+ <return type="Variant">
+ </return>
+ <argument index="0" name="key" type="Variant">
+ </argument>
+ <argument index="1" name="default" type="Variant" default="null">
+ </argument>
+ <description>
+ Returns the current value for the specified key in the [code]Dictionary[/code]. If the key does not exist, the method returns the value of the optional default argument, or Null if it is omitted.
+ </description>
+ </method>
</methods>
<constants>
</constants>