From bf1867aaab8d34ee2524f5b703214bb1c2684eb7 Mon Sep 17 00:00:00 2001 From: m Date: Mon, 30 Jul 2018 00:04:32 -0400 Subject: 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. --- core/dictionary.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'core/dictionary.cpp') 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(); -- cgit v1.2.3