diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index 5a95d44f8..1ad06b312 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -136,6 +136,15 @@ bool Dictionary::has_all(const Array &p_keys) const { return true; } +Variant Dictionary::find_key(const Variant &p_value) const { + for (OrderedHashMap::Element E = _p->variant_map.front(); E; E = E.next()) { + if (E.value() == p_value) { + return E.key(); + } + } + return Variant(); +} + bool Dictionary::erase(const Variant &p_key) { return _p->variant_map.erase(p_key); } diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index 3cc3a03b2..dbfc7756f 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -65,6 +65,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 dcc764d49..ba77f1f00 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -852,6 +852,7 @@ struct _VariantCall { VCALL_LOCALMEM2(Dictionary, merge); VCALL_LOCALMEM1R(Dictionary, has); VCALL_LOCALMEM1R(Dictionary, has_all); + VCALL_LOCALMEM1R(Dictionary, find_key); VCALL_LOCALMEM1R(Dictionary, erase); VCALL_LOCALMEM0R(Dictionary, hash); VCALL_LOCALMEM0R(Dictionary, keys); @@ -2918,6 +2919,7 @@ void register_variant_methods() { ADDFUNC2NC(DICTIONARY, NIL, Dictionary, merge, DICTIONARY, "dictionary", BOOL, "overwrite", varray(false)); ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has, NIL, "key", varray()); ADDFUNC1R(DICTIONARY, BOOL, Dictionary, has_all, ARRAY, "keys", varray()); + ADDFUNC1R(DICTIONARY, NIL, Dictionary, find_key, NIL, "value", varray()); ADDFUNC1RNC(DICTIONARY, BOOL, Dictionary, erase, NIL, "key", varray()); ADDFUNC0R(DICTIONARY, INT, Dictionary, hash, varray()); ADDFUNC0R(DICTIONARY, ARRAY, Dictionary, keys, varray()); diff --git a/doc/classes/Dictionary.xml b/doc/classes/Dictionary.xml index b27deef50..2da47dc49 100644 --- a/doc/classes/Dictionary.xml +++ b/doc/classes/Dictionary.xml @@ -4,7 +4,7 @@ Dictionary type. - Dictionary type. Associative container which contains values referenced by unique keys. Dictionaries are composed of pairs of keys (which must be unique) and values. Dictionaries will preserve the insertion order when adding elements, even though this may not be reflected when printing the dictionary. In other programming languages, this data structure is sometimes referred to as a hash map or associative array. + Dictionary type. Associative container which contains values referenced by unique keys. Dictionaries are composed of pairs of keys (which must be unique) and values. Dictionaries will preserve the insertion order when adding elements. In other programming languages, this data structure is sometimes referred to as a hash map or associative array. You can define a dictionary by placing a comma-separated list of [code]key: value[/code] pairs in curly braces [code]{}[/code]. Erasing elements while iterating over them [b]is not supported[/b] and will result in undefined behavior. [b]Note:[/b] Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use [method duplicate]. @@ -115,6 +115,14 @@ [b]Note:[/b] Don't erase elements while iterating over the dictionary. You can iterate over the [method keys] array instead. + + + + + Returns the first key whose associated value is equal to [code]value[/code], or [code]null[/code] if no such value is found. + [b]Node:[/b] [code]null[/code] is also a valid key. I you have it in your [Dictionary], the [method find_key] method can give misleading results. + +