class_dictionary.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/3.6/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/3.6/doc/classes/Dictionary.xml.
  6. .. _class_Dictionary:
  7. Dictionary
  8. ==========
  9. Dictionary type.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. 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.
  14. You can define a dictionary by placing a comma-separated list of ``key: value`` pairs in curly braces ``{}``.
  15. Erasing elements while iterating over them **is not supported** and will result in undefined behavior.
  16. \ **Note:** Dictionaries are always passed by reference. To get a copy of a dictionary which can be modified independently of the original dictionary, use :ref:`duplicate<class_Dictionary_method_duplicate>`.
  17. Creating a dictionary:
  18. ::
  19. var my_dict = {} # Creates an empty dictionary.
  20. var dict_variable_key = "Another key name"
  21. var dict_variable_value = "value2"
  22. var another_dict = {
  23. "Some key name": "value1",
  24. dict_variable_key: dict_variable_value,
  25. }
  26. var points_dict = {"White": 50, "Yellow": 75, "Orange": 100}
  27. # Alternative Lua-style syntax.
  28. # Doesn't require quotes around keys, but only string constants can be used as key names.
  29. # Additionally, key names must start with a letter or an underscore.
  30. # Here, `some_key` is a string literal, not a variable!
  31. another_dict = {
  32. some_key = 42,
  33. }
  34. You can access a dictionary's values by referencing the appropriate key. In the above example, ``points_dict["White"]`` will return ``50``. You can also write ``points_dict.White``, which is equivalent. However, you'll have to use the bracket syntax if the key you're accessing the dictionary with isn't a fixed string (such as a number or variable).
  35. ::
  36. export(String, "White", "Yellow", "Orange") var my_color
  37. var points_dict = {"White": 50, "Yellow": 75, "Orange": 100}
  38. func _ready():
  39. # We can't use dot syntax here as `my_color` is a variable.
  40. var points = points_dict[my_color]
  41. In the above code, ``points`` will be assigned the value that is paired with the appropriate color selected in ``my_color``.
  42. Dictionaries can contain more complex data:
  43. ::
  44. my_dict = {"First Array": [1, 2, 3, 4]} # Assigns an Array to a String key.
  45. To add a key to an existing dictionary, access it like an existing key and assign to it:
  46. ::
  47. var points_dict = {"White": 50, "Yellow": 75, "Orange": 100}
  48. points_dict["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value.
  49. Finally, dictionaries can contain different types of keys and values in the same dictionary:
  50. ::
  51. # This is a valid dictionary.
  52. # To access the string "Nested value" below, use `my_dict.sub_dict.sub_key` or `my_dict["sub_dict"]["sub_key"]`.
  53. # Indexing styles can be mixed and matched depending on your needs.
  54. var my_dict = {
  55. "String Key": 5,
  56. 4: [1, 2, 3],
  57. 7: "Hello",
  58. "sub_dict": {"sub_key": "Nested value"},
  59. }
  60. \ **Note:** Unlike :ref:`Array<class_Array>`\ s, you can't compare dictionaries directly:
  61. ::
  62. array1 = [1, 2, 3]
  63. array2 = [1, 2, 3]
  64. func compare_arrays():
  65. print(array1 == array2) # Will print true.
  66. var dict1 = {"a": 1, "b": 2, "c": 3}
  67. var dict2 = {"a": 1, "b": 2, "c": 3}
  68. func compare_dictionaries():
  69. print(dict1 == dict2) # Will NOT print true.
  70. You need to first calculate the dictionary's hash with :ref:`hash<class_Dictionary_method_hash>` before you can compare them:
  71. ::
  72. var dict1 = {"a": 1, "b": 2, "c": 3}
  73. var dict2 = {"a": 1, "b": 2, "c": 3}
  74. func compare_dictionaries():
  75. print(dict1.hash() == dict2.hash()) # Will print true.
  76. \ **Note:** When declaring a dictionary with ``const``, the dictionary itself can still be mutated by defining the values of individual keys. Using ``const`` will only prevent assigning the constant with another value after it was initialized.
  77. .. rst-class:: classref-introduction-group
  78. Tutorials
  79. ---------
  80. - `GDScript basics: Dictionary <../tutorials/scripting/gdscript/gdscript_basics.html#dictionary>`__
  81. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  82. - `OS Test Demo <https://godotengine.org/asset-library/asset/677>`__
  83. .. rst-class:: classref-reftable-group
  84. Methods
  85. -------
  86. .. table::
  87. :widths: auto
  88. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | void | :ref:`clear<class_Dictionary_method_clear>` **(** **)** |
  90. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | :ref:`Dictionary<class_Dictionary>` | :ref:`duplicate<class_Dictionary_method_duplicate>` **(** :ref:`bool<class_bool>` deep=false **)** |
  92. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | :ref:`bool<class_bool>` | :ref:`empty<class_Dictionary_method_empty>` **(** **)** |
  94. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | :ref:`bool<class_bool>` | :ref:`erase<class_Dictionary_method_erase>` **(** :ref:`Variant<class_Variant>` key **)** |
  96. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | :ref:`Variant<class_Variant>` | :ref:`find_key<class_Dictionary_method_find_key>` **(** :ref:`Variant<class_Variant>` value **)** |
  98. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  99. | :ref:`Variant<class_Variant>` | :ref:`get<class_Dictionary_method_get>` **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=null **)** |
  100. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  101. | :ref:`Variant<class_Variant>` | :ref:`get_or_add<class_Dictionary_method_get_or_add>` **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=null **)** |
  102. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  103. | :ref:`bool<class_bool>` | :ref:`has<class_Dictionary_method_has>` **(** :ref:`Variant<class_Variant>` key **)** |
  104. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  105. | :ref:`bool<class_bool>` | :ref:`has_all<class_Dictionary_method_has_all>` **(** :ref:`Array<class_Array>` keys **)** |
  106. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  107. | :ref:`int<class_int>` | :ref:`hash<class_Dictionary_method_hash>` **(** **)** |
  108. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  109. | :ref:`Array<class_Array>` | :ref:`keys<class_Dictionary_method_keys>` **(** **)** |
  110. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  111. | void | :ref:`merge<class_Dictionary_method_merge>` **(** :ref:`Dictionary<class_Dictionary>` dictionary, :ref:`bool<class_bool>` overwrite=false **)** |
  112. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  113. | :ref:`int<class_int>` | :ref:`size<class_Dictionary_method_size>` **(** **)** |
  114. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  115. | :ref:`Array<class_Array>` | :ref:`values<class_Dictionary_method_values>` **(** **)** |
  116. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  117. .. rst-class:: classref-section-separator
  118. ----
  119. .. rst-class:: classref-descriptions-group
  120. Method Descriptions
  121. -------------------
  122. .. _class_Dictionary_method_clear:
  123. .. rst-class:: classref-method
  124. void **clear** **(** **)**
  125. Clear the dictionary, removing all key/value pairs.
  126. .. rst-class:: classref-item-separator
  127. ----
  128. .. _class_Dictionary_method_duplicate:
  129. .. rst-class:: classref-method
  130. :ref:`Dictionary<class_Dictionary>` **duplicate** **(** :ref:`bool<class_bool>` deep=false **)**
  131. Creates a copy of the dictionary, and returns it. The ``deep`` parameter causes inner dictionaries and arrays to be copied recursively, but does not apply to objects.
  132. .. rst-class:: classref-item-separator
  133. ----
  134. .. _class_Dictionary_method_empty:
  135. .. rst-class:: classref-method
  136. :ref:`bool<class_bool>` **empty** **(** **)**
  137. Returns ``true`` if the dictionary is empty.
  138. .. rst-class:: classref-item-separator
  139. ----
  140. .. _class_Dictionary_method_erase:
  141. .. rst-class:: classref-method
  142. :ref:`bool<class_bool>` **erase** **(** :ref:`Variant<class_Variant>` key **)**
  143. Erase a dictionary key/value pair by key. Returns ``true`` if the given key was present in the dictionary, ``false`` otherwise.
  144. \ **Note:** Don't erase elements while iterating over the dictionary. You can iterate over the :ref:`keys<class_Dictionary_method_keys>` array instead.
  145. .. rst-class:: classref-item-separator
  146. ----
  147. .. _class_Dictionary_method_find_key:
  148. .. rst-class:: classref-method
  149. :ref:`Variant<class_Variant>` **find_key** **(** :ref:`Variant<class_Variant>` value **)**
  150. Returns the first key whose associated value is equal to ``value``, or ``null`` if no such value is found.
  151. \ **Note:** ``null`` is also a valid key. If you have it in your **Dictionary**, the :ref:`find_key<class_Dictionary_method_find_key>` method can give misleading results.
  152. .. rst-class:: classref-item-separator
  153. ----
  154. .. _class_Dictionary_method_get:
  155. .. rst-class:: classref-method
  156. :ref:`Variant<class_Variant>` **get** **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=null **)**
  157. Returns the current value for the specified key in the **Dictionary**. If the key does not exist, the method returns the value of the optional default argument, or ``null`` if it is omitted.
  158. .. rst-class:: classref-item-separator
  159. ----
  160. .. _class_Dictionary_method_get_or_add:
  161. .. rst-class:: classref-method
  162. :ref:`Variant<class_Variant>` **get_or_add** **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=null **)**
  163. Gets a value and ensures the key is set. If the ``key`` exists in the dictionary, this behaves like :ref:`get<class_Dictionary_method_get>`. Otherwise, the ``default`` value is inserted into the dictionary and returned.
  164. .. rst-class:: classref-item-separator
  165. ----
  166. .. _class_Dictionary_method_has:
  167. .. rst-class:: classref-method
  168. :ref:`bool<class_bool>` **has** **(** :ref:`Variant<class_Variant>` key **)**
  169. Returns ``true`` if the dictionary has a given key.
  170. \ **Note:** This is equivalent to using the ``in`` operator as follows:
  171. ::
  172. # Will evaluate to `true`.
  173. if "godot" in {"godot": "engine"}:
  174. pass
  175. This method (like the ``in`` operator) will evaluate to ``true`` as long as the key exists, even if the associated value is ``null``.
  176. .. rst-class:: classref-item-separator
  177. ----
  178. .. _class_Dictionary_method_has_all:
  179. .. rst-class:: classref-method
  180. :ref:`bool<class_bool>` **has_all** **(** :ref:`Array<class_Array>` keys **)**
  181. Returns ``true`` if the dictionary has all the keys in the given array.
  182. .. rst-class:: classref-item-separator
  183. ----
  184. .. _class_Dictionary_method_hash:
  185. .. rst-class:: classref-method
  186. :ref:`int<class_int>` **hash** **(** **)**
  187. Returns a hashed 32-bit integer value representing the dictionary contents. This can be used to compare dictionaries by value:
  188. ::
  189. var dict1 = {0: 10}
  190. var dict2 = {0: 10}
  191. # The line below prints `true`, whereas it would have printed `false` if both variables were compared directly.
  192. print(dict1.hash() == dict2.hash())
  193. \ **Note:** Dictionaries with the same keys/values but in a different order will have a different hash.
  194. \ **Note:** Dictionaries with equal content will always produce identical hash values. However, the reverse is not true. Returning identical hash values does *not* imply the dictionaries are equal, because different dictionaries can have identical hash values due to hash collisions.
  195. .. rst-class:: classref-item-separator
  196. ----
  197. .. _class_Dictionary_method_keys:
  198. .. rst-class:: classref-method
  199. :ref:`Array<class_Array>` **keys** **(** **)**
  200. Returns the list of keys in the **Dictionary**.
  201. .. rst-class:: classref-item-separator
  202. ----
  203. .. _class_Dictionary_method_merge:
  204. .. rst-class:: classref-method
  205. void **merge** **(** :ref:`Dictionary<class_Dictionary>` dictionary, :ref:`bool<class_bool>` overwrite=false **)**
  206. Adds elements from ``dictionary`` to this **Dictionary**. By default, duplicate keys will not be copied over, unless ``overwrite`` is ``true``.
  207. .. rst-class:: classref-item-separator
  208. ----
  209. .. _class_Dictionary_method_size:
  210. .. rst-class:: classref-method
  211. :ref:`int<class_int>` **size** **(** **)**
  212. Returns the number of keys in the dictionary.
  213. .. rst-class:: classref-item-separator
  214. ----
  215. .. _class_Dictionary_method_values:
  216. .. rst-class:: classref-method
  217. :ref:`Array<class_Array>` **values** **(** **)**
  218. Returns the list of values in the **Dictionary**.
  219. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  220. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  221. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  222. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`