class_dictionary.rst 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. A built-in data structure that holds key-value pairs.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. Dictionaries are associative containers that contain values referenced by unique keys. Dictionaries will preserve the insertion order when adding new entries. In other programming languages, this data structure is often referred to as a hash map or an associative array.
  14. You can define a dictionary by placing a comma-separated list of ``key: value`` pairs inside curly braces ``{}``.
  15. Creating a dictionary:
  16. .. tabs::
  17. .. code-tab:: gdscript
  18. var my_dict = {} # Creates an empty dictionary.
  19. var dict_variable_key = "Another key name"
  20. var dict_variable_value = "value2"
  21. var another_dict = {
  22. "Some key name": "value1",
  23. dict_variable_key: dict_variable_value,
  24. }
  25. var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 }
  26. # Alternative Lua-style syntax.
  27. # Doesn't require quotes around keys, but only string constants can be used as key names.
  28. # Additionally, key names must start with a letter or an underscore.
  29. # Here, `some_key` is a string literal, not a variable!
  30. another_dict = {
  31. some_key = 42,
  32. }
  33. .. code-tab:: csharp
  34. var myDict = new Godot.Collections.Dictionary(); // Creates an empty dictionary.
  35. var pointsDict = new Godot.Collections.Dictionary
  36. {
  37. { "White", 50 },
  38. { "Yellow", 75 },
  39. { "Orange", 100 },
  40. };
  41. 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).
  42. .. tabs::
  43. .. code-tab:: gdscript
  44. @export_enum("White", "Yellow", "Orange") var my_color: String
  45. var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 }
  46. func _ready():
  47. # We can't use dot syntax here as `my_color` is a variable.
  48. var points = points_dict[my_color]
  49. .. code-tab:: csharp
  50. [Export(PropertyHint.Enum, "White,Yellow,Orange")]
  51. public string MyColor { get; set; }
  52. private Godot.Collections.Dictionary _pointsDict = new Godot.Collections.Dictionary
  53. {
  54. { "White", 50 },
  55. { "Yellow", 75 },
  56. { "Orange", 100 },
  57. };
  58. public override void _Ready()
  59. {
  60. int points = (int)_pointsDict[MyColor];
  61. }
  62. In the above code, ``points`` will be assigned the value that is paired with the appropriate color selected in ``my_color``.
  63. Dictionaries can contain more complex data:
  64. .. tabs::
  65. .. code-tab:: gdscript
  66. var my_dict = {
  67. "First Array": [1, 2, 3, 4] # Assigns an Array to a String key.
  68. }
  69. .. code-tab:: csharp
  70. var myDict = new Godot.Collections.Dictionary
  71. {
  72. { "First Array", new Godot.Collections.Array { 1, 2, 3, 4 } }
  73. };
  74. To add a key to an existing dictionary, access it like an existing key and assign to it:
  75. .. tabs::
  76. .. code-tab:: gdscript
  77. var points_dict = { "White": 50, "Yellow": 75, "Orange": 100 }
  78. points_dict["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value.
  79. .. code-tab:: csharp
  80. var pointsDict = new Godot.Collections.Dictionary
  81. {
  82. { "White", 50 },
  83. { "Yellow", 75 },
  84. { "Orange", 100 },
  85. };
  86. pointsDict["Blue"] = 150; // Add "Blue" as a key and assign 150 as its value.
  87. Finally, untyped dictionaries can contain different types of keys and values in the same dictionary:
  88. .. tabs::
  89. .. code-tab:: gdscript
  90. # This is a valid dictionary.
  91. # To access the string "Nested value" below, use `my_dict.sub_dict.sub_key` or `my_dict["sub_dict"]["sub_key"]`.
  92. # Indexing styles can be mixed and matched depending on your needs.
  93. var my_dict = {
  94. "String Key": 5,
  95. 4: [1, 2, 3],
  96. 7: "Hello",
  97. "sub_dict": { "sub_key": "Nested value" },
  98. }
  99. .. code-tab:: csharp
  100. // This is a valid dictionary.
  101. // To access the string "Nested value" below, use `((Godot.Collections.Dictionary)myDict["sub_dict"])["sub_key"]`.
  102. var myDict = new Godot.Collections.Dictionary {
  103. { "String Key", 5 },
  104. { 4, new Godot.Collections.Array { 1, 2, 3 } },
  105. { 7, "Hello" },
  106. { "sub_dict", new Godot.Collections.Dictionary { { "sub_key", "Nested value" } } },
  107. };
  108. The keys of a dictionary can be iterated with the ``for`` keyword:
  109. .. tabs::
  110. .. code-tab:: gdscript
  111. var groceries = { "Orange": 20, "Apple": 2, "Banana": 4 }
  112. for fruit in groceries:
  113. var amount = groceries[fruit]
  114. .. code-tab:: csharp
  115. var groceries = new Godot.Collections.Dictionary { { "Orange", 20 }, { "Apple", 2 }, { "Banana", 4 } };
  116. foreach (var (fruit, amount) in groceries)
  117. {
  118. // `fruit` is the key, `amount` is the value.
  119. }
  120. To enforce a certain type for keys and values, you can create a *typed dictionary*. Typed dictionaries can only contain keys and values of the given types, or that inherit from the given classes:
  121. .. tabs::
  122. .. code-tab:: gdscript
  123. # Creates a typed dictionary with String keys and int values.
  124. # Attempting to use any other type for keys or values will result in an error.
  125. var typed_dict: Dictionary[String, int] = {
  126. "some_key": 1,
  127. "some_other_key": 2,
  128. }
  129. # Creates a typed dictionary with String keys and values of any type.
  130. # Attempting to use any other type for keys will result in an error.
  131. var typed_dict_key_only: Dictionary[String, Variant] = {
  132. "some_key": 12.34,
  133. "some_other_key": "string",
  134. }
  135. .. code-tab:: csharp
  136. // Creates a typed dictionary with String keys and int values.
  137. // Attempting to use any other type for keys or values will result in an error.
  138. var typedDict = new Godot.Collections.Dictionary<String, int> {
  139. {"some_key", 1},
  140. {"some_other_key", 2},
  141. };
  142. // Creates a typed dictionary with String keys and values of any type.
  143. // Attempting to use any other type for keys will result in an error.
  144. var typedDictKeyOnly = new Godot.Collections.Dictionary<String, Variant> {
  145. {"some_key", 12.34},
  146. {"some_other_key", "string"},
  147. };
  148. \ **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>`.
  149. \ **Note:** Erasing elements while iterating over dictionaries is **not** supported and will result in unpredictable behavior.
  150. .. note::
  151. There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information.
  152. .. rst-class:: classref-introduction-group
  153. Tutorials
  154. ---------
  155. - `GDScript basics: Dictionary <../tutorials/scripting/gdscript/gdscript_basics.html#dictionary>`__
  156. - `3D Voxel Demo <https://godotengine.org/asset-library/asset/2755>`__
  157. - `Operating System Testing Demo <https://godotengine.org/asset-library/asset/2789>`__
  158. .. rst-class:: classref-reftable-group
  159. Constructors
  160. ------------
  161. .. table::
  162. :widths: auto
  163. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  164. | :ref:`Dictionary<class_Dictionary>` | :ref:`Dictionary<class_Dictionary_constructor_Dictionary>`\ (\ ) |
  165. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  166. | :ref:`Dictionary<class_Dictionary>` | :ref:`Dictionary<class_Dictionary_constructor_Dictionary>`\ (\ base\: :ref:`Dictionary<class_Dictionary>`, key_type\: :ref:`int<class_int>`, key_class_name\: :ref:`StringName<class_StringName>`, key_script\: :ref:`Variant<class_Variant>`, value_type\: :ref:`int<class_int>`, value_class_name\: :ref:`StringName<class_StringName>`, value_script\: :ref:`Variant<class_Variant>`\ ) |
  167. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  168. | :ref:`Dictionary<class_Dictionary>` | :ref:`Dictionary<class_Dictionary_constructor_Dictionary>`\ (\ from\: :ref:`Dictionary<class_Dictionary>`\ ) |
  169. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  170. .. rst-class:: classref-reftable-group
  171. Methods
  172. -------
  173. .. table::
  174. :widths: auto
  175. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  176. | |void| | :ref:`assign<class_Dictionary_method_assign>`\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) |
  177. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  178. | |void| | :ref:`clear<class_Dictionary_method_clear>`\ (\ ) |
  179. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  180. | :ref:`Dictionary<class_Dictionary>` | :ref:`duplicate<class_Dictionary_method_duplicate>`\ (\ deep\: :ref:`bool<class_bool>` = false\ ) |const| |
  181. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  182. | :ref:`Dictionary<class_Dictionary>` | :ref:`duplicate_deep<class_Dictionary_method_duplicate_deep>`\ (\ deep_subresources_mode\: :ref:`int<class_int>` = 1\ ) |const| |
  183. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  184. | :ref:`bool<class_bool>` | :ref:`erase<class_Dictionary_method_erase>`\ (\ key\: :ref:`Variant<class_Variant>`\ ) |
  185. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  186. | :ref:`Variant<class_Variant>` | :ref:`find_key<class_Dictionary_method_find_key>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| |
  187. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  188. | :ref:`Variant<class_Variant>` | :ref:`get<class_Dictionary_method_get>`\ (\ key\: :ref:`Variant<class_Variant>`, default\: :ref:`Variant<class_Variant>` = null\ ) |const| |
  189. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  190. | :ref:`Variant<class_Variant>` | :ref:`get_or_add<class_Dictionary_method_get_or_add>`\ (\ key\: :ref:`Variant<class_Variant>`, default\: :ref:`Variant<class_Variant>` = null\ ) |
  191. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  192. | :ref:`int<class_int>` | :ref:`get_typed_key_builtin<class_Dictionary_method_get_typed_key_builtin>`\ (\ ) |const| |
  193. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  194. | :ref:`StringName<class_StringName>` | :ref:`get_typed_key_class_name<class_Dictionary_method_get_typed_key_class_name>`\ (\ ) |const| |
  195. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  196. | :ref:`Variant<class_Variant>` | :ref:`get_typed_key_script<class_Dictionary_method_get_typed_key_script>`\ (\ ) |const| |
  197. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  198. | :ref:`int<class_int>` | :ref:`get_typed_value_builtin<class_Dictionary_method_get_typed_value_builtin>`\ (\ ) |const| |
  199. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  200. | :ref:`StringName<class_StringName>` | :ref:`get_typed_value_class_name<class_Dictionary_method_get_typed_value_class_name>`\ (\ ) |const| |
  201. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  202. | :ref:`Variant<class_Variant>` | :ref:`get_typed_value_script<class_Dictionary_method_get_typed_value_script>`\ (\ ) |const| |
  203. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  204. | :ref:`bool<class_bool>` | :ref:`has<class_Dictionary_method_has>`\ (\ key\: :ref:`Variant<class_Variant>`\ ) |const| |
  205. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  206. | :ref:`bool<class_bool>` | :ref:`has_all<class_Dictionary_method_has_all>`\ (\ keys\: :ref:`Array<class_Array>`\ ) |const| |
  207. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  208. | :ref:`int<class_int>` | :ref:`hash<class_Dictionary_method_hash>`\ (\ ) |const| |
  209. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  210. | :ref:`bool<class_bool>` | :ref:`is_empty<class_Dictionary_method_is_empty>`\ (\ ) |const| |
  211. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  212. | :ref:`bool<class_bool>` | :ref:`is_read_only<class_Dictionary_method_is_read_only>`\ (\ ) |const| |
  213. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  214. | :ref:`bool<class_bool>` | :ref:`is_same_typed<class_Dictionary_method_is_same_typed>`\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) |const| |
  215. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  216. | :ref:`bool<class_bool>` | :ref:`is_same_typed_key<class_Dictionary_method_is_same_typed_key>`\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) |const| |
  217. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  218. | :ref:`bool<class_bool>` | :ref:`is_same_typed_value<class_Dictionary_method_is_same_typed_value>`\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) |const| |
  219. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  220. | :ref:`bool<class_bool>` | :ref:`is_typed<class_Dictionary_method_is_typed>`\ (\ ) |const| |
  221. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  222. | :ref:`bool<class_bool>` | :ref:`is_typed_key<class_Dictionary_method_is_typed_key>`\ (\ ) |const| |
  223. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  224. | :ref:`bool<class_bool>` | :ref:`is_typed_value<class_Dictionary_method_is_typed_value>`\ (\ ) |const| |
  225. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  226. | :ref:`Array<class_Array>` | :ref:`keys<class_Dictionary_method_keys>`\ (\ ) |const| |
  227. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  228. | |void| | :ref:`make_read_only<class_Dictionary_method_make_read_only>`\ (\ ) |
  229. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  230. | |void| | :ref:`merge<class_Dictionary_method_merge>`\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`, overwrite\: :ref:`bool<class_bool>` = false\ ) |
  231. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  232. | :ref:`Dictionary<class_Dictionary>` | :ref:`merged<class_Dictionary_method_merged>`\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`, overwrite\: :ref:`bool<class_bool>` = false\ ) |const| |
  233. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  234. | :ref:`bool<class_bool>` | :ref:`recursive_equal<class_Dictionary_method_recursive_equal>`\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`, recursion_count\: :ref:`int<class_int>`\ ) |const| |
  235. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  236. | :ref:`bool<class_bool>` | :ref:`set<class_Dictionary_method_set>`\ (\ key\: :ref:`Variant<class_Variant>`, value\: :ref:`Variant<class_Variant>`\ ) |
  237. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  238. | :ref:`int<class_int>` | :ref:`size<class_Dictionary_method_size>`\ (\ ) |const| |
  239. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  240. | |void| | :ref:`sort<class_Dictionary_method_sort>`\ (\ ) |
  241. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  242. | :ref:`Array<class_Array>` | :ref:`values<class_Dictionary_method_values>`\ (\ ) |const| |
  243. +-------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  244. .. rst-class:: classref-reftable-group
  245. Operators
  246. ---------
  247. .. table::
  248. :widths: auto
  249. +-------------------------------+-----------------------------------------------------------------------------------------------------------------+
  250. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Dictionary_operator_neq_Dictionary>`\ (\ right\: :ref:`Dictionary<class_Dictionary>`\ ) |
  251. +-------------------------------+-----------------------------------------------------------------------------------------------------------------+
  252. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Dictionary_operator_eq_Dictionary>`\ (\ right\: :ref:`Dictionary<class_Dictionary>`\ ) |
  253. +-------------------------------+-----------------------------------------------------------------------------------------------------------------+
  254. | :ref:`Variant<class_Variant>` | :ref:`operator []<class_Dictionary_operator_idx_Variant>`\ (\ key\: :ref:`Variant<class_Variant>`\ ) |
  255. +-------------------------------+-----------------------------------------------------------------------------------------------------------------+
  256. .. rst-class:: classref-section-separator
  257. ----
  258. .. rst-class:: classref-descriptions-group
  259. Constructor Descriptions
  260. ------------------------
  261. .. _class_Dictionary_constructor_Dictionary:
  262. .. rst-class:: classref-constructor
  263. :ref:`Dictionary<class_Dictionary>` **Dictionary**\ (\ ) :ref:`🔗<class_Dictionary_constructor_Dictionary>`
  264. Constructs an empty **Dictionary**.
  265. .. rst-class:: classref-item-separator
  266. ----
  267. .. rst-class:: classref-constructor
  268. :ref:`Dictionary<class_Dictionary>` **Dictionary**\ (\ base\: :ref:`Dictionary<class_Dictionary>`, key_type\: :ref:`int<class_int>`, key_class_name\: :ref:`StringName<class_StringName>`, key_script\: :ref:`Variant<class_Variant>`, value_type\: :ref:`int<class_int>`, value_class_name\: :ref:`StringName<class_StringName>`, value_script\: :ref:`Variant<class_Variant>`\ )
  269. Creates a typed dictionary from the ``base`` dictionary. A typed dictionary can only contain keys and values of the given types, or that inherit from the given classes, as described by this constructor's parameters.
  270. .. rst-class:: classref-item-separator
  271. ----
  272. .. rst-class:: classref-constructor
  273. :ref:`Dictionary<class_Dictionary>` **Dictionary**\ (\ from\: :ref:`Dictionary<class_Dictionary>`\ )
  274. Returns the same dictionary as ``from``. If you need a copy of the dictionary, use :ref:`duplicate()<class_Dictionary_method_duplicate>`.
  275. .. rst-class:: classref-section-separator
  276. ----
  277. .. rst-class:: classref-descriptions-group
  278. Method Descriptions
  279. -------------------
  280. .. _class_Dictionary_method_assign:
  281. .. rst-class:: classref-method
  282. |void| **assign**\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) :ref:`🔗<class_Dictionary_method_assign>`
  283. Assigns elements of another ``dictionary`` into the dictionary. Resizes the dictionary to match ``dictionary``. Performs type conversions if the dictionary is typed.
  284. .. rst-class:: classref-item-separator
  285. ----
  286. .. _class_Dictionary_method_clear:
  287. .. rst-class:: classref-method
  288. |void| **clear**\ (\ ) :ref:`🔗<class_Dictionary_method_clear>`
  289. Clears the dictionary, removing all entries from it.
  290. .. rst-class:: classref-item-separator
  291. ----
  292. .. _class_Dictionary_method_duplicate:
  293. .. rst-class:: classref-method
  294. :ref:`Dictionary<class_Dictionary>` **duplicate**\ (\ deep\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`🔗<class_Dictionary_method_duplicate>`
  295. Returns a new copy of the dictionary.
  296. By default, a **shallow** copy is returned: all nested :ref:`Array<class_Array>`, **Dictionary**, and :ref:`Resource<class_Resource>` keys and values are shared with the original dictionary. Modifying any of those in one dictionary will also affect them in the other.
  297. If ``deep`` is ``true``, a **deep** copy is returned: all nested arrays and dictionaries are also duplicated (recursively). Any :ref:`Resource<class_Resource>` is still shared with the original dictionary, though.
  298. .. rst-class:: classref-item-separator
  299. ----
  300. .. _class_Dictionary_method_duplicate_deep:
  301. .. rst-class:: classref-method
  302. :ref:`Dictionary<class_Dictionary>` **duplicate_deep**\ (\ deep_subresources_mode\: :ref:`int<class_int>` = 1\ ) |const| :ref:`🔗<class_Dictionary_method_duplicate_deep>`
  303. Duplicates this dictionary, deeply, like :ref:`duplicate()<class_Dictionary_method_duplicate>`\ ``(true)``, with extra control over how subresources are handled.
  304. \ ``deep_subresources_mode`` must be one of the values from :ref:`DeepDuplicateMode<enum_Resource_DeepDuplicateMode>`. By default, only internal resources will be duplicated (recursively).
  305. .. rst-class:: classref-item-separator
  306. ----
  307. .. _class_Dictionary_method_erase:
  308. .. rst-class:: classref-method
  309. :ref:`bool<class_bool>` **erase**\ (\ key\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Dictionary_method_erase>`
  310. Removes the dictionary entry by key, if it exists. Returns ``true`` if the given ``key`` existed in the dictionary, otherwise ``false``.
  311. \ **Note:** Do not erase entries while iterating over the dictionary. You can iterate over the :ref:`keys()<class_Dictionary_method_keys>` array instead.
  312. .. rst-class:: classref-item-separator
  313. ----
  314. .. _class_Dictionary_method_find_key:
  315. .. rst-class:: classref-method
  316. :ref:`Variant<class_Variant>` **find_key**\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| :ref:`🔗<class_Dictionary_method_find_key>`
  317. Finds and returns the first key whose associated value is equal to ``value``, or ``null`` if it is not found.
  318. \ **Note:** ``null`` is also a valid key. If inside the dictionary, :ref:`find_key()<class_Dictionary_method_find_key>` may give misleading results.
  319. .. rst-class:: classref-item-separator
  320. ----
  321. .. _class_Dictionary_method_get:
  322. .. rst-class:: classref-method
  323. :ref:`Variant<class_Variant>` **get**\ (\ key\: :ref:`Variant<class_Variant>`, default\: :ref:`Variant<class_Variant>` = null\ ) |const| :ref:`🔗<class_Dictionary_method_get>`
  324. 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.
  325. .. rst-class:: classref-item-separator
  326. ----
  327. .. _class_Dictionary_method_get_or_add:
  328. .. rst-class:: classref-method
  329. :ref:`Variant<class_Variant>` **get_or_add**\ (\ key\: :ref:`Variant<class_Variant>`, default\: :ref:`Variant<class_Variant>` = null\ ) :ref:`🔗<class_Dictionary_method_get_or_add>`
  330. 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.
  331. .. rst-class:: classref-item-separator
  332. ----
  333. .. _class_Dictionary_method_get_typed_key_builtin:
  334. .. rst-class:: classref-method
  335. :ref:`int<class_int>` **get_typed_key_builtin**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_get_typed_key_builtin>`
  336. Returns the built-in :ref:`Variant<class_Variant>` type of the typed dictionary's keys as a :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` constant. If the keys are not typed, returns :ref:`@GlobalScope.TYPE_NIL<class_@GlobalScope_constant_TYPE_NIL>`. See also :ref:`is_typed_key()<class_Dictionary_method_is_typed_key>`.
  337. .. rst-class:: classref-item-separator
  338. ----
  339. .. _class_Dictionary_method_get_typed_key_class_name:
  340. .. rst-class:: classref-method
  341. :ref:`StringName<class_StringName>` **get_typed_key_class_name**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_get_typed_key_class_name>`
  342. Returns the **built-in** class name of the typed dictionary's keys, if the built-in :ref:`Variant<class_Variant>` type is :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`. Otherwise, returns an empty :ref:`StringName<class_StringName>`. See also :ref:`is_typed_key()<class_Dictionary_method_is_typed_key>` and :ref:`Object.get_class()<class_Object_method_get_class>`.
  343. .. rst-class:: classref-item-separator
  344. ----
  345. .. _class_Dictionary_method_get_typed_key_script:
  346. .. rst-class:: classref-method
  347. :ref:`Variant<class_Variant>` **get_typed_key_script**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_get_typed_key_script>`
  348. Returns the :ref:`Script<class_Script>` instance associated with this typed dictionary's keys, or ``null`` if it does not exist. See also :ref:`is_typed_key()<class_Dictionary_method_is_typed_key>`.
  349. .. rst-class:: classref-item-separator
  350. ----
  351. .. _class_Dictionary_method_get_typed_value_builtin:
  352. .. rst-class:: classref-method
  353. :ref:`int<class_int>` **get_typed_value_builtin**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_get_typed_value_builtin>`
  354. Returns the built-in :ref:`Variant<class_Variant>` type of the typed dictionary's values as a :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` constant. If the values are not typed, returns :ref:`@GlobalScope.TYPE_NIL<class_@GlobalScope_constant_TYPE_NIL>`. See also :ref:`is_typed_value()<class_Dictionary_method_is_typed_value>`.
  355. .. rst-class:: classref-item-separator
  356. ----
  357. .. _class_Dictionary_method_get_typed_value_class_name:
  358. .. rst-class:: classref-method
  359. :ref:`StringName<class_StringName>` **get_typed_value_class_name**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_get_typed_value_class_name>`
  360. Returns the **built-in** class name of the typed dictionary's values, if the built-in :ref:`Variant<class_Variant>` type is :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`. Otherwise, returns an empty :ref:`StringName<class_StringName>`. See also :ref:`is_typed_value()<class_Dictionary_method_is_typed_value>` and :ref:`Object.get_class()<class_Object_method_get_class>`.
  361. .. rst-class:: classref-item-separator
  362. ----
  363. .. _class_Dictionary_method_get_typed_value_script:
  364. .. rst-class:: classref-method
  365. :ref:`Variant<class_Variant>` **get_typed_value_script**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_get_typed_value_script>`
  366. Returns the :ref:`Script<class_Script>` instance associated with this typed dictionary's values, or ``null`` if it does not exist. See also :ref:`is_typed_value()<class_Dictionary_method_is_typed_value>`.
  367. .. rst-class:: classref-item-separator
  368. ----
  369. .. _class_Dictionary_method_has:
  370. .. rst-class:: classref-method
  371. :ref:`bool<class_bool>` **has**\ (\ key\: :ref:`Variant<class_Variant>`\ ) |const| :ref:`🔗<class_Dictionary_method_has>`
  372. Returns ``true`` if the dictionary contains an entry with the given ``key``.
  373. .. tabs::
  374. .. code-tab:: gdscript
  375. var my_dict = {
  376. "Godot" : 4,
  377. 210 : null,
  378. }
  379. print(my_dict.has("Godot")) # Prints true
  380. print(my_dict.has(210)) # Prints true
  381. print(my_dict.has(4)) # Prints false
  382. .. code-tab:: csharp
  383. var myDict = new Godot.Collections.Dictionary
  384. {
  385. { "Godot", 4 },
  386. { 210, default },
  387. };
  388. GD.Print(myDict.ContainsKey("Godot")); // Prints True
  389. GD.Print(myDict.ContainsKey(210)); // Prints True
  390. GD.Print(myDict.ContainsKey(4)); // Prints False
  391. In GDScript, this is equivalent to the ``in`` operator:
  392. ::
  393. if "Godot" in { "Godot": 4 }:
  394. print("The key is here!") # Will be printed.
  395. \ **Note:** This method returns ``true`` as long as the ``key`` exists, even if its corresponding value is ``null``.
  396. .. rst-class:: classref-item-separator
  397. ----
  398. .. _class_Dictionary_method_has_all:
  399. .. rst-class:: classref-method
  400. :ref:`bool<class_bool>` **has_all**\ (\ keys\: :ref:`Array<class_Array>`\ ) |const| :ref:`🔗<class_Dictionary_method_has_all>`
  401. Returns ``true`` if the dictionary contains all keys in the given ``keys`` array.
  402. ::
  403. var data = { "width": 10, "height": 20 }
  404. data.has_all(["height", "width"]) # Returns true
  405. .. rst-class:: classref-item-separator
  406. ----
  407. .. _class_Dictionary_method_hash:
  408. .. rst-class:: classref-method
  409. :ref:`int<class_int>` **hash**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_hash>`
  410. Returns a hashed 32-bit integer value representing the dictionary contents.
  411. .. tabs::
  412. .. code-tab:: gdscript
  413. var dict1 = { "A": 10, "B": 2 }
  414. var dict2 = { "A": 10, "B": 2 }
  415. print(dict1.hash() == dict2.hash()) # Prints true
  416. .. code-tab:: csharp
  417. var dict1 = new Godot.Collections.Dictionary { { "A", 10 }, { "B", 2 } };
  418. var dict2 = new Godot.Collections.Dictionary { { "A", 10 }, { "B", 2 } };
  419. // Godot.Collections.Dictionary has no Hash() method. Use GD.Hash() instead.
  420. GD.Print(GD.Hash(dict1) == GD.Hash(dict2)); // Prints True
  421. \ **Note:** Dictionaries with the same entries but in a different order will not have the same hash.
  422. \ **Note:** Dictionaries with equal hash values are *not* guaranteed to be the same, because of hash collisions. On the contrary, dictionaries with different hash values are guaranteed to be different.
  423. .. rst-class:: classref-item-separator
  424. ----
  425. .. _class_Dictionary_method_is_empty:
  426. .. rst-class:: classref-method
  427. :ref:`bool<class_bool>` **is_empty**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_is_empty>`
  428. Returns ``true`` if the dictionary is empty (its size is ``0``). See also :ref:`size()<class_Dictionary_method_size>`.
  429. .. rst-class:: classref-item-separator
  430. ----
  431. .. _class_Dictionary_method_is_read_only:
  432. .. rst-class:: classref-method
  433. :ref:`bool<class_bool>` **is_read_only**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_is_read_only>`
  434. 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.
  435. .. rst-class:: classref-item-separator
  436. ----
  437. .. _class_Dictionary_method_is_same_typed:
  438. .. rst-class:: classref-method
  439. :ref:`bool<class_bool>` **is_same_typed**\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) |const| :ref:`🔗<class_Dictionary_method_is_same_typed>`
  440. Returns ``true`` if the dictionary is typed the same as ``dictionary``.
  441. .. rst-class:: classref-item-separator
  442. ----
  443. .. _class_Dictionary_method_is_same_typed_key:
  444. .. rst-class:: classref-method
  445. :ref:`bool<class_bool>` **is_same_typed_key**\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) |const| :ref:`🔗<class_Dictionary_method_is_same_typed_key>`
  446. Returns ``true`` if the dictionary's keys are typed the same as ``dictionary``'s keys.
  447. .. rst-class:: classref-item-separator
  448. ----
  449. .. _class_Dictionary_method_is_same_typed_value:
  450. .. rst-class:: classref-method
  451. :ref:`bool<class_bool>` **is_same_typed_value**\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`\ ) |const| :ref:`🔗<class_Dictionary_method_is_same_typed_value>`
  452. Returns ``true`` if the dictionary's values are typed the same as ``dictionary``'s values.
  453. .. rst-class:: classref-item-separator
  454. ----
  455. .. _class_Dictionary_method_is_typed:
  456. .. rst-class:: classref-method
  457. :ref:`bool<class_bool>` **is_typed**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_is_typed>`
  458. Returns ``true`` if the dictionary is typed. Typed dictionaries can only store keys/values of their associated type and provide type safety for the ``[]`` operator. Methods of typed dictionary still return :ref:`Variant<class_Variant>`.
  459. .. rst-class:: classref-item-separator
  460. ----
  461. .. _class_Dictionary_method_is_typed_key:
  462. .. rst-class:: classref-method
  463. :ref:`bool<class_bool>` **is_typed_key**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_is_typed_key>`
  464. Returns ``true`` if the dictionary's keys are typed.
  465. .. rst-class:: classref-item-separator
  466. ----
  467. .. _class_Dictionary_method_is_typed_value:
  468. .. rst-class:: classref-method
  469. :ref:`bool<class_bool>` **is_typed_value**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_is_typed_value>`
  470. Returns ``true`` if the dictionary's values are typed.
  471. .. rst-class:: classref-item-separator
  472. ----
  473. .. _class_Dictionary_method_keys:
  474. .. rst-class:: classref-method
  475. :ref:`Array<class_Array>` **keys**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_keys>`
  476. Returns the list of keys in the dictionary.
  477. .. rst-class:: classref-item-separator
  478. ----
  479. .. _class_Dictionary_method_make_read_only:
  480. .. rst-class:: classref-method
  481. |void| **make_read_only**\ (\ ) :ref:`🔗<class_Dictionary_method_make_read_only>`
  482. Makes the dictionary read-only, i.e. disables modification of the dictionary's contents. Does not apply to nested content, e.g. content of nested dictionaries.
  483. .. rst-class:: classref-item-separator
  484. ----
  485. .. _class_Dictionary_method_merge:
  486. .. rst-class:: classref-method
  487. |void| **merge**\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`, overwrite\: :ref:`bool<class_bool>` = false\ ) :ref:`🔗<class_Dictionary_method_merge>`
  488. Adds entries from ``dictionary`` to this dictionary. By default, duplicate keys are not copied over, unless ``overwrite`` is ``true``.
  489. .. tabs::
  490. .. code-tab:: gdscript
  491. var dict = { "item": "sword", "quantity": 2 }
  492. var other_dict = { "quantity": 15, "color": "silver" }
  493. # Overwriting of existing keys is disabled by default.
  494. dict.merge(other_dict)
  495. print(dict) # { "item": "sword", "quantity": 2, "color": "silver" }
  496. # With overwriting of existing keys enabled.
  497. dict.merge(other_dict, true)
  498. print(dict) # { "item": "sword", "quantity": 15, "color": "silver" }
  499. .. code-tab:: csharp
  500. var dict = new Godot.Collections.Dictionary
  501. {
  502. ["item"] = "sword",
  503. ["quantity"] = 2,
  504. };
  505. var otherDict = new Godot.Collections.Dictionary
  506. {
  507. ["quantity"] = 15,
  508. ["color"] = "silver",
  509. };
  510. // Overwriting of existing keys is disabled by default.
  511. dict.Merge(otherDict);
  512. GD.Print(dict); // { "item": "sword", "quantity": 2, "color": "silver" }
  513. // With overwriting of existing keys enabled.
  514. dict.Merge(otherDict, true);
  515. GD.Print(dict); // { "item": "sword", "quantity": 15, "color": "silver" }
  516. \ **Note:** :ref:`merge()<class_Dictionary_method_merge>` is *not* recursive. Nested dictionaries are considered as keys that can be overwritten or not depending on the value of ``overwrite``, but they will never be merged together.
  517. .. rst-class:: classref-item-separator
  518. ----
  519. .. _class_Dictionary_method_merged:
  520. .. rst-class:: classref-method
  521. :ref:`Dictionary<class_Dictionary>` **merged**\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`, overwrite\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`🔗<class_Dictionary_method_merged>`
  522. Returns a copy of this dictionary merged with the other ``dictionary``. By default, duplicate keys are not copied over, unless ``overwrite`` is ``true``. See also :ref:`merge()<class_Dictionary_method_merge>`.
  523. This method is useful for quickly making dictionaries with default values:
  524. ::
  525. var base = { "fruit": "apple", "vegetable": "potato" }
  526. var extra = { "fruit": "orange", "dressing": "vinegar" }
  527. # Prints { "fruit": "orange", "vegetable": "potato", "dressing": "vinegar" }
  528. print(extra.merged(base))
  529. # Prints { "fruit": "apple", "vegetable": "potato", "dressing": "vinegar" }
  530. print(extra.merged(base, true))
  531. .. rst-class:: classref-item-separator
  532. ----
  533. .. _class_Dictionary_method_recursive_equal:
  534. .. rst-class:: classref-method
  535. :ref:`bool<class_bool>` **recursive_equal**\ (\ dictionary\: :ref:`Dictionary<class_Dictionary>`, recursion_count\: :ref:`int<class_int>`\ ) |const| :ref:`🔗<class_Dictionary_method_recursive_equal>`
  536. Returns ``true`` if the two dictionaries contain the same keys and values, inner **Dictionary** and :ref:`Array<class_Array>` keys and values are compared recursively.
  537. .. rst-class:: classref-item-separator
  538. ----
  539. .. _class_Dictionary_method_set:
  540. .. rst-class:: classref-method
  541. :ref:`bool<class_bool>` **set**\ (\ key\: :ref:`Variant<class_Variant>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Dictionary_method_set>`
  542. Sets the value of the element at the given ``key`` to the given ``value``. This is the same as using the ``[]`` operator (``dict[key] = value``).
  543. .. rst-class:: classref-item-separator
  544. ----
  545. .. _class_Dictionary_method_size:
  546. .. rst-class:: classref-method
  547. :ref:`int<class_int>` **size**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_size>`
  548. Returns the number of entries in the dictionary. Empty dictionaries (``{ }``) always return ``0``. See also :ref:`is_empty()<class_Dictionary_method_is_empty>`.
  549. .. rst-class:: classref-item-separator
  550. ----
  551. .. _class_Dictionary_method_sort:
  552. .. rst-class:: classref-method
  553. |void| **sort**\ (\ ) :ref:`🔗<class_Dictionary_method_sort>`
  554. Sorts the dictionary in ascending order, by key. The final order is dependent on the "less than" (``<``) comparison between keys.
  555. .. tabs::
  556. .. code-tab:: gdscript
  557. var numbers = { "c": 2, "a": 0, "b": 1 }
  558. numbers.sort()
  559. print(numbers) # Prints { "a": 0, "b": 1, "c": 2 }
  560. This method ensures that the dictionary's entries are ordered consistently when :ref:`keys()<class_Dictionary_method_keys>` or :ref:`values()<class_Dictionary_method_values>` are called, or when the dictionary needs to be converted to a string through :ref:`@GlobalScope.str()<class_@GlobalScope_method_str>` or :ref:`JSON.stringify()<class_JSON_method_stringify>`.
  561. .. rst-class:: classref-item-separator
  562. ----
  563. .. _class_Dictionary_method_values:
  564. .. rst-class:: classref-method
  565. :ref:`Array<class_Array>` **values**\ (\ ) |const| :ref:`🔗<class_Dictionary_method_values>`
  566. Returns the list of values in this dictionary.
  567. .. rst-class:: classref-section-separator
  568. ----
  569. .. rst-class:: classref-descriptions-group
  570. Operator Descriptions
  571. ---------------------
  572. .. _class_Dictionary_operator_neq_Dictionary:
  573. .. rst-class:: classref-operator
  574. :ref:`bool<class_bool>` **operator !=**\ (\ right\: :ref:`Dictionary<class_Dictionary>`\ ) :ref:`🔗<class_Dictionary_operator_neq_Dictionary>`
  575. Returns ``true`` if the two dictionaries do not contain the same keys and values.
  576. .. rst-class:: classref-item-separator
  577. ----
  578. .. _class_Dictionary_operator_eq_Dictionary:
  579. .. rst-class:: classref-operator
  580. :ref:`bool<class_bool>` **operator ==**\ (\ right\: :ref:`Dictionary<class_Dictionary>`\ ) :ref:`🔗<class_Dictionary_operator_eq_Dictionary>`
  581. Returns ``true`` if the two dictionaries contain the same keys and values. The order of the entries does not matter.
  582. \ **Note:** In C#, by convention, this operator compares by **reference**. If you need to compare by value, iterate over both dictionaries.
  583. .. rst-class:: classref-item-separator
  584. ----
  585. .. _class_Dictionary_operator_idx_Variant:
  586. .. rst-class:: classref-operator
  587. :ref:`Variant<class_Variant>` **operator []**\ (\ key\: :ref:`Variant<class_Variant>`\ ) :ref:`🔗<class_Dictionary_operator_idx_Variant>`
  588. 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>`.
  589. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  590. .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
  591. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  592. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  593. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  594. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  595. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  596. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  597. .. |void| replace:: :abbr:`void (No return value.)`