Merge pull request #20627 from malcolmhoward/core-dictionary-get-key

#20488 core dictionary get key
This commit is contained in:
Rémi Verschelde 2018-11-19 21:11:55 +01:00 committed by GitHub
commit 35fbbeb99b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 0 deletions

View file

@ -112,6 +112,15 @@ Variant Dictionary::get_valid(const Variant &p_key) const {
return E.get(); 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 { int Dictionary::size() const {
return _p->variant_map.size(); return _p->variant_map.size();

View file

@ -58,6 +58,7 @@ public:
Variant *getptr(const Variant &p_key); Variant *getptr(const Variant &p_key);
Variant get_valid(const Variant &p_key) const; Variant get_valid(const Variant &p_key) const;
Variant get(const Variant &p_key, const Variant &p_default) const;
int size() const; int size() const;
bool empty() const; bool empty() const;

View file

@ -484,6 +484,7 @@ struct _VariantCall {
VCALL_LOCALMEM0R(Dictionary, keys); VCALL_LOCALMEM0R(Dictionary, keys);
VCALL_LOCALMEM0R(Dictionary, values); VCALL_LOCALMEM0R(Dictionary, values);
VCALL_LOCALMEM1R(Dictionary, duplicate); VCALL_LOCALMEM1R(Dictionary, duplicate);
VCALL_LOCALMEM2R(Dictionary, get);
VCALL_LOCALMEM2(Array, set); VCALL_LOCALMEM2(Array, set);
VCALL_LOCALMEM1R(Array, get); VCALL_LOCALMEM1R(Array, get);
@ -1677,6 +1678,7 @@ void register_variant_methods() {
ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, keys, varray()); ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, keys, varray());
ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, values, varray()); ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, values, varray());
ADDFUNC1R(DICTIONARY, DICTIONARY, Dictionary, duplicate, BOOL, "deep", varray(false)); 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, INT, Array, size, varray());
ADDFUNC0R(ARRAY, BOOL, Array, empty, varray()); ADDFUNC0R(ARRAY, BOOL, Array, empty, varray());

View file

@ -87,6 +87,17 @@
Return the list of values in the [code]Dictionary[/code]. Return the list of values in the [code]Dictionary[/code].
</description> </description>
</method> </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> </methods>
<constants> <constants>
</constants> </constants>