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.
This commit is contained in:
m
2018-07-30 00:04:32 -04:00
parent 451e5fd051
commit bf1867aaab
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();
}
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();