class_dictionary.rst 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/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 new entries. 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. \ **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>`.
  16. Creating a dictionary:
  17. .. tabs::
  18. .. code-tab:: gdscript
  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. .. code-tab:: csharp
  35. var myDict = new Godot.Collections.Dictionary(); // Creates an empty dictionary.
  36. var pointsDict = new Godot.Collections.Dictionary
  37. {
  38. {"White", 50},
  39. {"Yellow", 75},
  40. {"Orange", 100}
  41. };
  42. You can access a dictionary's value by referencing its corresponding 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).
  43. .. tabs::
  44. .. code-tab:: gdscript
  45. @export_enum("White", "Yellow", "Orange") var my_color: String
  46. var points_dict = {"White": 50, "Yellow": 75, "Orange": 100}
  47. func _ready():
  48. # We can't use dot syntax here as `my_color` is a variable.
  49. var points = points_dict[my_color]
  50. .. code-tab:: csharp
  51. [Export(PropertyHint.Enum, "White,Yellow,Orange")]
  52. public string MyColor { get; set; }
  53. public Godot.Collections.Dictionary pointsDict = new Godot.Collections.Dictionary
  54. {
  55. {"White", 50},
  56. {"Yellow", 75},
  57. {"Orange", 100}
  58. };
  59. public override void _Ready()
  60. {
  61. int points = (int)pointsDict[MyColor];
  62. }
  63. In the above code, ``points`` will be assigned the value that is paired with the appropriate color selected in ``my_color``.
  64. Dictionaries can contain more complex data:
  65. .. tabs::
  66. .. code-tab:: gdscript
  67. var my_dict = {
  68. "First Array": [1, 2, 3, 4] # Assigns an Array to a String key.
  69. }
  70. .. code-tab:: csharp
  71. var myDict = new Godot.Collections.Dictionary
  72. {
  73. {"First Array", new Godot.Collections.Array{1, 2, 3, 4}}
  74. };
  75. To add a key to an existing dictionary, access it like an existing key and assign to it:
  76. .. tabs::
  77. .. code-tab:: gdscript
  78. var points_dict = {"White": 50, "Yellow": 75, "Orange": 100}
  79. points_dict["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value.
  80. .. code-tab:: csharp
  81. var pointsDict = new Godot.Collections.Dictionary
  82. {
  83. {"White", 50},
  84. {"Yellow", 75},
  85. {"Orange", 100}
  86. };
  87. pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value.
  88. Finally, dictionaries can contain different types of keys and values in the same dictionary:
  89. .. tabs::
  90. .. code-tab:: gdscript
  91. # This is a valid dictionary.
  92. # To access the string "Nested value" below, use `my_dict.sub_dict.sub_key` or `my_dict["sub_dict"]["sub_key"]`.
  93. # Indexing styles can be mixed and matched depending on your needs.
  94. var my_dict = {
  95. "String Key": 5,
  96. 4: [1, 2, 3],
  97. 7: "Hello",
  98. "sub_dict": {"sub_key": "Nested value"},
  99. }
  100. .. code-tab:: csharp
  101. // This is a valid dictionary.
  102. // To access the string "Nested value" below, use `((Godot.Collections.Dictionary)myDict["sub_dict"])["sub_key"]`.
  103. var myDict = new Godot.Collections.Dictionary {
  104. {"String Key", 5},
  105. {4, new Godot.Collections.Array{1,2,3}},
  106. {7, "Hello"},
  107. {"sub_dict", new Godot.Collections.Dictionary{{"sub_key", "Nested value"}}}
  108. };
  109. The keys of a dictionary can be iterated with the ``for`` keyword:
  110. .. tabs::
  111. .. code-tab:: gdscript
  112. var groceries = {"Orange": 20, "Apple": 2, "Banana": 4}
  113. for fruit in groceries:
  114. var amount = groceries[fruit]
  115. .. code-tab:: csharp
  116. var groceries = new Godot.Collections.Dictionary{{"Orange", 20}, {"Apple", 2}, {"Banana", 4}};
  117. foreach (var (fruit, amount) in groceries)
  118. {
  119. // `fruit` is the key, `amount` is the value.
  120. }
  121. \ **Note:** Erasing elements while iterating over dictionaries is **not** supported and will result in unpredictable behavior.
  122. \ **Note:** When declaring a dictionary with ``const``, the dictionary becomes read-only. A read-only Dictionary's entries cannot be overridden at run-time. This does *not* affect nested :ref:`Array<class_Array>` and **Dictionary** values.
  123. .. rst-class:: classref-introduction-group
  124. Tutorials
  125. ---------
  126. - `GDScript basics: Dictionary <../tutorials/scripting/gdscript/gdscript_basics.html#dictionary>`__
  127. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/676>`__
  128. - `OS Test Demo <https://godotengine.org/asset-library/asset/677>`__
  129. .. rst-class:: classref-reftable-group
  130. Constructors
  131. ------------
  132. .. table::
  133. :widths: auto
  134. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------+
  135. | :ref:`Dictionary<class_Dictionary>` | :ref:`Dictionary<class_Dictionary_constructor_Dictionary>` **(** **)** |
  136. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------+
  137. | :ref:`Dictionary<class_Dictionary>` | :ref:`Dictionary<class_Dictionary_constructor_Dictionary>` **(** :ref:`Dictionary<class_Dictionary>` from **)** |
  138. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------+
  139. .. rst-class:: classref-reftable-group
  140. Methods
  141. -------
  142. .. table::
  143. :widths: auto
  144. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  145. | void | :ref:`clear<class_Dictionary_method_clear>` **(** **)** |
  146. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  147. | :ref:`Dictionary<class_Dictionary>` | :ref:`duplicate<class_Dictionary_method_duplicate>` **(** :ref:`bool<class_bool>` deep=false **)** |const| |
  148. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  149. | :ref:`bool<class_bool>` | :ref:`erase<class_Dictionary_method_erase>` **(** :ref:`Variant<class_Variant>` key **)** |
  150. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  151. | :ref:`Variant<class_Variant>` | :ref:`find_key<class_Dictionary_method_find_key>` **(** :ref:`Variant<class_Variant>` value **)** |const| |
  152. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  153. | :ref:`Variant<class_Variant>` | :ref:`get<class_Dictionary_method_get>` **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=null **)** |const| |
  154. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  155. | :ref:`bool<class_bool>` | :ref:`has<class_Dictionary_method_has>` **(** :ref:`Variant<class_Variant>` key **)** |const| |
  156. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  157. | :ref:`bool<class_bool>` | :ref:`has_all<class_Dictionary_method_has_all>` **(** :ref:`Array<class_Array>` keys **)** |const| |
  158. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  159. | :ref:`int<class_int>` | :ref:`hash<class_Dictionary_method_hash>` **(** **)** |const| |
  160. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  161. | :ref:`bool<class_bool>` | :ref:`is_empty<class_Dictionary_method_is_empty>` **(** **)** |const| |
  162. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  163. | :ref:`bool<class_bool>` | :ref:`is_read_only<class_Dictionary_method_is_read_only>` **(** **)** |const| |
  164. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  165. | :ref:`Array<class_Array>` | :ref:`keys<class_Dictionary_method_keys>` **(** **)** |const| |
  166. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  167. | void | :ref:`make_read_only<class_Dictionary_method_make_read_only>` **(** **)** |
  168. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  169. | void | :ref:`merge<class_Dictionary_method_merge>` **(** :ref:`Dictionary<class_Dictionary>` dictionary, :ref:`bool<class_bool>` overwrite=false **)** |
  170. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  171. | :ref:`int<class_int>` | :ref:`size<class_Dictionary_method_size>` **(** **)** |const| |
  172. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  173. | :ref:`Array<class_Array>` | :ref:`values<class_Dictionary_method_values>` **(** **)** |const| |
  174. +-------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------+
  175. .. rst-class:: classref-reftable-group
  176. Operators
  177. ---------
  178. .. table::
  179. :widths: auto
  180. +-------------------------------+--------------------------------------------------------------------------------------------------------------------+
  181. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Dictionary_operator_neq_Dictionary>` **(** :ref:`Dictionary<class_Dictionary>` right **)** |
  182. +-------------------------------+--------------------------------------------------------------------------------------------------------------------+
  183. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Dictionary_operator_eq_Dictionary>` **(** :ref:`Dictionary<class_Dictionary>` right **)** |
  184. +-------------------------------+--------------------------------------------------------------------------------------------------------------------+
  185. | :ref:`Variant<class_Variant>` | :ref:`operator []<class_Dictionary_operator_idx_Variant>` **(** :ref:`Variant<class_Variant>` key **)** |
  186. +-------------------------------+--------------------------------------------------------------------------------------------------------------------+
  187. .. rst-class:: classref-section-separator
  188. ----
  189. .. rst-class:: classref-descriptions-group
  190. Constructor Descriptions
  191. ------------------------
  192. .. _class_Dictionary_constructor_Dictionary:
  193. .. rst-class:: classref-constructor
  194. :ref:`Dictionary<class_Dictionary>` **Dictionary** **(** **)**
  195. Constructs an empty **Dictionary**.
  196. .. rst-class:: classref-item-separator
  197. ----
  198. .. rst-class:: classref-constructor
  199. :ref:`Dictionary<class_Dictionary>` **Dictionary** **(** :ref:`Dictionary<class_Dictionary>` from **)**
  200. Returns the same array as ``from``. If you need a copy of the array, use :ref:`duplicate<class_Dictionary_method_duplicate>`.
  201. .. rst-class:: classref-section-separator
  202. ----
  203. .. rst-class:: classref-descriptions-group
  204. Method Descriptions
  205. -------------------
  206. .. _class_Dictionary_method_clear:
  207. .. rst-class:: classref-method
  208. void **clear** **(** **)**
  209. Clears the dictionary, removing all entries from it.
  210. .. rst-class:: classref-item-separator
  211. ----
  212. .. _class_Dictionary_method_duplicate:
  213. .. rst-class:: classref-method
  214. :ref:`Dictionary<class_Dictionary>` **duplicate** **(** :ref:`bool<class_bool>` deep=false **)** |const|
  215. Creates and returns a new copy of the dictionary. If ``deep`` is ``true``, inner **Dictionary** and :ref:`Array<class_Array>` keys and values are also copied, recursively.
  216. .. rst-class:: classref-item-separator
  217. ----
  218. .. _class_Dictionary_method_erase:
  219. .. rst-class:: classref-method
  220. :ref:`bool<class_bool>` **erase** **(** :ref:`Variant<class_Variant>` key **)**
  221. Removes the dictionary entry by key, if it exists. Returns ``true`` if the given ``key`` existed in the dictionary, otherwise ``false``.
  222. \ **Note:** Do not erase entries while iterating over the dictionary. You can iterate over the :ref:`keys<class_Dictionary_method_keys>` array instead.
  223. .. rst-class:: classref-item-separator
  224. ----
  225. .. _class_Dictionary_method_find_key:
  226. .. rst-class:: classref-method
  227. :ref:`Variant<class_Variant>` **find_key** **(** :ref:`Variant<class_Variant>` value **)** |const|
  228. Finds and returns the first key whose associated value is equal to ``value``, or ``null`` if it is not found.
  229. \ **Note:** ``null`` is also a valid key. If inside the dictionary, :ref:`find_key<class_Dictionary_method_find_key>` may give misleading results.
  230. .. rst-class:: classref-item-separator
  231. ----
  232. .. _class_Dictionary_method_get:
  233. .. rst-class:: classref-method
  234. :ref:`Variant<class_Variant>` **get** **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=null **)** |const|
  235. Returns the corresponding value for the given ``key`` in the dictionary. If the ``key`` does not exist, returns ``default``, or ``null`` if the parameter is omitted.
  236. .. rst-class:: classref-item-separator
  237. ----
  238. .. _class_Dictionary_method_has:
  239. .. rst-class:: classref-method
  240. :ref:`bool<class_bool>` **has** **(** :ref:`Variant<class_Variant>` key **)** |const|
  241. Returns ``true`` if the dictionary contains an entry with the given ``key``.
  242. .. tabs::
  243. .. code-tab:: gdscript
  244. var my_dict = {
  245. "Godot" : 4,
  246. 210 : null,
  247. }
  248. print(my_dict.has("Godot")) # Prints true
  249. print(my_dict.has(210)) # Prints true
  250. print(my_dict.has(4)) # Prints false
  251. .. code-tab:: csharp
  252. var myDict = new Godot.Collections.Dictionary
  253. {
  254. { "Godot", 4 },
  255. { 210, default },
  256. };
  257. GD.Print(myDict.Contains("Godot")); // Prints true
  258. GD.Print(myDict.Contains(210)); // Prints true
  259. GD.Print(myDict.Contains(4)); // Prints false
  260. In GDScript, this is equivalent to the ``in`` operator:
  261. ::
  262. if "Godot" in {"Godot": 4}:
  263. print("The key is here!") # Will be printed.
  264. \ **Note:** This method returns ``true`` as long as the ``key`` exists, even if its corresponding value is ``null``.
  265. .. rst-class:: classref-item-separator
  266. ----
  267. .. _class_Dictionary_method_has_all:
  268. .. rst-class:: classref-method
  269. :ref:`bool<class_bool>` **has_all** **(** :ref:`Array<class_Array>` keys **)** |const|
  270. Returns ``true`` if the dictionary contains all keys in the given ``keys`` array.
  271. ::
  272. var data = {"width" : 10, "height" : 20}
  273. data.has_all(["height", "width"]) # Returns true
  274. .. rst-class:: classref-item-separator
  275. ----
  276. .. _class_Dictionary_method_hash:
  277. .. rst-class:: classref-method
  278. :ref:`int<class_int>` **hash** **(** **)** |const|
  279. Returns a hashed 32-bit integer value representing the dictionary contents.
  280. .. tabs::
  281. .. code-tab:: gdscript
  282. var dict1 = {"A": 10, "B": 2}
  283. var dict2 = {"A": 10, "B": 2}
  284. print(dict1.hash() == dict2.hash()) # Prints true
  285. .. code-tab:: csharp
  286. var dict1 = new Godot.Collections.Dictionary{{"A", 10}, {"B", 2}};
  287. var dict2 = new Godot.Collections.Dictionary{{"A", 10}, {"B", 2}};
  288. // Godot.Collections.Dictionary has no Hash() method. Use GD.Hash() instead.
  289. GD.Print(GD.Hash(dict1) == GD.Hash(dict2)); // Prints true
  290. \ **Note:** Dictionaries with the same entries but in a different order will not have the same hash.
  291. \ **Note:** Dictionaries with equal hash values are *not* guaranteed to be the same, because of hash collisions. On the countrary, dictionaries with different hash values are guaranteed to be different.
  292. .. rst-class:: classref-item-separator
  293. ----
  294. .. _class_Dictionary_method_is_empty:
  295. .. rst-class:: classref-method
  296. :ref:`bool<class_bool>` **is_empty** **(** **)** |const|
  297. Returns ``true`` if the dictionary is empty (its size is ``0``). See also :ref:`size<class_Dictionary_method_size>`.
  298. .. rst-class:: classref-item-separator
  299. ----
  300. .. _class_Dictionary_method_is_read_only:
  301. .. rst-class:: classref-method
  302. :ref:`bool<class_bool>` **is_read_only** **(** **)** |const|
  303. Returns ``true`` if the dictionary is read-only. See :ref:`make_read_only<class_Dictionary_method_make_read_only>`. Dictionaries are automatically read-only if declared with ``const`` keyword.
  304. .. rst-class:: classref-item-separator
  305. ----
  306. .. _class_Dictionary_method_keys:
  307. .. rst-class:: classref-method
  308. :ref:`Array<class_Array>` **keys** **(** **)** |const|
  309. Returns the list of keys in the dictionary.
  310. .. rst-class:: classref-item-separator
  311. ----
  312. .. _class_Dictionary_method_make_read_only:
  313. .. rst-class:: classref-method
  314. void **make_read_only** **(** **)**
  315. Makes the dictionary read-only, i.e. disabled modifying of the dictionary's contents. Does not apply to nested content, e.g. content of nested dicitonaries.
  316. .. rst-class:: classref-item-separator
  317. ----
  318. .. _class_Dictionary_method_merge:
  319. .. rst-class:: classref-method
  320. void **merge** **(** :ref:`Dictionary<class_Dictionary>` dictionary, :ref:`bool<class_bool>` overwrite=false **)**
  321. Adds entries from ``dictionary`` to this dictionary. By default, duplicate keys are not copied over, unless ``overwrite`` is ``true``.
  322. .. rst-class:: classref-item-separator
  323. ----
  324. .. _class_Dictionary_method_size:
  325. .. rst-class:: classref-method
  326. :ref:`int<class_int>` **size** **(** **)** |const|
  327. Returns the number of entries in the dictionary. Empty dictionaries (``{ }``) always return ``0``. See also :ref:`is_empty<class_Dictionary_method_is_empty>`.
  328. .. rst-class:: classref-item-separator
  329. ----
  330. .. _class_Dictionary_method_values:
  331. .. rst-class:: classref-method
  332. :ref:`Array<class_Array>` **values** **(** **)** |const|
  333. Returns the list of values in this dictionary.
  334. .. rst-class:: classref-section-separator
  335. ----
  336. .. rst-class:: classref-descriptions-group
  337. Operator Descriptions
  338. ---------------------
  339. .. _class_Dictionary_operator_neq_Dictionary:
  340. .. rst-class:: classref-operator
  341. :ref:`bool<class_bool>` **operator !=** **(** :ref:`Dictionary<class_Dictionary>` right **)**
  342. Returns ``true`` if the two dictionaries do not contain the same keys and values.
  343. .. rst-class:: classref-item-separator
  344. ----
  345. .. _class_Dictionary_operator_eq_Dictionary:
  346. .. rst-class:: classref-operator
  347. :ref:`bool<class_bool>` **operator ==** **(** :ref:`Dictionary<class_Dictionary>` right **)**
  348. Returns ``true`` if the two dictionaries contain the same keys and values. The order of the entries does not matter.
  349. \ **Note:** In C#, by convention, this operator compares by **reference**. If you need to compare by value, iterate over both dictionaries.
  350. .. rst-class:: classref-item-separator
  351. ----
  352. .. _class_Dictionary_operator_idx_Variant:
  353. .. rst-class:: classref-operator
  354. :ref:`Variant<class_Variant>` **operator []** **(** :ref:`Variant<class_Variant>` key **)**
  355. Returns the corresponding value for the given ``key`` in the dictionary. If the entry does not exist, fails and returns ``null``. For safe access, use :ref:`get<class_Dictionary_method_get>` or :ref:`has<class_Dictionary_method_has>`.
  356. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  357. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  358. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  359. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  360. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  361. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`