class_dictionary.rst 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the Dictionary.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_Dictionary:
  6. Dictionary
  7. ==========
  8. Dictionary type.
  9. Description
  10. -----------
  11. Dictionary type. Associative container which contains values referenced by unique keys. Dictionary are composed of pairs of keys (which must be unique) and values. You can define a dictionary by placing a comma separated list of ``key: value`` pairs in curly braces ``{}``.
  12. Erasing elements while iterating over them **is not supported**.
  13. Creating a dictionary:
  14. ::
  15. var my_dir = {} # Creates an empty dictionary.
  16. var points_dir = {"White": 50, "Yellow": 75, "Orange": 100}
  17. var my_dir = {
  18. key1: value1,
  19. key2: value2,
  20. key3: value3,
  21. }
  22. You can access values of a dictionary by referencing appropriate key in above example ``points_dir["White"]`` would return value of 50.
  23. ::
  24. export(String, "White", "Yellow", "Orange") var my_color
  25. var points_dir = {"White": 50, "Yellow": 75, "Orange": 100}
  26. func _ready():
  27. var points = points_dir[my_color]
  28. In the above code ``points`` will be assigned the value that is paired with the appropriate color selected in ``my_color``.
  29. Dictionaries can contain more complex data:
  30. ::
  31. my_dir = {"First Array": [1, 2, 3, 4]} # Assigns an Array to a String key.
  32. To add a key to an existing dictionary, access it like an existing key and assign to it:
  33. ::
  34. var points_dir = {"White": 50, "Yellow": 75, "Orange": 100}
  35. var points_dir["Blue"] = 150 # Add "Blue" as a key and assign 150 as its value.
  36. Finally, dictionaries can contain different types of keys and values in the same dictionary:
  37. ::
  38. var my_dir = {"String Key": 5, 4: [1, 2, 3], 7: "Hello"} # This is a valid dictionary.
  39. **Note:** Unlike :ref:`Array<class_Array>`\ s you can't compare dictionaries directly:
  40. ::
  41. array1 = [1, 2, 3]
  42. array2 = [1, 2, 3]
  43. func compare_arrays():
  44. print(array1 == array2) # Will print true.
  45. dir1 = {"a": 1, "b": 2, "c": 3}
  46. dir2 = {"a": 1, "b": 2, "c": 3}
  47. func compare_dictionaries():
  48. print(dir1 == dir2) # Will NOT print true.
  49. You need to first calculate the dictionary's hash with :ref:`hash<class_Dictionary_method_hash>` before you can compare them:
  50. ::
  51. dir1 = {"a": 1, "b": 2, "c": 3}
  52. dir2 = {"a": 1, "b": 2, "c": 3}
  53. func compare_dictionaries():
  54. print(dir1.hash() == dir2.hash()) # Will print true.
  55. Tutorials
  56. ---------
  57. - `#dictionary <../getting_started/scripting/gdscript/gdscript_basics.html#dictionary>`_ in :doc:`../getting_started/scripting/gdscript/gdscript_basics`
  58. Methods
  59. -------
  60. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  61. | void | :ref:`clear<class_Dictionary_method_clear>` **(** **)** |
  62. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`Dictionary<class_Dictionary>` | :ref:`duplicate<class_Dictionary_method_duplicate>` **(** :ref:`bool<class_bool>` deep=False **)** |
  64. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`bool<class_bool>` | :ref:`empty<class_Dictionary_method_empty>` **(** **)** |
  66. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`bool<class_bool>` | :ref:`erase<class_Dictionary_method_erase>` **(** :ref:`Variant<class_Variant>` key **)** |
  68. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Variant<class_Variant>` | :ref:`get<class_Dictionary_method_get>` **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=Null **)** |
  70. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`bool<class_bool>` | :ref:`has<class_Dictionary_method_has>` **(** :ref:`Variant<class_Variant>` key **)** |
  72. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  73. | :ref:`bool<class_bool>` | :ref:`has_all<class_Dictionary_method_has_all>` **(** :ref:`Array<class_Array>` keys **)** |
  74. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  75. | :ref:`int<class_int>` | :ref:`hash<class_Dictionary_method_hash>` **(** **)** |
  76. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  77. | :ref:`Array<class_Array>` | :ref:`keys<class_Dictionary_method_keys>` **(** **)** |
  78. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`int<class_int>` | :ref:`size<class_Dictionary_method_size>` **(** **)** |
  80. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`Array<class_Array>` | :ref:`values<class_Dictionary_method_values>` **(** **)** |
  82. +-------------------------------------+-----------------------------------------------------------------------------------------------------------------------------------+
  83. Method Descriptions
  84. -------------------
  85. .. _class_Dictionary_method_clear:
  86. - void **clear** **(** **)**
  87. Clear the dictionary, removing all key/value pairs.
  88. ----
  89. .. _class_Dictionary_method_duplicate:
  90. - :ref:`Dictionary<class_Dictionary>` **duplicate** **(** :ref:`bool<class_bool>` deep=False **)**
  91. Creates a copy of the dictionary, and returns it.
  92. ----
  93. .. _class_Dictionary_method_empty:
  94. - :ref:`bool<class_bool>` **empty** **(** **)**
  95. Returns ``true`` if the dictionary is empty.
  96. ----
  97. .. _class_Dictionary_method_erase:
  98. - :ref:`bool<class_bool>` **erase** **(** :ref:`Variant<class_Variant>` key **)**
  99. Erase a dictionary key/value pair by key. Returns ``true`` if the given key was present in the dictionary, ``false`` otherwise. Does not erase elements while iterating over the dictionary.
  100. ----
  101. .. _class_Dictionary_method_get:
  102. - :ref:`Variant<class_Variant>` **get** **(** :ref:`Variant<class_Variant>` key, :ref:`Variant<class_Variant>` default=Null **)**
  103. 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.
  104. ----
  105. .. _class_Dictionary_method_has:
  106. - :ref:`bool<class_bool>` **has** **(** :ref:`Variant<class_Variant>` key **)**
  107. Returns ``true`` if the dictionary has a given key.
  108. ----
  109. .. _class_Dictionary_method_has_all:
  110. - :ref:`bool<class_bool>` **has_all** **(** :ref:`Array<class_Array>` keys **)**
  111. Returns ``true`` if the dictionary has all of the keys in the given array.
  112. ----
  113. .. _class_Dictionary_method_hash:
  114. - :ref:`int<class_int>` **hash** **(** **)**
  115. Returns a hashed integer value representing the dictionary contents. This can be used to compare dictionaries by value:
  116. ::
  117. var dict1 = {0: 10}
  118. var dict2 = {0: 10}
  119. # The line below prints `true`, whereas it would have printed `false` if both variables were compared directly.
  120. print(dict1.hash() == dict2.hash())
  121. ----
  122. .. _class_Dictionary_method_keys:
  123. - :ref:`Array<class_Array>` **keys** **(** **)**
  124. Returns the list of keys in the ``Dictionary``.
  125. ----
  126. .. _class_Dictionary_method_size:
  127. - :ref:`int<class_int>` **size** **(** **)**
  128. Returns the size of the dictionary (in pairs).
  129. ----
  130. .. _class_Dictionary_method_values:
  131. - :ref:`Array<class_Array>` **values** **(** **)**
  132. Returns the list of values in the ``Dictionary``.