2
0

class_array.rst 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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/Array.xml.
  6. .. _class_Array:
  7. Array
  8. =====
  9. A generic array datatype.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. A generic array that can contain several elements of any type, accessible by a numerical index starting at 0. Negative indices can be used to count from the back, like in Python (-1 is the last element, -2 is the second to last, etc.).
  14. \ **Example:**\
  15. ::
  16. var array = ["One", 2, 3, "Four"]
  17. print(array[0]) # One.
  18. print(array[2]) # 3.
  19. print(array[-1]) # Four.
  20. array[2] = "Three"
  21. print(array[-2]) # Three.
  22. Arrays can be concatenated using the ``+`` operator:
  23. ::
  24. var array1 = ["One", 2]
  25. var array2 = [3, "Four"]
  26. print(array1 + array2) # ["One", 2, 3, "Four"]
  27. \ **Note:** Concatenating with the ``+=`` operator will create a new array, which has a cost. If you want to append another array to an existing array, :ref:`append_array<class_Array_method_append_array>` is more efficient.
  28. \ **Note:** Arrays are always passed by reference. To get a copy of an array that can be modified independently of the original array, use :ref:`duplicate<class_Array_method_duplicate>`.
  29. \ **Note:** When declaring an array with ``const``, the array itself can still be mutated by defining the values at individual indices or pushing/removing elements. Using ``const`` will only prevent assigning the constant with another value after it was initialized.
  30. .. rst-class:: classref-reftable-group
  31. Methods
  32. -------
  33. .. table::
  34. :widths: auto
  35. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolColorArray<class_PoolColorArray>` from **)** |
  37. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolVector3Array<class_PoolVector3Array>` from **)** |
  39. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolVector2Array<class_PoolVector2Array>` from **)** |
  41. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolStringArray<class_PoolStringArray>` from **)** |
  43. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolRealArray<class_PoolRealArray>` from **)** |
  45. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolIntArray<class_PoolIntArray>` from **)** |
  47. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_method_Array>` **(** :ref:`PoolByteArray<class_PoolByteArray>` from **)** |
  49. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | void | :ref:`append<class_Array_method_append>` **(** :ref:`Variant<class_Variant>` value **)** |
  51. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | void | :ref:`append_array<class_Array_method_append_array>` **(** :ref:`Array<class_Array>` array **)** |
  53. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`Variant<class_Variant>` | :ref:`back<class_Array_method_back>` **(** **)** |
  55. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`int<class_int>` | :ref:`bsearch<class_Array_method_bsearch>` **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)** |
  57. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`int<class_int>` | :ref:`bsearch_custom<class_Array_method_bsearch_custom>` **(** :ref:`Variant<class_Variant>` value, :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func, :ref:`bool<class_bool>` before=true **)** |
  59. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | void | :ref:`clear<class_Array_method_clear>` **(** **)** |
  61. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`int<class_int>` | :ref:`count<class_Array_method_count>` **(** :ref:`Variant<class_Variant>` value **)** |
  63. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`Array<class_Array>` | :ref:`duplicate<class_Array_method_duplicate>` **(** :ref:`bool<class_bool>` deep=false **)** |
  65. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`bool<class_bool>` | :ref:`empty<class_Array_method_empty>` **(** **)** |
  67. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | void | :ref:`erase<class_Array_method_erase>` **(** :ref:`Variant<class_Variant>` value **)** |
  69. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | void | :ref:`fill<class_Array_method_fill>` **(** :ref:`Variant<class_Variant>` value **)** |
  71. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`int<class_int>` | :ref:`find<class_Array_method_find>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)** |
  73. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`int<class_int>` | :ref:`find_last<class_Array_method_find_last>` **(** :ref:`Variant<class_Variant>` value **)** |
  75. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`Variant<class_Variant>` | :ref:`front<class_Array_method_front>` **(** **)** |
  77. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | :ref:`bool<class_bool>` | :ref:`has<class_Array_method_has>` **(** :ref:`Variant<class_Variant>` value **)** |
  79. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | :ref:`int<class_int>` | :ref:`hash<class_Array_method_hash>` **(** **)** |
  81. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | void | :ref:`insert<class_Array_method_insert>` **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)** |
  83. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | void | :ref:`invert<class_Array_method_invert>` **(** **)** |
  85. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`Variant<class_Variant>` | :ref:`max<class_Array_method_max>` **(** **)** |
  87. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`Variant<class_Variant>` | :ref:`min<class_Array_method_min>` **(** **)** |
  89. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. | :ref:`Variant<class_Variant>` | :ref:`pick_random<class_Array_method_pick_random>` **(** **)** |
  91. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  92. | :ref:`Variant<class_Variant>` | :ref:`pop_at<class_Array_method_pop_at>` **(** :ref:`int<class_int>` position **)** |
  93. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. | :ref:`Variant<class_Variant>` | :ref:`pop_back<class_Array_method_pop_back>` **(** **)** |
  95. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | :ref:`Variant<class_Variant>` | :ref:`pop_front<class_Array_method_pop_front>` **(** **)** |
  97. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. | void | :ref:`push_back<class_Array_method_push_back>` **(** :ref:`Variant<class_Variant>` value **)** |
  99. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  100. | void | :ref:`push_front<class_Array_method_push_front>` **(** :ref:`Variant<class_Variant>` value **)** |
  101. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  102. | void | :ref:`remove<class_Array_method_remove>` **(** :ref:`int<class_int>` position **)** |
  103. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  104. | void | :ref:`resize<class_Array_method_resize>` **(** :ref:`int<class_int>` size **)** |
  105. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  106. | :ref:`int<class_int>` | :ref:`rfind<class_Array_method_rfind>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)** |
  107. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  108. | void | :ref:`shuffle<class_Array_method_shuffle>` **(** **)** |
  109. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  110. | :ref:`int<class_int>` | :ref:`size<class_Array_method_size>` **(** **)** |
  111. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  112. | :ref:`Array<class_Array>` | :ref:`slice<class_Array_method_slice>` **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)** |
  113. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  114. | void | :ref:`sort<class_Array_method_sort>` **(** **)** |
  115. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  116. | void | :ref:`sort_custom<class_Array_method_sort_custom>` **(** :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func **)** |
  117. +-------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  118. .. rst-class:: classref-section-separator
  119. ----
  120. .. rst-class:: classref-descriptions-group
  121. Method Descriptions
  122. -------------------
  123. .. _class_Array_method_Array:
  124. .. rst-class:: classref-method
  125. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolColorArray<class_PoolColorArray>` from **)**
  126. Constructs an array from a :ref:`PoolColorArray<class_PoolColorArray>`.
  127. .. rst-class:: classref-item-separator
  128. ----
  129. .. rst-class:: classref-method
  130. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolVector3Array<class_PoolVector3Array>` from **)**
  131. Constructs an array from a :ref:`PoolVector3Array<class_PoolVector3Array>`.
  132. .. rst-class:: classref-item-separator
  133. ----
  134. .. rst-class:: classref-method
  135. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolVector2Array<class_PoolVector2Array>` from **)**
  136. Constructs an array from a :ref:`PoolVector2Array<class_PoolVector2Array>`.
  137. .. rst-class:: classref-item-separator
  138. ----
  139. .. rst-class:: classref-method
  140. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolStringArray<class_PoolStringArray>` from **)**
  141. Constructs an array from a :ref:`PoolStringArray<class_PoolStringArray>`.
  142. .. rst-class:: classref-item-separator
  143. ----
  144. .. rst-class:: classref-method
  145. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolRealArray<class_PoolRealArray>` from **)**
  146. Constructs an array from a :ref:`PoolRealArray<class_PoolRealArray>`.
  147. .. rst-class:: classref-item-separator
  148. ----
  149. .. rst-class:: classref-method
  150. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolIntArray<class_PoolIntArray>` from **)**
  151. Constructs an array from a :ref:`PoolIntArray<class_PoolIntArray>`.
  152. .. rst-class:: classref-item-separator
  153. ----
  154. .. rst-class:: classref-method
  155. :ref:`Array<class_Array>` **Array** **(** :ref:`PoolByteArray<class_PoolByteArray>` from **)**
  156. Constructs an array from a :ref:`PoolByteArray<class_PoolByteArray>`.
  157. .. rst-class:: classref-item-separator
  158. ----
  159. .. _class_Array_method_append:
  160. .. rst-class:: classref-method
  161. void **append** **(** :ref:`Variant<class_Variant>` value **)**
  162. Appends an element at the end of the array (alias of :ref:`push_back<class_Array_method_push_back>`).
  163. .. rst-class:: classref-item-separator
  164. ----
  165. .. _class_Array_method_append_array:
  166. .. rst-class:: classref-method
  167. void **append_array** **(** :ref:`Array<class_Array>` array **)**
  168. Appends another array at the end of this array.
  169. ::
  170. var array1 = [1, 2, 3]
  171. var array2 = [4, 5, 6]
  172. array1.append_array(array2)
  173. print(array1) # Prints [1, 2, 3, 4, 5, 6].
  174. .. rst-class:: classref-item-separator
  175. ----
  176. .. _class_Array_method_back:
  177. .. rst-class:: classref-method
  178. :ref:`Variant<class_Variant>` **back** **(** **)**
  179. Returns the last element of the array. Prints an error and returns ``null`` if the array is empty.
  180. \ **Note:** Calling this function is not the same as writing ``array[-1]``. If the array is empty, accessing by index will pause project execution when running from the editor.
  181. .. rst-class:: classref-item-separator
  182. ----
  183. .. _class_Array_method_bsearch:
  184. .. rst-class:: classref-method
  185. :ref:`int<class_int>` **bsearch** **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)**
  186. Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search. Optionally, a ``before`` specifier can be passed. If ``false``, the returned index comes after all existing entries of the value in the array.
  187. \ **Note:** Calling :ref:`bsearch<class_Array_method_bsearch>` on an unsorted array results in unexpected behavior.
  188. .. rst-class:: classref-item-separator
  189. ----
  190. .. _class_Array_method_bsearch_custom:
  191. .. rst-class:: classref-method
  192. :ref:`int<class_int>` **bsearch_custom** **(** :ref:`Variant<class_Variant>` value, :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func, :ref:`bool<class_bool>` before=true **)**
  193. Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search and a custom comparison method declared in the ``obj``. Optionally, a ``before`` specifier can be passed. If ``false``, the returned index comes after all existing entries of the value in the array. The custom method receives two arguments (an element from the array and the value searched for) and must return ``true`` if the first argument is less than the second, and return ``false`` otherwise.
  194. ::
  195. func cardinal_to_algebraic(a):
  196. match a:
  197. "one":
  198. return 1
  199. "two":
  200. return 2
  201. "three":
  202. return 3
  203. "four":
  204. return 4
  205. _:
  206. return 0
  207. func compare(a, b):
  208. return cardinal_to_algebraic(a) < cardinal_to_algebraic(b)
  209. func _ready():
  210. var a = ["one", "two", "three", "four"]
  211. # `compare` is defined in this object, so we use `self` as the `obj` parameter.
  212. print(a.bsearch_custom("three", self, "compare", true)) # Expected value is 2.
  213. \ **Note:** Calling :ref:`bsearch_custom<class_Array_method_bsearch_custom>` on an unsorted array results in unexpected behavior.
  214. .. rst-class:: classref-item-separator
  215. ----
  216. .. _class_Array_method_clear:
  217. .. rst-class:: classref-method
  218. void **clear** **(** **)**
  219. Clears the array. This is equivalent to using :ref:`resize<class_Array_method_resize>` with a size of ``0``.
  220. .. rst-class:: classref-item-separator
  221. ----
  222. .. _class_Array_method_count:
  223. .. rst-class:: classref-method
  224. :ref:`int<class_int>` **count** **(** :ref:`Variant<class_Variant>` value **)**
  225. Returns the number of times an element is in the array.
  226. .. rst-class:: classref-item-separator
  227. ----
  228. .. _class_Array_method_duplicate:
  229. .. rst-class:: classref-method
  230. :ref:`Array<class_Array>` **duplicate** **(** :ref:`bool<class_bool>` deep=false **)**
  231. Returns a copy of the array.
  232. If ``deep`` is ``true``, a deep copy is performed: all nested arrays and dictionaries are duplicated and will not be shared with the original array. If ``false``, a shallow copy is made and references to the original nested arrays and dictionaries are kept, so that modifying a sub-array or dictionary in the copy will also impact those referenced in the source array.
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. _class_Array_method_empty:
  236. .. rst-class:: classref-method
  237. :ref:`bool<class_bool>` **empty** **(** **)**
  238. Returns ``true`` if the array is empty.
  239. .. rst-class:: classref-item-separator
  240. ----
  241. .. _class_Array_method_erase:
  242. .. rst-class:: classref-method
  243. void **erase** **(** :ref:`Variant<class_Variant>` value **)**
  244. Removes the first occurrence of a value from the array. If the value does not exist in the array, nothing happens. To remove an element by index, use :ref:`remove<class_Array_method_remove>` instead.
  245. \ **Note:** This method acts in-place and doesn't return a value.
  246. \ **Note:** On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
  247. .. rst-class:: classref-item-separator
  248. ----
  249. .. _class_Array_method_fill:
  250. .. rst-class:: classref-method
  251. void **fill** **(** :ref:`Variant<class_Variant>` value **)**
  252. Assigns the given value to all elements in the array. This can typically be used together with :ref:`resize<class_Array_method_resize>` to create an array with a given size and initialized elements:
  253. ::
  254. var array = []
  255. array.resize(10)
  256. array.fill(0) # Initialize the 10 elements to 0.
  257. \ **Note:** If ``value`` is of a reference type (:ref:`Object<class_Object>`-derived, **Array**, :ref:`Dictionary<class_Dictionary>`, etc.) then the array is filled with the references to the same object, i.e. no duplicates are created.
  258. .. rst-class:: classref-item-separator
  259. ----
  260. .. _class_Array_method_find:
  261. .. rst-class:: classref-method
  262. :ref:`int<class_int>` **find** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)**
  263. Searches the array for a value and returns its index or ``-1`` if not found. Optionally, the initial search index can be passed. Returns ``-1`` if ``from`` is out of bounds.
  264. .. rst-class:: classref-item-separator
  265. ----
  266. .. _class_Array_method_find_last:
  267. .. rst-class:: classref-method
  268. :ref:`int<class_int>` **find_last** **(** :ref:`Variant<class_Variant>` value **)**
  269. Searches the array in reverse order for a value and returns its index or ``-1`` if not found.
  270. .. rst-class:: classref-item-separator
  271. ----
  272. .. _class_Array_method_front:
  273. .. rst-class:: classref-method
  274. :ref:`Variant<class_Variant>` **front** **(** **)**
  275. Returns the first element of the array. Prints an error and returns ``null`` if the array is empty.
  276. \ **Note:** Calling this function is not the same as writing ``array[0]``. If the array is empty, accessing by index will pause project execution when running from the editor.
  277. .. rst-class:: classref-item-separator
  278. ----
  279. .. _class_Array_method_has:
  280. .. rst-class:: classref-method
  281. :ref:`bool<class_bool>` **has** **(** :ref:`Variant<class_Variant>` value **)**
  282. Returns ``true`` if the array contains the given value.
  283. ::
  284. ["inside", 7].has("inside") # True
  285. ["inside", 7].has("outside") # False
  286. ["inside", 7].has(7) # True
  287. ["inside", 7].has("7") # False
  288. \ **Note:** This is equivalent to using the ``in`` operator as follows:
  289. ::
  290. # Will evaluate to `true`.
  291. if 2 in [2, 4, 6, 8]:
  292. pass
  293. .. rst-class:: classref-item-separator
  294. ----
  295. .. _class_Array_method_hash:
  296. .. rst-class:: classref-method
  297. :ref:`int<class_int>` **hash** **(** **)**
  298. Returns a hashed 32-bit integer value representing the array and its contents.
  299. \ **Note:** **Array**\ s with equal content will always produce identical hash values. However, the reverse is not true. Returning identical hash values does *not* imply the arrays are equal, because different arrays can have identical hash values due to hash collisions.
  300. .. rst-class:: classref-item-separator
  301. ----
  302. .. _class_Array_method_insert:
  303. .. rst-class:: classref-method
  304. void **insert** **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)**
  305. Inserts a new element at a given position in the array. The position must be valid, or at the end of the array (``pos == size()``).
  306. \ **Note:** This method acts in-place and doesn't return a value.
  307. \ **Note:** On large arrays, this method will be slower if the inserted element is close to the beginning of the array (index 0). This is because all elements placed after the newly inserted element have to be reindexed.
  308. .. rst-class:: classref-item-separator
  309. ----
  310. .. _class_Array_method_invert:
  311. .. rst-class:: classref-method
  312. void **invert** **(** **)**
  313. Reverses the order of the elements in the array.
  314. .. rst-class:: classref-item-separator
  315. ----
  316. .. _class_Array_method_max:
  317. .. rst-class:: classref-method
  318. :ref:`Variant<class_Variant>` **max** **(** **)**
  319. Returns the maximum value contained in the array if all elements are of comparable types. If the elements can't be compared, ``null`` is returned.
  320. .. rst-class:: classref-item-separator
  321. ----
  322. .. _class_Array_method_min:
  323. .. rst-class:: classref-method
  324. :ref:`Variant<class_Variant>` **min** **(** **)**
  325. Returns the minimum value contained in the array if all elements are of comparable types. If the elements can't be compared, ``null`` is returned.
  326. .. rst-class:: classref-item-separator
  327. ----
  328. .. _class_Array_method_pick_random:
  329. .. rst-class:: classref-method
  330. :ref:`Variant<class_Variant>` **pick_random** **(** **)**
  331. Returns a random value from the target array.
  332. ::
  333. var array: Array[int] = [1, 2, 3, 4]
  334. print(array.pick_random()) # Prints either of the four numbers.
  335. .. rst-class:: classref-item-separator
  336. ----
  337. .. _class_Array_method_pop_at:
  338. .. rst-class:: classref-method
  339. :ref:`Variant<class_Variant>` **pop_at** **(** :ref:`int<class_int>` position **)**
  340. Removes and returns the element of the array at index ``position``. If negative, ``position`` is considered relative to the end of the array. Leaves the array untouched and returns ``null`` if the array is empty or if it's accessed out of bounds. An error message is printed when the array is accessed out of bounds, but not when the array is empty.
  341. \ **Note:** On large arrays, this method can be slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex the array's elements that are located after the removed element. The larger the array and the lower the index of the removed element, the slower :ref:`pop_at<class_Array_method_pop_at>` will be.
  342. .. rst-class:: classref-item-separator
  343. ----
  344. .. _class_Array_method_pop_back:
  345. .. rst-class:: classref-method
  346. :ref:`Variant<class_Variant>` **pop_back** **(** **)**
  347. Removes and returns the last element of the array. Returns ``null`` if the array is empty, without printing an error message. See also :ref:`pop_front<class_Array_method_pop_front>`.
  348. .. rst-class:: classref-item-separator
  349. ----
  350. .. _class_Array_method_pop_front:
  351. .. rst-class:: classref-method
  352. :ref:`Variant<class_Variant>` **pop_front** **(** **)**
  353. Removes and returns the first element of the array. Returns ``null`` if the array is empty, without printing an error message. See also :ref:`pop_back<class_Array_method_pop_back>`.
  354. \ **Note:** On large arrays, this method is much slower than :ref:`pop_back<class_Array_method_pop_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`pop_front<class_Array_method_pop_front>` will be.
  355. .. rst-class:: classref-item-separator
  356. ----
  357. .. _class_Array_method_push_back:
  358. .. rst-class:: classref-method
  359. void **push_back** **(** :ref:`Variant<class_Variant>` value **)**
  360. Appends an element at the end of the array. See also :ref:`push_front<class_Array_method_push_front>`.
  361. .. rst-class:: classref-item-separator
  362. ----
  363. .. _class_Array_method_push_front:
  364. .. rst-class:: classref-method
  365. void **push_front** **(** :ref:`Variant<class_Variant>` value **)**
  366. Adds an element at the beginning of the array. See also :ref:`push_back<class_Array_method_push_back>`.
  367. \ **Note:** On large arrays, this method is much slower than :ref:`push_back<class_Array_method_push_back>` as it will reindex all the array's elements every time it's called. The larger the array, the slower :ref:`push_front<class_Array_method_push_front>` will be.
  368. .. rst-class:: classref-item-separator
  369. ----
  370. .. _class_Array_method_remove:
  371. .. rst-class:: classref-method
  372. void **remove** **(** :ref:`int<class_int>` position **)**
  373. Removes an element from the array by index. If the index does not exist in the array, nothing happens. To remove an element by searching for its value, use :ref:`erase<class_Array_method_erase>` instead.
  374. \ **Note:** This method acts in-place and doesn't return a value.
  375. \ **Note:** On large arrays, this method will be slower if the removed element is close to the beginning of the array (index 0). This is because all elements placed after the removed element have to be reindexed.
  376. .. rst-class:: classref-item-separator
  377. ----
  378. .. _class_Array_method_resize:
  379. .. rst-class:: classref-method
  380. void **resize** **(** :ref:`int<class_int>` size **)**
  381. Resizes the array to contain a different number of elements. If the array size is smaller, elements are cleared, if bigger, new elements are ``null``.
  382. .. rst-class:: classref-item-separator
  383. ----
  384. .. _class_Array_method_rfind:
  385. .. rst-class:: classref-method
  386. :ref:`int<class_int>` **rfind** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)**
  387. Searches the array in reverse order. Optionally, a start search index can be passed. If negative, the start index is considered relative to the end of the array. If the adjusted start index is out of bounds, this method searches from the end of the array.
  388. .. rst-class:: classref-item-separator
  389. ----
  390. .. _class_Array_method_shuffle:
  391. .. rst-class:: classref-method
  392. void **shuffle** **(** **)**
  393. Shuffles the array such that the items will have a random order. This method uses the global random number generator common to methods such as :ref:`@GDScript.randi<class_@GDScript_method_randi>`. Call :ref:`@GDScript.randomize<class_@GDScript_method_randomize>` to ensure that a new seed will be used each time if you want non-reproducible shuffling.
  394. .. rst-class:: classref-item-separator
  395. ----
  396. .. _class_Array_method_size:
  397. .. rst-class:: classref-method
  398. :ref:`int<class_int>` **size** **(** **)**
  399. Returns the number of elements in the array.
  400. .. rst-class:: classref-item-separator
  401. ----
  402. .. _class_Array_method_slice:
  403. .. rst-class:: classref-method
  404. :ref:`Array<class_Array>` **slice** **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)**
  405. Duplicates the subset described in the function and returns it in an array, deeply copying the array if ``deep`` is ``true``. Lower and upper index are inclusive, with the ``step`` describing the change between indices while slicing.
  406. .. rst-class:: classref-item-separator
  407. ----
  408. .. _class_Array_method_sort:
  409. .. rst-class:: classref-method
  410. void **sort** **(** **)**
  411. Sorts the array.
  412. \ **Note:** The sorting algorithm used is not `stable <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`__. This means that values considered equal may have their order changed when using :ref:`sort<class_Array_method_sort>`.
  413. \ **Note:** Strings are sorted in alphabetical order (as opposed to natural order). This may lead to unexpected behavior when sorting an array of strings ending with a sequence of numbers. Consider the following example:
  414. ::
  415. var strings = ["string1", "string2", "string10", "string11"]
  416. strings.sort()
  417. print(strings) # Prints [string1, string10, string11, string2]
  418. .. rst-class:: classref-item-separator
  419. ----
  420. .. _class_Array_method_sort_custom:
  421. .. rst-class:: classref-method
  422. void **sort_custom** **(** :ref:`Object<class_Object>` obj, :ref:`String<class_String>` func **)**
  423. Sorts the array using a custom method. The arguments are an object that holds the method and the name of such method. The custom method receives two arguments (a pair of elements from the array) and must return either ``true`` or ``false``.
  424. For two elements ``a`` and ``b``, if the given method returns ``true``, element ``b`` will be after element ``a`` in the array.
  425. \ **Note:** The sorting algorithm used is not `stable <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`__. This means that values considered equal may have their order changed when using :ref:`sort_custom<class_Array_method_sort_custom>`.
  426. \ **Note:** You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Randomizing the return value will result in unexpected behavior.
  427. ::
  428. class MyCustomSorter:
  429. static func sort_ascending(a, b):
  430. if a[0] < b[0]:
  431. return true
  432. return false
  433. var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
  434. my_items.sort_custom(MyCustomSorter, "sort_ascending")
  435. print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]].
  436. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  437. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  438. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  439. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`