diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-09-01 17:38:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-01 17:38:39 +0200 |
commit | 41156a1e83b1110dcd08e08d2fc063dfd7cd6899 (patch) | |
tree | a2583b37bb55d340828914f17a5b8e8634b6d23c /core/variant | |
parent | d214956925c4d336c0a4f0d36e45cef30fdaab3c (diff) | |
parent | cc424bcb18585f09148d12e28f8d4b9f54c8445d (diff) |
Merge pull request #63968 from KoBeWi/finding_stuff_in_a_dictionary
Diffstat (limited to 'core/variant')
-rw-r--r-- | core/variant/dictionary.cpp | 9 | ||||
-rw-r--r-- | core/variant/dictionary.h | 1 | ||||
-rw-r--r-- | core/variant/variant_call.cpp | 1 |
3 files changed, 11 insertions, 0 deletions
diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index d9f4359ee5..c1cb782a57 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -195,6 +195,15 @@ bool Dictionary::has_all(const Array &p_keys) const { return true; } +Variant Dictionary::find_key(const Variant &p_value) const { + for (const KeyValue<Variant, Variant> &E : _p->variant_map) { + if (E.value == p_value) { + return E.key; + } + } + return Variant(); +} + bool Dictionary::erase(const Variant &p_key) { ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state."); if (p_key.get_type() == Variant::STRING_NAME) { diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index 2632893e8d..d9c9db56cf 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -66,6 +66,7 @@ public: bool has(const Variant &p_key) const; bool has_all(const Array &p_keys) const; + Variant find_key(const Variant &p_value) const; bool erase(const Variant &p_key); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index a4bce979fe..537992705e 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -2014,6 +2014,7 @@ static void _register_variant_builtin_methods() { bind_method(Dictionary, merge, 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()); bind_method(Dictionary, erase, sarray("key"), varray()); bind_method(Dictionary, hash, sarray(), varray()); bind_method(Dictionary, keys, sarray(), varray()); |