class_array.rst 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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/4.1/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/4.1/doc/classes/Array.xml.
  6. .. _class_Array:
  7. Array
  8. =====
  9. A built-in data structure that holds a sequence of elements.
  10. .. rst-class:: classref-introduction-group
  11. Description
  12. -----------
  13. An array data structure that can contain a sequence of elements of any type. Elements are accessed by a numerical index starting at 0. Negative indices are used to count from the back (-1 is the last element, -2 is the second to last, etc.).
  14. \ **Example:**\
  15. .. tabs::
  16. .. code-tab:: gdscript
  17. var array = ["One", 2, 3, "Four"]
  18. print(array[0]) # One.
  19. print(array[2]) # 3.
  20. print(array[-1]) # Four.
  21. array[2] = "Three"
  22. print(array[-2]) # Three.
  23. .. code-tab:: csharp
  24. var array = new Godot.Collections.Array{"One", 2, 3, "Four"};
  25. GD.Print(array[0]); // One.
  26. GD.Print(array[2]); // 3.
  27. GD.Print(array[array.Count - 1]); // Four.
  28. array[2] = "Three";
  29. GD.Print(array[array.Count - 2]); // Three.
  30. Arrays can be concatenated using the ``+`` operator:
  31. .. tabs::
  32. .. code-tab:: gdscript
  33. var array1 = ["One", 2]
  34. var array2 = [3, "Four"]
  35. print(array1 + array2) # ["One", 2, 3, "Four"]
  36. .. code-tab:: csharp
  37. // Array concatenation is not possible with C# arrays, but is with Godot.Collections.Array.
  38. var array1 = new Godot.Collections.Array{"One", 2};
  39. var array2 = new Godot.Collections.Array{3, "Four"};
  40. GD.Print(array1 + array2); // Prints [One, 2, 3, Four]
  41. \ **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>`.
  42. \ **Note:** Erasing elements while iterating over arrays is **not** supported and will result in unpredictable behavior.
  43. .. rst-class:: classref-reftable-group
  44. Constructors
  45. ------------
  46. .. table::
  47. :widths: auto
  48. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  49. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** **)** |
  50. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  51. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`Array<class_Array>` base, :ref:`int<class_int>` type, :ref:`StringName<class_StringName>` class_name, :ref:`Variant<class_Variant>` script **)** |
  52. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  53. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`Array<class_Array>` from **)** |
  54. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  55. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedByteArray<class_PackedByteArray>` from **)** |
  56. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  57. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedColorArray<class_PackedColorArray>` from **)** |
  58. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  59. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedFloat32Array<class_PackedFloat32Array>` from **)** |
  60. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  61. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedFloat64Array<class_PackedFloat64Array>` from **)** |
  62. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  63. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedInt32Array<class_PackedInt32Array>` from **)** |
  64. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  65. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedInt64Array<class_PackedInt64Array>` from **)** |
  66. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  67. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedStringArray<class_PackedStringArray>` from **)** |
  68. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  69. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedVector2Array<class_PackedVector2Array>` from **)** |
  70. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  71. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>` **(** :ref:`PackedVector3Array<class_PackedVector3Array>` from **)** |
  72. +---------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  73. .. rst-class:: classref-reftable-group
  74. Methods
  75. -------
  76. .. table::
  77. :widths: auto
  78. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  79. | :ref:`bool<class_bool>` | :ref:`all<class_Array_method_all>` **(** :ref:`Callable<class_Callable>` method **)** |const| |
  80. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  81. | :ref:`bool<class_bool>` | :ref:`any<class_Array_method_any>` **(** :ref:`Callable<class_Callable>` method **)** |const| |
  82. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  83. | void | :ref:`append<class_Array_method_append>` **(** :ref:`Variant<class_Variant>` value **)** |
  84. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  85. | void | :ref:`append_array<class_Array_method_append_array>` **(** :ref:`Array<class_Array>` array **)** |
  86. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  87. | void | :ref:`assign<class_Array_method_assign>` **(** :ref:`Array<class_Array>` array **)** |
  88. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  89. | :ref:`Variant<class_Variant>` | :ref:`back<class_Array_method_back>` **(** **)** |const| |
  90. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  91. | :ref:`int<class_int>` | :ref:`bsearch<class_Array_method_bsearch>` **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)** |const| |
  92. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  93. | :ref:`int<class_int>` | :ref:`bsearch_custom<class_Array_method_bsearch_custom>` **(** :ref:`Variant<class_Variant>` value, :ref:`Callable<class_Callable>` func, :ref:`bool<class_bool>` before=true **)** |const| |
  94. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  95. | void | :ref:`clear<class_Array_method_clear>` **(** **)** |
  96. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  97. | :ref:`int<class_int>` | :ref:`count<class_Array_method_count>` **(** :ref:`Variant<class_Variant>` value **)** |const| |
  98. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  99. | :ref:`Array<class_Array>` | :ref:`duplicate<class_Array_method_duplicate>` **(** :ref:`bool<class_bool>` deep=false **)** |const| |
  100. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  101. | void | :ref:`erase<class_Array_method_erase>` **(** :ref:`Variant<class_Variant>` value **)** |
  102. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  103. | void | :ref:`fill<class_Array_method_fill>` **(** :ref:`Variant<class_Variant>` value **)** |
  104. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  105. | :ref:`Array<class_Array>` | :ref:`filter<class_Array_method_filter>` **(** :ref:`Callable<class_Callable>` method **)** |const| |
  106. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  107. | :ref:`int<class_int>` | :ref:`find<class_Array_method_find>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)** |const| |
  108. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  109. | :ref:`Variant<class_Variant>` | :ref:`front<class_Array_method_front>` **(** **)** |const| |
  110. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  111. | :ref:`int<class_int>` | :ref:`get_typed_builtin<class_Array_method_get_typed_builtin>` **(** **)** |const| |
  112. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  113. | :ref:`StringName<class_StringName>` | :ref:`get_typed_class_name<class_Array_method_get_typed_class_name>` **(** **)** |const| |
  114. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  115. | :ref:`Variant<class_Variant>` | :ref:`get_typed_script<class_Array_method_get_typed_script>` **(** **)** |const| |
  116. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  117. | :ref:`bool<class_bool>` | :ref:`has<class_Array_method_has>` **(** :ref:`Variant<class_Variant>` value **)** |const| |
  118. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  119. | :ref:`int<class_int>` | :ref:`hash<class_Array_method_hash>` **(** **)** |const| |
  120. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  121. | :ref:`int<class_int>` | :ref:`insert<class_Array_method_insert>` **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)** |
  122. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  123. | :ref:`bool<class_bool>` | :ref:`is_empty<class_Array_method_is_empty>` **(** **)** |const| |
  124. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  125. | :ref:`bool<class_bool>` | :ref:`is_read_only<class_Array_method_is_read_only>` **(** **)** |const| |
  126. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  127. | :ref:`bool<class_bool>` | :ref:`is_same_typed<class_Array_method_is_same_typed>` **(** :ref:`Array<class_Array>` array **)** |const| |
  128. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  129. | :ref:`bool<class_bool>` | :ref:`is_typed<class_Array_method_is_typed>` **(** **)** |const| |
  130. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  131. | void | :ref:`make_read_only<class_Array_method_make_read_only>` **(** **)** |
  132. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  133. | :ref:`Array<class_Array>` | :ref:`map<class_Array_method_map>` **(** :ref:`Callable<class_Callable>` method **)** |const| |
  134. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  135. | :ref:`Variant<class_Variant>` | :ref:`max<class_Array_method_max>` **(** **)** |const| |
  136. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  137. | :ref:`Variant<class_Variant>` | :ref:`min<class_Array_method_min>` **(** **)** |const| |
  138. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  139. | :ref:`Variant<class_Variant>` | :ref:`pick_random<class_Array_method_pick_random>` **(** **)** |const| |
  140. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  141. | :ref:`Variant<class_Variant>` | :ref:`pop_at<class_Array_method_pop_at>` **(** :ref:`int<class_int>` position **)** |
  142. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  143. | :ref:`Variant<class_Variant>` | :ref:`pop_back<class_Array_method_pop_back>` **(** **)** |
  144. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  145. | :ref:`Variant<class_Variant>` | :ref:`pop_front<class_Array_method_pop_front>` **(** **)** |
  146. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  147. | void | :ref:`push_back<class_Array_method_push_back>` **(** :ref:`Variant<class_Variant>` value **)** |
  148. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  149. | void | :ref:`push_front<class_Array_method_push_front>` **(** :ref:`Variant<class_Variant>` value **)** |
  150. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  151. | :ref:`Variant<class_Variant>` | :ref:`reduce<class_Array_method_reduce>` **(** :ref:`Callable<class_Callable>` method, :ref:`Variant<class_Variant>` accum=null **)** |const| |
  152. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  153. | void | :ref:`remove_at<class_Array_method_remove_at>` **(** :ref:`int<class_int>` position **)** |
  154. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  155. | :ref:`int<class_int>` | :ref:`resize<class_Array_method_resize>` **(** :ref:`int<class_int>` size **)** |
  156. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  157. | void | :ref:`reverse<class_Array_method_reverse>` **(** **)** |
  158. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  159. | :ref:`int<class_int>` | :ref:`rfind<class_Array_method_rfind>` **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)** |const| |
  160. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  161. | void | :ref:`shuffle<class_Array_method_shuffle>` **(** **)** |
  162. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  163. | :ref:`int<class_int>` | :ref:`size<class_Array_method_size>` **(** **)** |const| |
  164. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  165. | :ref:`Array<class_Array>` | :ref:`slice<class_Array_method_slice>` **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end=2147483647, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)** |const| |
  166. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  167. | void | :ref:`sort<class_Array_method_sort>` **(** **)** |
  168. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  169. | void | :ref:`sort_custom<class_Array_method_sort_custom>` **(** :ref:`Callable<class_Callable>` func **)** |
  170. +-------------------------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  171. .. rst-class:: classref-reftable-group
  172. Operators
  173. ---------
  174. .. table::
  175. :widths: auto
  176. +-------------------------------+-------------------------------------------------------------------------------------------------+
  177. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Array_operator_neq_Array>` **(** :ref:`Array<class_Array>` right **)** |
  178. +-------------------------------+-------------------------------------------------------------------------------------------------+
  179. | :ref:`Array<class_Array>` | :ref:`operator +<class_Array_operator_sum_Array>` **(** :ref:`Array<class_Array>` right **)** |
  180. +-------------------------------+-------------------------------------------------------------------------------------------------+
  181. | :ref:`bool<class_bool>` | :ref:`operator \<<class_Array_operator_lt_Array>` **(** :ref:`Array<class_Array>` right **)** |
  182. +-------------------------------+-------------------------------------------------------------------------------------------------+
  183. | :ref:`bool<class_bool>` | :ref:`operator \<=<class_Array_operator_lte_Array>` **(** :ref:`Array<class_Array>` right **)** |
  184. +-------------------------------+-------------------------------------------------------------------------------------------------+
  185. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Array_operator_eq_Array>` **(** :ref:`Array<class_Array>` right **)** |
  186. +-------------------------------+-------------------------------------------------------------------------------------------------+
  187. | :ref:`bool<class_bool>` | :ref:`operator ><class_Array_operator_gt_Array>` **(** :ref:`Array<class_Array>` right **)** |
  188. +-------------------------------+-------------------------------------------------------------------------------------------------+
  189. | :ref:`bool<class_bool>` | :ref:`operator >=<class_Array_operator_gte_Array>` **(** :ref:`Array<class_Array>` right **)** |
  190. +-------------------------------+-------------------------------------------------------------------------------------------------+
  191. | :ref:`Variant<class_Variant>` | :ref:`operator []<class_Array_operator_idx_int>` **(** :ref:`int<class_int>` index **)** |
  192. +-------------------------------+-------------------------------------------------------------------------------------------------+
  193. .. rst-class:: classref-section-separator
  194. ----
  195. .. rst-class:: classref-descriptions-group
  196. Constructor Descriptions
  197. ------------------------
  198. .. _class_Array_constructor_Array:
  199. .. rst-class:: classref-constructor
  200. :ref:`Array<class_Array>` **Array** **(** **)**
  201. Constructs an empty **Array**.
  202. .. rst-class:: classref-item-separator
  203. ----
  204. .. rst-class:: classref-constructor
  205. :ref:`Array<class_Array>` **Array** **(** :ref:`Array<class_Array>` base, :ref:`int<class_int>` type, :ref:`StringName<class_StringName>` class_name, :ref:`Variant<class_Variant>` script **)**
  206. Creates a typed array from the ``base`` array.
  207. .. rst-class:: classref-item-separator
  208. ----
  209. .. rst-class:: classref-constructor
  210. :ref:`Array<class_Array>` **Array** **(** :ref:`Array<class_Array>` from **)**
  211. Returns the same array as ``from``. If you need a copy of the array, use :ref:`duplicate<class_Array_method_duplicate>`.
  212. .. rst-class:: classref-item-separator
  213. ----
  214. .. rst-class:: classref-constructor
  215. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedByteArray<class_PackedByteArray>` from **)**
  216. Constructs an array from a :ref:`PackedByteArray<class_PackedByteArray>`.
  217. .. rst-class:: classref-item-separator
  218. ----
  219. .. rst-class:: classref-constructor
  220. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedColorArray<class_PackedColorArray>` from **)**
  221. Constructs an array from a :ref:`PackedColorArray<class_PackedColorArray>`.
  222. .. rst-class:: classref-item-separator
  223. ----
  224. .. rst-class:: classref-constructor
  225. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedFloat32Array<class_PackedFloat32Array>` from **)**
  226. Constructs an array from a :ref:`PackedFloat32Array<class_PackedFloat32Array>`.
  227. .. rst-class:: classref-item-separator
  228. ----
  229. .. rst-class:: classref-constructor
  230. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedFloat64Array<class_PackedFloat64Array>` from **)**
  231. Constructs an array from a :ref:`PackedFloat64Array<class_PackedFloat64Array>`.
  232. .. rst-class:: classref-item-separator
  233. ----
  234. .. rst-class:: classref-constructor
  235. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedInt32Array<class_PackedInt32Array>` from **)**
  236. Constructs an array from a :ref:`PackedInt32Array<class_PackedInt32Array>`.
  237. .. rst-class:: classref-item-separator
  238. ----
  239. .. rst-class:: classref-constructor
  240. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedInt64Array<class_PackedInt64Array>` from **)**
  241. Constructs an array from a :ref:`PackedInt64Array<class_PackedInt64Array>`.
  242. .. rst-class:: classref-item-separator
  243. ----
  244. .. rst-class:: classref-constructor
  245. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedStringArray<class_PackedStringArray>` from **)**
  246. Constructs an array from a :ref:`PackedStringArray<class_PackedStringArray>`.
  247. .. rst-class:: classref-item-separator
  248. ----
  249. .. rst-class:: classref-constructor
  250. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedVector2Array<class_PackedVector2Array>` from **)**
  251. Constructs an array from a :ref:`PackedVector2Array<class_PackedVector2Array>`.
  252. .. rst-class:: classref-item-separator
  253. ----
  254. .. rst-class:: classref-constructor
  255. :ref:`Array<class_Array>` **Array** **(** :ref:`PackedVector3Array<class_PackedVector3Array>` from **)**
  256. Constructs an array from a :ref:`PackedVector3Array<class_PackedVector3Array>`.
  257. .. rst-class:: classref-section-separator
  258. ----
  259. .. rst-class:: classref-descriptions-group
  260. Method Descriptions
  261. -------------------
  262. .. _class_Array_method_all:
  263. .. rst-class:: classref-method
  264. :ref:`bool<class_bool>` **all** **(** :ref:`Callable<class_Callable>` method **)** |const|
  265. Calls the provided :ref:`Callable<class_Callable>` on each element in the array and returns ``true`` if the :ref:`Callable<class_Callable>` returns ``true`` for *all* elements in the array. If the :ref:`Callable<class_Callable>` returns ``false`` for one array element or more, this method returns ``false``.
  266. The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a boolean value.
  267. ::
  268. func _ready():
  269. print([6, 10, 6].all(greater_than_5)) # Prints True (3/3 elements evaluate to `true`).
  270. print([4, 10, 4].all(greater_than_5)) # Prints False (1/3 elements evaluate to `true`).
  271. print([4, 4, 4].all(greater_than_5)) # Prints False (0/3 elements evaluate to `true`).
  272. print([].all(greater_than_5)) # Prints True (0/0 elements evaluate to `true`).
  273. print([6, 10, 6].all(func(number): return number > 5)) # Prints True. Same as the first line above, but using lambda function.
  274. func greater_than_5(number):
  275. return number > 5
  276. See also :ref:`any<class_Array_method_any>`, :ref:`filter<class_Array_method_filter>`, :ref:`map<class_Array_method_map>` and :ref:`reduce<class_Array_method_reduce>`.
  277. \ **Note:** Unlike relying on the size of an array returned by :ref:`filter<class_Array_method_filter>`, this method will return as early as possible to improve performance (especially with large arrays).
  278. \ **Note:** For an empty array, this method `always <https://en.wikipedia.org/wiki/Vacuous_truth>`__ returns ``true``.
  279. .. rst-class:: classref-item-separator
  280. ----
  281. .. _class_Array_method_any:
  282. .. rst-class:: classref-method
  283. :ref:`bool<class_bool>` **any** **(** :ref:`Callable<class_Callable>` method **)** |const|
  284. Calls the provided :ref:`Callable<class_Callable>` on each element in the array and returns ``true`` if the :ref:`Callable<class_Callable>` returns ``true`` for *one or more* elements in the array. If the :ref:`Callable<class_Callable>` returns ``false`` for all elements in the array, this method returns ``false``.
  285. The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a boolean value.
  286. ::
  287. func _ready():
  288. print([6, 10, 6].any(greater_than_5)) # Prints True (3 elements evaluate to `true`).
  289. print([4, 10, 4].any(greater_than_5)) # Prints True (1 elements evaluate to `true`).
  290. print([4, 4, 4].any(greater_than_5)) # Prints False (0 elements evaluate to `true`).
  291. print([].any(greater_than_5)) # Prints False (0 elements evaluate to `true`).
  292. print([6, 10, 6].any(func(number): return number > 5)) # Prints True. Same as the first line above, but using lambda function.
  293. func greater_than_5(number):
  294. return number > 5
  295. See also :ref:`all<class_Array_method_all>`, :ref:`filter<class_Array_method_filter>`, :ref:`map<class_Array_method_map>` and :ref:`reduce<class_Array_method_reduce>`.
  296. \ **Note:** Unlike relying on the size of an array returned by :ref:`filter<class_Array_method_filter>`, this method will return as early as possible to improve performance (especially with large arrays).
  297. \ **Note:** For an empty array, this method always returns ``false``.
  298. .. rst-class:: classref-item-separator
  299. ----
  300. .. _class_Array_method_append:
  301. .. rst-class:: classref-method
  302. void **append** **(** :ref:`Variant<class_Variant>` value **)**
  303. Appends an element at the end of the array (alias of :ref:`push_back<class_Array_method_push_back>`).
  304. .. rst-class:: classref-item-separator
  305. ----
  306. .. _class_Array_method_append_array:
  307. .. rst-class:: classref-method
  308. void **append_array** **(** :ref:`Array<class_Array>` array **)**
  309. Appends another array at the end of this array.
  310. ::
  311. var array1 = [1, 2, 3]
  312. var array2 = [4, 5, 6]
  313. array1.append_array(array2)
  314. print(array1) # Prints [1, 2, 3, 4, 5, 6].
  315. .. rst-class:: classref-item-separator
  316. ----
  317. .. _class_Array_method_assign:
  318. .. rst-class:: classref-method
  319. void **assign** **(** :ref:`Array<class_Array>` array **)**
  320. Assigns elements of another ``array`` into the array. Resizes the array to match ``array``. Performs type conversions if the array is typed.
  321. .. rst-class:: classref-item-separator
  322. ----
  323. .. _class_Array_method_back:
  324. .. rst-class:: classref-method
  325. :ref:`Variant<class_Variant>` **back** **(** **)** |const|
  326. Returns the last element of the array. Prints an error and returns ``null`` if the array is empty.
  327. \ **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.
  328. .. rst-class:: classref-item-separator
  329. ----
  330. .. _class_Array_method_bsearch:
  331. .. rst-class:: classref-method
  332. :ref:`int<class_int>` **bsearch** **(** :ref:`Variant<class_Variant>` value, :ref:`bool<class_bool>` before=true **)** |const|
  333. 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.
  334. ::
  335. var array = ["a", "b", "c", "c", "d", "e"]
  336. print(array.bsearch("c", true)) # Prints 2, at the first matching element.
  337. print(array.bsearch("c", false)) # Prints 4, after the last matching element, pointing to "d".
  338. \ **Note:** Calling :ref:`bsearch<class_Array_method_bsearch>` on an unsorted array results in unexpected behavior.
  339. .. rst-class:: classref-item-separator
  340. ----
  341. .. _class_Array_method_bsearch_custom:
  342. .. rst-class:: classref-method
  343. :ref:`int<class_int>` **bsearch_custom** **(** :ref:`Variant<class_Variant>` value, :ref:`Callable<class_Callable>` func, :ref:`bool<class_bool>` before=true **)** |const|
  344. 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. 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.
  345. \ **Note:** The custom method must accept the two arguments in any order, you cannot rely on that the first argument will always be from the array.
  346. \ **Note:** Calling :ref:`bsearch_custom<class_Array_method_bsearch_custom>` on an unsorted array results in unexpected behavior.
  347. .. rst-class:: classref-item-separator
  348. ----
  349. .. _class_Array_method_clear:
  350. .. rst-class:: classref-method
  351. void **clear** **(** **)**
  352. Clears the array. This is equivalent to using :ref:`resize<class_Array_method_resize>` with a size of ``0``.
  353. .. rst-class:: classref-item-separator
  354. ----
  355. .. _class_Array_method_count:
  356. .. rst-class:: classref-method
  357. :ref:`int<class_int>` **count** **(** :ref:`Variant<class_Variant>` value **)** |const|
  358. Returns the number of times an element is in the array.
  359. .. rst-class:: classref-item-separator
  360. ----
  361. .. _class_Array_method_duplicate:
  362. .. rst-class:: classref-method
  363. :ref:`Array<class_Array>` **duplicate** **(** :ref:`bool<class_bool>` deep=false **)** |const|
  364. Returns a copy of the array.
  365. 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. Note that any :ref:`Object<class_Object>`-derived elements will be shallow copied regardless of the ``deep`` setting.
  366. .. rst-class:: classref-item-separator
  367. ----
  368. .. _class_Array_method_erase:
  369. .. rst-class:: classref-method
  370. void **erase** **(** :ref:`Variant<class_Variant>` value **)**
  371. 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_at<class_Array_method_remove_at>` instead.
  372. \ **Note:** This method acts in-place and doesn't return a value.
  373. \ **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.
  374. \ **Note:** Do not erase entries while iterating over the array.
  375. .. rst-class:: classref-item-separator
  376. ----
  377. .. _class_Array_method_fill:
  378. .. rst-class:: classref-method
  379. void **fill** **(** :ref:`Variant<class_Variant>` value **)**
  380. 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:
  381. .. tabs::
  382. .. code-tab:: gdscript
  383. var array = []
  384. array.resize(10)
  385. array.fill(0) # Initialize the 10 elements to 0.
  386. .. code-tab:: csharp
  387. var array = new Godot.Collections.Array();
  388. array.Resize(10);
  389. array.Fill(0); // Initialize the 10 elements to 0.
  390. \ **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.
  391. .. rst-class:: classref-item-separator
  392. ----
  393. .. _class_Array_method_filter:
  394. .. rst-class:: classref-method
  395. :ref:`Array<class_Array>` **filter** **(** :ref:`Callable<class_Callable>` method **)** |const|
  396. Calls the provided :ref:`Callable<class_Callable>` on each element in the array and returns a new array with the elements for which the method returned ``true``.
  397. The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a boolean value.
  398. ::
  399. func _ready():
  400. print([1, 2, 3].filter(remove_1)) # Prints [2, 3].
  401. print([1, 2, 3].filter(func(number): return number != 1)) # Same as above, but using lambda function.
  402. func remove_1(number):
  403. return number != 1
  404. See also :ref:`any<class_Array_method_any>`, :ref:`all<class_Array_method_all>`, :ref:`map<class_Array_method_map>` and :ref:`reduce<class_Array_method_reduce>`.
  405. .. rst-class:: classref-item-separator
  406. ----
  407. .. _class_Array_method_find:
  408. .. rst-class:: classref-method
  409. :ref:`int<class_int>` **find** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=0 **)** |const|
  410. Searches the array for a value and returns its index or ``-1`` if not found. Optionally, the initial search index can be passed.
  411. .. rst-class:: classref-item-separator
  412. ----
  413. .. _class_Array_method_front:
  414. .. rst-class:: classref-method
  415. :ref:`Variant<class_Variant>` **front** **(** **)** |const|
  416. Returns the first element of the array. Prints an error and returns ``null`` if the array is empty.
  417. \ **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.
  418. .. rst-class:: classref-item-separator
  419. ----
  420. .. _class_Array_method_get_typed_builtin:
  421. .. rst-class:: classref-method
  422. :ref:`int<class_int>` **get_typed_builtin** **(** **)** |const|
  423. Returns the :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` constant for a typed array. If the **Array** is not typed, returns :ref:`@GlobalScope.TYPE_NIL<class_@GlobalScope_constant_TYPE_NIL>`.
  424. .. rst-class:: classref-item-separator
  425. ----
  426. .. _class_Array_method_get_typed_class_name:
  427. .. rst-class:: classref-method
  428. :ref:`StringName<class_StringName>` **get_typed_class_name** **(** **)** |const|
  429. Returns a class name of a typed **Array** of type :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`.
  430. .. rst-class:: classref-item-separator
  431. ----
  432. .. _class_Array_method_get_typed_script:
  433. .. rst-class:: classref-method
  434. :ref:`Variant<class_Variant>` **get_typed_script** **(** **)** |const|
  435. Returns the script associated with a typed array tied to a class name.
  436. .. rst-class:: classref-item-separator
  437. ----
  438. .. _class_Array_method_has:
  439. .. rst-class:: classref-method
  440. :ref:`bool<class_bool>` **has** **(** :ref:`Variant<class_Variant>` value **)** |const|
  441. Returns ``true`` if the array contains the given value.
  442. .. tabs::
  443. .. code-tab:: gdscript
  444. print(["inside", 7].has("inside")) # True
  445. print(["inside", 7].has("outside")) # False
  446. print(["inside", 7].has(7)) # True
  447. print(["inside", 7].has("7")) # False
  448. .. code-tab:: csharp
  449. var arr = new Godot.Collections.Array { "inside", 7 };
  450. // has is renamed to Contains
  451. GD.Print(arr.Contains("inside")); // True
  452. GD.Print(arr.Contains("outside")); // False
  453. GD.Print(arr.Contains(7)); // True
  454. GD.Print(arr.Contains("7")); // False
  455. \ **Note:** This is equivalent to using the ``in`` operator as follows:
  456. .. tabs::
  457. .. code-tab:: gdscript
  458. # Will evaluate to `true`.
  459. if 2 in [2, 4, 6, 8]:
  460. print("Contains!")
  461. .. code-tab:: csharp
  462. // As there is no "in" keyword in C#, you have to use Contains
  463. var array = new Godot.Collections.Array { 2, 4, 6, 8 };
  464. if (array.Contains(2))
  465. {
  466. GD.Print("Contains!");
  467. }
  468. .. rst-class:: classref-item-separator
  469. ----
  470. .. _class_Array_method_hash:
  471. .. rst-class:: classref-method
  472. :ref:`int<class_int>` **hash** **(** **)** |const|
  473. Returns a hashed 32-bit integer value representing the array and its contents.
  474. \ **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.
  475. .. rst-class:: classref-item-separator
  476. ----
  477. .. _class_Array_method_insert:
  478. .. rst-class:: classref-method
  479. :ref:`int<class_int>` **insert** **(** :ref:`int<class_int>` position, :ref:`Variant<class_Variant>` value **)**
  480. 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()``).
  481. \ **Note:** This method acts in-place and doesn't return a value.
  482. \ **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.
  483. .. rst-class:: classref-item-separator
  484. ----
  485. .. _class_Array_method_is_empty:
  486. .. rst-class:: classref-method
  487. :ref:`bool<class_bool>` **is_empty** **(** **)** |const|
  488. Returns ``true`` if the array is empty.
  489. .. rst-class:: classref-item-separator
  490. ----
  491. .. _class_Array_method_is_read_only:
  492. .. rst-class:: classref-method
  493. :ref:`bool<class_bool>` **is_read_only** **(** **)** |const|
  494. Returns ``true`` if the array is read-only. See :ref:`make_read_only<class_Array_method_make_read_only>`. Arrays are automatically read-only if declared with ``const`` keyword.
  495. .. rst-class:: classref-item-separator
  496. ----
  497. .. _class_Array_method_is_same_typed:
  498. .. rst-class:: classref-method
  499. :ref:`bool<class_bool>` **is_same_typed** **(** :ref:`Array<class_Array>` array **)** |const|
  500. Returns ``true`` if the array is typed the same as ``array``.
  501. .. rst-class:: classref-item-separator
  502. ----
  503. .. _class_Array_method_is_typed:
  504. .. rst-class:: classref-method
  505. :ref:`bool<class_bool>` **is_typed** **(** **)** |const|
  506. Returns ``true`` if the array is typed. Typed arrays can only store elements of their associated type and provide type safety for the ``[]`` operator. Methods of typed array still return :ref:`Variant<class_Variant>`.
  507. .. rst-class:: classref-item-separator
  508. ----
  509. .. _class_Array_method_make_read_only:
  510. .. rst-class:: classref-method
  511. void **make_read_only** **(** **)**
  512. Makes the array read-only, i.e. disabled modifying of the array's elements. Does not apply to nested content, e.g. content of nested arrays.
  513. .. rst-class:: classref-item-separator
  514. ----
  515. .. _class_Array_method_map:
  516. .. rst-class:: classref-method
  517. :ref:`Array<class_Array>` **map** **(** :ref:`Callable<class_Callable>` method **)** |const|
  518. Calls the provided :ref:`Callable<class_Callable>` for each element in the array and returns a new array filled with values returned by the method.
  519. The callable's method should take one :ref:`Variant<class_Variant>` parameter (the current array element) and can return any :ref:`Variant<class_Variant>`.
  520. ::
  521. func _ready():
  522. print([1, 2, 3].map(negate)) # Prints [-1, -2, -3].
  523. print([1, 2, 3].map(func(number): return -number)) # Same as above, but using lambda function.
  524. func negate(number):
  525. return -number
  526. See also :ref:`filter<class_Array_method_filter>`, :ref:`reduce<class_Array_method_reduce>`, :ref:`any<class_Array_method_any>` and :ref:`all<class_Array_method_all>`.
  527. .. rst-class:: classref-item-separator
  528. ----
  529. .. _class_Array_method_max:
  530. .. rst-class:: classref-method
  531. :ref:`Variant<class_Variant>` **max** **(** **)** |const|
  532. 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.
  533. To find the maximum value using a custom comparator, you can use :ref:`reduce<class_Array_method_reduce>`. In this example every array element is checked and the first maximum value is returned:
  534. ::
  535. func _ready():
  536. var arr = [Vector2(0, 1), Vector2(2, 0), Vector2(1, 1), Vector2(1, 0), Vector2(0, 2)]
  537. # In this example we compare the lengths.
  538. print(arr.reduce(func(max, val): return val if is_length_greater(val, max) else max))
  539. func is_length_greater(a, b):
  540. return a.length() > b.length()
  541. .. rst-class:: classref-item-separator
  542. ----
  543. .. _class_Array_method_min:
  544. .. rst-class:: classref-method
  545. :ref:`Variant<class_Variant>` **min** **(** **)** |const|
  546. 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.
  547. See also :ref:`max<class_Array_method_max>` for an example of using a custom comparator.
  548. .. rst-class:: classref-item-separator
  549. ----
  550. .. _class_Array_method_pick_random:
  551. .. rst-class:: classref-method
  552. :ref:`Variant<class_Variant>` **pick_random** **(** **)** |const|
  553. Returns a random value from the target array.
  554. .. tabs::
  555. .. code-tab:: gdscript
  556. var array: Array[int] = [1, 2, 3, 4]
  557. print(array.pick_random()) # Prints either of the four numbers.
  558. .. code-tab:: csharp
  559. var array = new Godot.Collections.Array { 1, 2, 3, 4 };
  560. GD.Print(array.PickRandom()); // Prints either of the four numbers.
  561. .. rst-class:: classref-item-separator
  562. ----
  563. .. _class_Array_method_pop_at:
  564. .. rst-class:: classref-method
  565. :ref:`Variant<class_Variant>` **pop_at** **(** :ref:`int<class_int>` position **)**
  566. 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.
  567. \ **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.
  568. .. rst-class:: classref-item-separator
  569. ----
  570. .. _class_Array_method_pop_back:
  571. .. rst-class:: classref-method
  572. :ref:`Variant<class_Variant>` **pop_back** **(** **)**
  573. 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>`.
  574. .. rst-class:: classref-item-separator
  575. ----
  576. .. _class_Array_method_pop_front:
  577. .. rst-class:: classref-method
  578. :ref:`Variant<class_Variant>` **pop_front** **(** **)**
  579. 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>`.
  580. \ **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.
  581. .. rst-class:: classref-item-separator
  582. ----
  583. .. _class_Array_method_push_back:
  584. .. rst-class:: classref-method
  585. void **push_back** **(** :ref:`Variant<class_Variant>` value **)**
  586. Appends an element at the end of the array. See also :ref:`push_front<class_Array_method_push_front>`.
  587. .. rst-class:: classref-item-separator
  588. ----
  589. .. _class_Array_method_push_front:
  590. .. rst-class:: classref-method
  591. void **push_front** **(** :ref:`Variant<class_Variant>` value **)**
  592. Adds an element at the beginning of the array. See also :ref:`push_back<class_Array_method_push_back>`.
  593. \ **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.
  594. .. rst-class:: classref-item-separator
  595. ----
  596. .. _class_Array_method_reduce:
  597. .. rst-class:: classref-method
  598. :ref:`Variant<class_Variant>` **reduce** **(** :ref:`Callable<class_Callable>` method, :ref:`Variant<class_Variant>` accum=null **)** |const|
  599. Calls the provided :ref:`Callable<class_Callable>` for each element in array and accumulates the result in ``accum``.
  600. The callable's method takes two arguments: the current value of ``accum`` and the current array element. If ``accum`` is ``null`` (default value), the iteration will start from the second element, with the first one used as initial value of ``accum``.
  601. ::
  602. func _ready():
  603. print([1, 2, 3].reduce(sum, 10)) # Prints 16.
  604. print([1, 2, 3].reduce(func(accum, number): return accum + number, 10)) # Same as above, but using lambda function.
  605. func sum(accum, number):
  606. return accum + number
  607. See also :ref:`map<class_Array_method_map>`, :ref:`filter<class_Array_method_filter>`, :ref:`any<class_Array_method_any>` and :ref:`all<class_Array_method_all>`.
  608. .. rst-class:: classref-item-separator
  609. ----
  610. .. _class_Array_method_remove_at:
  611. .. rst-class:: classref-method
  612. void **remove_at** **(** :ref:`int<class_int>` position **)**
  613. 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.
  614. \ **Note:** This method acts in-place and doesn't return a value.
  615. \ **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.
  616. \ **Note:** ``position`` cannot be negative. To remove an element relative to the end of the array, use ``arr.remove_at(arr.size() - (i + 1))``. To remove the last element from the array without returning the value, use ``arr.resize(arr.size() - 1)``.
  617. .. rst-class:: classref-item-separator
  618. ----
  619. .. _class_Array_method_resize:
  620. .. rst-class:: classref-method
  621. :ref:`int<class_int>` **resize** **(** :ref:`int<class_int>` size **)**
  622. 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``.
  623. .. rst-class:: classref-item-separator
  624. ----
  625. .. _class_Array_method_reverse:
  626. .. rst-class:: classref-method
  627. void **reverse** **(** **)**
  628. Reverses the order of the elements in the array.
  629. .. rst-class:: classref-item-separator
  630. ----
  631. .. _class_Array_method_rfind:
  632. .. rst-class:: classref-method
  633. :ref:`int<class_int>` **rfind** **(** :ref:`Variant<class_Variant>` what, :ref:`int<class_int>` from=-1 **)** |const|
  634. 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.
  635. .. rst-class:: classref-item-separator
  636. ----
  637. .. _class_Array_method_shuffle:
  638. .. rst-class:: classref-method
  639. void **shuffle** **(** **)**
  640. 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:`@GlobalScope.randi<class_@GlobalScope_method_randi>`. Call :ref:`@GlobalScope.randomize<class_@GlobalScope_method_randomize>` to ensure that a new seed will be used each time if you want non-reproducible shuffling.
  641. .. rst-class:: classref-item-separator
  642. ----
  643. .. _class_Array_method_size:
  644. .. rst-class:: classref-method
  645. :ref:`int<class_int>` **size** **(** **)** |const|
  646. Returns the number of elements in the array.
  647. .. rst-class:: classref-item-separator
  648. ----
  649. .. _class_Array_method_slice:
  650. .. rst-class:: classref-method
  651. :ref:`Array<class_Array>` **slice** **(** :ref:`int<class_int>` begin, :ref:`int<class_int>` end=2147483647, :ref:`int<class_int>` step=1, :ref:`bool<class_bool>` deep=false **)** |const|
  652. Returns the slice of the **Array**, from ``begin`` (inclusive) to ``end`` (exclusive), as a new **Array**.
  653. The absolute value of ``begin`` and ``end`` will be clamped to the array size, so the default value for ``end`` makes it slice to the size of the array by default (i.e. ``arr.slice(1)`` is a shorthand for ``arr.slice(1, arr.size())``).
  654. If either ``begin`` or ``end`` are negative, they will be relative to the end of the array (i.e. ``arr.slice(0, -2)`` is a shorthand for ``arr.slice(0, arr.size() - 2)``).
  655. If specified, ``step`` is the relative index between source elements. It can be negative, then ``begin`` must be higher than ``end``. For example, ``[0, 1, 2, 3, 4, 5].slice(5, 1, -2)`` returns ``[5, 3]``.
  656. If ``deep`` is true, each element will be copied by value rather than by reference.
  657. \ **Note:** To include the first element when ``step`` is negative, use ``arr.slice(begin, -arr.size() - 1, step)`` (i.e. ``[0, 1, 2].slice(1, -4, -1)`` returns ``[1, 0]``).
  658. .. rst-class:: classref-item-separator
  659. ----
  660. .. _class_Array_method_sort:
  661. .. rst-class:: classref-method
  662. void **sort** **(** **)**
  663. Sorts the array.
  664. \ **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>`.
  665. \ **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:
  666. .. tabs::
  667. .. code-tab:: gdscript
  668. var strings = ["string1", "string2", "string10", "string11"]
  669. strings.sort()
  670. print(strings) # Prints [string1, string10, string11, string2]
  671. .. code-tab:: csharp
  672. var strings = new Godot.Collections.Array { "string1", "string2", "string10", "string11" };
  673. strings.Sort();
  674. GD.Print(strings); // Prints [string1, string10, string11, string2]
  675. To perform natural order sorting, you can use :ref:`sort_custom<class_Array_method_sort_custom>` with :ref:`String.naturalnocasecmp_to<class_String_method_naturalnocasecmp_to>` as follows:
  676. ::
  677. var strings = ["string1", "string2", "string10", "string11"]
  678. strings.sort_custom(func(a, b): return a.naturalnocasecmp_to(b) < 0)
  679. print(strings) # Prints [string1, string2, string10, string11]
  680. .. rst-class:: classref-item-separator
  681. ----
  682. .. _class_Array_method_sort_custom:
  683. .. rst-class:: classref-method
  684. void **sort_custom** **(** :ref:`Callable<class_Callable>` func **)**
  685. Sorts the array using a custom method. The custom method receives two arguments (a pair of elements from the array) and must return either ``true`` or ``false``. For two elements ``a`` and ``b``, if the given method returns ``true``, element ``b`` will be after element ``a`` in the array.
  686. \ **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>`.
  687. \ **Note:** You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Randomizing the return value will result in unexpected behavior.
  688. .. tabs::
  689. .. code-tab:: gdscript
  690. func sort_ascending(a, b):
  691. if a[0] < b[0]:
  692. return true
  693. return false
  694. func _ready():
  695. var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
  696. my_items.sort_custom(sort_ascending)
  697. print(my_items) # Prints [[4, Tomato], [5, Potato], [9, Rice]].
  698. # Descending, lambda version.
  699. my_items.sort_custom(func(a, b): return a[0] > b[0])
  700. print(my_items) # Prints [[9, Rice], [5, Potato], [4, Tomato]].
  701. .. code-tab:: csharp
  702. // There is no custom sort support for Godot.Collections.Array
  703. .. rst-class:: classref-section-separator
  704. ----
  705. .. rst-class:: classref-descriptions-group
  706. Operator Descriptions
  707. ---------------------
  708. .. _class_Array_operator_neq_Array:
  709. .. rst-class:: classref-operator
  710. :ref:`bool<class_bool>` **operator !=** **(** :ref:`Array<class_Array>` right **)**
  711. Compares the left operand **Array** against the ``right`` **Array**. Returns ``true`` if the sizes or contents of the arrays are *not* equal, ``false`` otherwise.
  712. .. rst-class:: classref-item-separator
  713. ----
  714. .. _class_Array_operator_sum_Array:
  715. .. rst-class:: classref-operator
  716. :ref:`Array<class_Array>` **operator +** **(** :ref:`Array<class_Array>` right **)**
  717. Concatenates two **Array**\ s together, with the ``right`` **Array** being added to the end of the **Array** specified in the left operand. For example, ``[1, 2] + [3, 4]`` results in ``[1, 2, 3, 4]``.
  718. .. rst-class:: classref-item-separator
  719. ----
  720. .. _class_Array_operator_lt_Array:
  721. .. rst-class:: classref-operator
  722. :ref:`bool<class_bool>` **operator <** **(** :ref:`Array<class_Array>` right **)**
  723. Performs a comparison for each index between the left operand **Array** and the ``right`` **Array**, considering the highest common index of both arrays for this comparison: Returns ``true`` on the first occurrence of an element that is less, or ``false`` if the element is greater. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns ``false`` if the left operand **Array** has fewer elements, otherwise it returns ``true``.
  724. .. rst-class:: classref-item-separator
  725. ----
  726. .. _class_Array_operator_lte_Array:
  727. .. rst-class:: classref-operator
  728. :ref:`bool<class_bool>` **operator <=** **(** :ref:`Array<class_Array>` right **)**
  729. Performs a comparison for each index between the left operand **Array** and the ``right`` **Array**, considering the highest common index of both arrays for this comparison: Returns ``true`` on the first occurrence of an element that is less, or ``false`` if the element is greater. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns ``true`` if the left operand **Array** has the same number of elements or fewer, otherwise it returns ``false``.
  730. .. rst-class:: classref-item-separator
  731. ----
  732. .. _class_Array_operator_eq_Array:
  733. .. rst-class:: classref-operator
  734. :ref:`bool<class_bool>` **operator ==** **(** :ref:`Array<class_Array>` right **)**
  735. Compares the left operand **Array** against the ``right`` **Array**. Returns ``true`` if the sizes and contents of the arrays are equal, ``false`` otherwise.
  736. .. rst-class:: classref-item-separator
  737. ----
  738. .. _class_Array_operator_gt_Array:
  739. .. rst-class:: classref-operator
  740. :ref:`bool<class_bool>` **operator >** **(** :ref:`Array<class_Array>` right **)**
  741. Performs a comparison for each index between the left operand **Array** and the ``right`` **Array**, considering the highest common index of both arrays for this comparison: Returns ``true`` on the first occurrence of an element that is greater, or ``false`` if the element is less. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns ``true`` if the ``right`` **Array** has more elements, otherwise it returns ``false``.
  742. .. rst-class:: classref-item-separator
  743. ----
  744. .. _class_Array_operator_gte_Array:
  745. .. rst-class:: classref-operator
  746. :ref:`bool<class_bool>` **operator >=** **(** :ref:`Array<class_Array>` right **)**
  747. Performs a comparison for each index between the left operand **Array** and the ``right`` **Array**, considering the highest common index of both arrays for this comparison: Returns ``true`` on the first occurrence of an element that is greater, or ``false`` if the element is less. Note that depending on the type of data stored, this function may be recursive. If all elements are equal, it compares the length of both arrays and returns ``true`` if the ``right`` **Array** has more or the same number of elements, otherwise it returns ``false``.
  748. .. rst-class:: classref-item-separator
  749. ----
  750. .. _class_Array_operator_idx_int:
  751. .. rst-class:: classref-operator
  752. :ref:`Variant<class_Variant>` **operator []** **(** :ref:`int<class_int>` index **)**
  753. Returns a reference to the element of type :ref:`Variant<class_Variant>` at the specified location. Arrays start at index 0. ``index`` can be a zero or positive value to start from the beginning, or a negative value to start from the end. Out-of-bounds array access causes a run-time error, which will result in an error being printed and the project execution pausing if run from the editor.
  754. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  755. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  756. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  757. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  758. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  759. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  760. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`