Skip to main content

VarDictionary

Type Alias VarDictionary 

pub type VarDictionary = Dictionary<Variant, Variant>;
Expand description

Untyped Godot Dictionary.

Alias for Dictionary<Variant, Variant>. This provides an untyped dictionary that can store any key-value pairs. Available on all Godot versions.

Aliased Type§

pub struct VarDictionary { /* private fields */ }

Methods from Deref<Target = AnyDictionary>§

pub fn at(&self, key: impl AsArg<Variant>) -> Variant

⚠️ Returns the value for the given key, or panics.

To check for presence, use get().

§Panics

If there is no value for the given key. Note that this is distinct from a NIL value, which is returned as Variant::nil().

pub fn get(&self, key: impl AsArg<Variant>) -> Option<Variant>

Returns the value for the given key, or None.

Note that NIL values are returned as Some(Variant::nil()), while absent values are returned as None.

pub fn contains_key(&self, key: impl AsArg<Variant>) -> bool

Returns true if the dictionary contains the given key.

Godot equivalent: has

pub fn contains_all_keys(&self, keys: &Array<Variant>) -> bool

Returns true if the dictionary contains all the given keys.

Godot equivalent: has_all

pub fn len(&self) -> usize

Returns the number of entries in the dictionary.

Godot equivalent: size

pub fn is_empty(&self) -> bool

Returns true if the dictionary is empty.

pub fn hash_u32(&self) -> u32

Returns a 32-bit integer hash value representing the dictionary and its contents.

Dictionaries with equal content will always produce identical hash values. However, the reverse is not true: Different dictionaries can have identical hash values due to hash collisions.

pub fn find_key_by_value(&self, value: impl AsArg<Variant>) -> Option<Variant>

Reverse-search a key by its value.

Unlike Godot, this will return None if the key does not exist and Some(key) if found.

This operation is rarely needed and very inefficient. If you find yourself needing it a lot, consider using a HashMap or Dictionary with the inverse mapping.

Godot equivalent: find_key

pub fn clear(&mut self)

Removes all key-value pairs from the dictionary.

pub fn remove(&mut self, key: impl AsArg<Variant>) -> Option<Variant>

Removes a key from the dictionary, and returns the value associated with the key if it was present.

This is a covariant-safe operation that removes data without adding typed data.

Godot equivalent: erase

pub fn erase(&mut self, key: impl AsArg<Variant>) -> Option<Variant>

Alias for remove().

Godot equivalent: erase

pub fn keys_array(&self) -> AnyArray

Creates a new Array containing all the keys currently in the dictionary.

Godot equivalent: keys

pub fn values_array(&self) -> AnyArray

Creates a new Array containing all the values currently in the dictionary.

Godot equivalent: values

pub fn duplicate_shallow(&self) -> AnyDictionary

Returns a shallow copy, sharing reference types (Array, Dictionary, Object…) with the original dictionary.

This operation retains the dynamic key/value types: copying Dictionary<K, V> will yield another Dictionary<K, V>.

To create a deep copy, use duplicate_deep() instead. To create a new reference to the same dictionary data, use clone().

pub fn duplicate_deep(&self) -> AnyDictionary

Returns a deep copy, duplicating nested Array/Dictionary elements but keeping Object elements shared.

This operation retains the dynamic key/value types: copying Dictionary<K, V> will yield another Dictionary<K, V>.

To create a shallow copy, use duplicate_shallow() instead. To create a new reference to the same dictionary data, use clone().

pub fn iter_shared(&self) -> AnyDictIter<'_> ⓘ

Returns an iterator over the key-value pairs as (Variant, Variant).

Each pair is a (cheap, shallow) copy from the original dictionary.

Use .typed::<K, V>() on the returned iterator to convert to typed (K, V) pairs.

Note that it’s possible to modify the dictionary through another reference while iterating over it. This will not result in unsoundness or crashes, but will cause the iterator to behave in an unspecified way.

pub fn keys_shared(&self) -> AnyDictKeys<'_> ⓘ

Returns an iterator over the keys as Variant.

Each key is a (cheap, shallow) copy from the original dictionary.

Use .typed::<K>() on the returned iterator to convert to typed K keys.

Note that it’s possible to modify the dictionary through another reference while iterating over it. This will not result in unsoundness or crashes, but will cause the iterator to behave in an unspecified way.

pub fn values_shared(&self) -> AnyDictValues<'_> ⓘ

Returns an iterator over the values as Variant.

Each value is a (cheap, shallow) copy from the original dictionary.

Use .typed::<V>() on the returned iterator to convert to typed V values.

Note that it’s possible to modify the dictionary through another reference while iterating over it. This will not result in unsoundness or crashes, but will cause the iterator to behave in an unspecified way.

pub fn is_read_only(&self) -> bool

Returns true if the dictionary is read-only.

See into_read_only().

pub fn key_element_type(&self) -> ElementType

Returns the runtime element type information for keys in this dictionary.

The result is generally cached, so feel free to call this method repeatedly.

§Compatibility

Always reflects the runtime engine-side type. Different behavior based on Godot runtime version (not the api-4-* flag):

  • Since 4.4: Returns the type information stored in the Godot engine. If this is a Dictionary<K, V>, returns a type representing K.
  • Before 4.4: Always returns ElementType::Untyped, as the engine does not support typed dictionaries.

pub fn value_element_type(&self) -> ElementType

Returns the runtime element type information for values in this dictionary.

The result is generally cached, so feel free to call this method repeatedly.

§Compatibility

Always reflects the runtime engine-side type. Different behavior based on Godot runtime version (not the api-4-* flag):

  • Since 4.4: Returns the type information stored in the Godot engine. If this is a Dictionary<K, V>, returns a type representing V.
  • Before 4.4: Always returns ElementType::Untyped, as the engine does not support typed dictionaries.