class_array.rst 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. :github_url: hide
  2. .. DO NOT EDIT THIS FILE!!!
  3. .. Generated automatically from Godot engine sources.
  4. .. Generator: https://github.com/godotengine/godot/tree/master/doc/tools/make_rst.py.
  5. .. XML source: https://github.com/godotengine/godot/tree/master/doc/classes/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 :ref:`Variant<class_Variant>` 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. .. tabs::
  15. .. code-tab:: gdscript
  16. var array = ["First", 2, 3, "Last"]
  17. print(array[0]) # Prints "First"
  18. print(array[2]) # Prints 3
  19. print(array[-1]) # Prints "Last"
  20. array[1] = "Second"
  21. print(array[1]) # Prints "Second"
  22. print(array[-3]) # Prints "Second"
  23. .. code-tab:: csharp
  24. Godot.Collections.Array array = ["First", 2, 3, "Last"];
  25. GD.Print(array[0]); // Prints "First"
  26. GD.Print(array[2]); // Prints 3
  27. GD.Print(array[^1]); // Prints "Last"
  28. array[1] = "Second";
  29. GD.Print(array[1]); // Prints "Second"
  30. GD.Print(array[^3]); // Prints "Second"
  31. \ **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>`.
  32. \ **Note:** Erasing elements while iterating over arrays is **not** supported and will result in unpredictable behavior.
  33. \ **Differences between packed arrays, typed arrays, and untyped arrays:** Packed arrays are generally faster to iterate on and modify compared to a typed array of the same type (e.g. :ref:`PackedInt64Array<class_PackedInt64Array>` versus ``Array[int]``). Also, packed arrays consume less memory. As a downside, packed arrays are less flexible as they don't offer as many convenience methods such as :ref:`map()<class_Array_method_map>`. Typed arrays are in turn faster to iterate on and modify than untyped arrays.
  34. .. note::
  35. There are notable differences when using this API with C#. See :ref:`doc_c_sharp_differences` for more information.
  36. .. rst-class:: classref-reftable-group
  37. Constructors
  38. ------------
  39. .. table::
  40. :widths: auto
  41. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ ) |
  43. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ base\: :ref:`Array<class_Array>`, type\: :ref:`int<class_int>`, class_name\: :ref:`StringName<class_StringName>`, script\: :ref:`Variant<class_Variant>`\ ) |
  45. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`Array<class_Array>`\ ) |
  47. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedByteArray<class_PackedByteArray>`\ ) |
  49. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedColorArray<class_PackedColorArray>`\ ) |
  51. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedFloat32Array<class_PackedFloat32Array>`\ ) |
  53. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedFloat64Array<class_PackedFloat64Array>`\ ) |
  55. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedInt32Array<class_PackedInt32Array>`\ ) |
  57. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedInt64Array<class_PackedInt64Array>`\ ) |
  59. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedStringArray<class_PackedStringArray>`\ ) |
  61. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedVector2Array<class_PackedVector2Array>`\ ) |
  63. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedVector3Array<class_PackedVector3Array>`\ ) |
  65. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | :ref:`Array<class_Array>` | :ref:`Array<class_Array_constructor_Array>`\ (\ from\: :ref:`PackedVector4Array<class_PackedVector4Array>`\ ) |
  67. +---------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. .. rst-class:: classref-reftable-group
  69. Methods
  70. -------
  71. .. table::
  72. :widths: auto
  73. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`bool<class_bool>` | :ref:`all<class_Array_method_all>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
  75. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | :ref:`bool<class_bool>` | :ref:`any<class_Array_method_any>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
  77. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | |void| | :ref:`append<class_Array_method_append>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
  79. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. | |void| | :ref:`append_array<class_Array_method_append_array>`\ (\ array\: :ref:`Array<class_Array>`\ ) |
  81. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  82. | |void| | :ref:`assign<class_Array_method_assign>`\ (\ array\: :ref:`Array<class_Array>`\ ) |
  83. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  84. | :ref:`Variant<class_Variant>` | :ref:`back<class_Array_method_back>`\ (\ ) |const| |
  85. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  86. | :ref:`int<class_int>` | :ref:`bsearch<class_Array_method_bsearch>`\ (\ value\: :ref:`Variant<class_Variant>`, before\: :ref:`bool<class_bool>` = true\ ) |const| |
  87. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  88. | :ref:`int<class_int>` | :ref:`bsearch_custom<class_Array_method_bsearch_custom>`\ (\ value\: :ref:`Variant<class_Variant>`, func\: :ref:`Callable<class_Callable>`, before\: :ref:`bool<class_bool>` = true\ ) |const| |
  89. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  90. | |void| | :ref:`clear<class_Array_method_clear>`\ (\ ) |
  91. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  92. | :ref:`int<class_int>` | :ref:`count<class_Array_method_count>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| |
  93. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  94. | :ref:`Array<class_Array>` | :ref:`duplicate<class_Array_method_duplicate>`\ (\ deep\: :ref:`bool<class_bool>` = false\ ) |const| |
  95. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  96. | :ref:`Array<class_Array>` | :ref:`duplicate_deep<class_Array_method_duplicate_deep>`\ (\ deep_subresources_mode\: :ref:`int<class_int>` = 1\ ) |const| |
  97. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  98. | |void| | :ref:`erase<class_Array_method_erase>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
  99. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  100. | |void| | :ref:`fill<class_Array_method_fill>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
  101. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  102. | :ref:`Array<class_Array>` | :ref:`filter<class_Array_method_filter>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
  103. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  104. | :ref:`int<class_int>` | :ref:`find<class_Array_method_find>`\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = 0\ ) |const| |
  105. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  106. | :ref:`int<class_int>` | :ref:`find_custom<class_Array_method_find_custom>`\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = 0\ ) |const| |
  107. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  108. | :ref:`Variant<class_Variant>` | :ref:`front<class_Array_method_front>`\ (\ ) |const| |
  109. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  110. | :ref:`Variant<class_Variant>` | :ref:`get<class_Array_method_get>`\ (\ index\: :ref:`int<class_int>`\ ) |const| |
  111. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  112. | :ref:`int<class_int>` | :ref:`get_typed_builtin<class_Array_method_get_typed_builtin>`\ (\ ) |const| |
  113. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  114. | :ref:`StringName<class_StringName>` | :ref:`get_typed_class_name<class_Array_method_get_typed_class_name>`\ (\ ) |const| |
  115. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  116. | :ref:`Variant<class_Variant>` | :ref:`get_typed_script<class_Array_method_get_typed_script>`\ (\ ) |const| |
  117. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  118. | :ref:`bool<class_bool>` | :ref:`has<class_Array_method_has>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| |
  119. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  120. | :ref:`int<class_int>` | :ref:`hash<class_Array_method_hash>`\ (\ ) |const| |
  121. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  122. | :ref:`int<class_int>` | :ref:`insert<class_Array_method_insert>`\ (\ position\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) |
  123. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  124. | :ref:`bool<class_bool>` | :ref:`is_empty<class_Array_method_is_empty>`\ (\ ) |const| |
  125. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  126. | :ref:`bool<class_bool>` | :ref:`is_read_only<class_Array_method_is_read_only>`\ (\ ) |const| |
  127. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  128. | :ref:`bool<class_bool>` | :ref:`is_same_typed<class_Array_method_is_same_typed>`\ (\ array\: :ref:`Array<class_Array>`\ ) |const| |
  129. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  130. | :ref:`bool<class_bool>` | :ref:`is_typed<class_Array_method_is_typed>`\ (\ ) |const| |
  131. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  132. | |void| | :ref:`make_read_only<class_Array_method_make_read_only>`\ (\ ) |
  133. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  134. | :ref:`Array<class_Array>` | :ref:`map<class_Array_method_map>`\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| |
  135. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  136. | :ref:`Variant<class_Variant>` | :ref:`max<class_Array_method_max>`\ (\ ) |const| |
  137. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  138. | :ref:`Variant<class_Variant>` | :ref:`min<class_Array_method_min>`\ (\ ) |const| |
  139. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  140. | :ref:`Variant<class_Variant>` | :ref:`pick_random<class_Array_method_pick_random>`\ (\ ) |const| |
  141. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  142. | :ref:`Variant<class_Variant>` | :ref:`pop_at<class_Array_method_pop_at>`\ (\ position\: :ref:`int<class_int>`\ ) |
  143. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  144. | :ref:`Variant<class_Variant>` | :ref:`pop_back<class_Array_method_pop_back>`\ (\ ) |
  145. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  146. | :ref:`Variant<class_Variant>` | :ref:`pop_front<class_Array_method_pop_front>`\ (\ ) |
  147. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  148. | |void| | :ref:`push_back<class_Array_method_push_back>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
  149. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  150. | |void| | :ref:`push_front<class_Array_method_push_front>`\ (\ value\: :ref:`Variant<class_Variant>`\ ) |
  151. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  152. | :ref:`Variant<class_Variant>` | :ref:`reduce<class_Array_method_reduce>`\ (\ method\: :ref:`Callable<class_Callable>`, accum\: :ref:`Variant<class_Variant>` = null\ ) |const| |
  153. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  154. | |void| | :ref:`remove_at<class_Array_method_remove_at>`\ (\ position\: :ref:`int<class_int>`\ ) |
  155. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  156. | :ref:`int<class_int>` | :ref:`resize<class_Array_method_resize>`\ (\ size\: :ref:`int<class_int>`\ ) |
  157. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  158. | |void| | :ref:`reverse<class_Array_method_reverse>`\ (\ ) |
  159. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  160. | :ref:`int<class_int>` | :ref:`rfind<class_Array_method_rfind>`\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = -1\ ) |const| |
  161. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  162. | :ref:`int<class_int>` | :ref:`rfind_custom<class_Array_method_rfind_custom>`\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = -1\ ) |const| |
  163. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  164. | |void| | :ref:`set<class_Array_method_set>`\ (\ index\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) |
  165. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  166. | |void| | :ref:`shuffle<class_Array_method_shuffle>`\ (\ ) |
  167. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  168. | :ref:`int<class_int>` | :ref:`size<class_Array_method_size>`\ (\ ) |const| |
  169. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  170. | :ref:`Array<class_Array>` | :ref:`slice<class_Array_method_slice>`\ (\ begin\: :ref:`int<class_int>`, end\: :ref:`int<class_int>` = 2147483647, step\: :ref:`int<class_int>` = 1, deep\: :ref:`bool<class_bool>` = false\ ) |const| |
  171. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  172. | |void| | :ref:`sort<class_Array_method_sort>`\ (\ ) |
  173. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  174. | |void| | :ref:`sort_custom<class_Array_method_sort_custom>`\ (\ func\: :ref:`Callable<class_Callable>`\ ) |
  175. +-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  176. .. rst-class:: classref-reftable-group
  177. Operators
  178. ---------
  179. .. table::
  180. :widths: auto
  181. +-------------------------------+----------------------------------------------------------------------------------------------+
  182. | :ref:`bool<class_bool>` | :ref:`operator !=<class_Array_operator_neq_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
  183. +-------------------------------+----------------------------------------------------------------------------------------------+
  184. | :ref:`Array<class_Array>` | :ref:`operator +<class_Array_operator_sum_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
  185. +-------------------------------+----------------------------------------------------------------------------------------------+
  186. | :ref:`bool<class_bool>` | :ref:`operator \<<class_Array_operator_lt_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
  187. +-------------------------------+----------------------------------------------------------------------------------------------+
  188. | :ref:`bool<class_bool>` | :ref:`operator \<=<class_Array_operator_lte_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
  189. +-------------------------------+----------------------------------------------------------------------------------------------+
  190. | :ref:`bool<class_bool>` | :ref:`operator ==<class_Array_operator_eq_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
  191. +-------------------------------+----------------------------------------------------------------------------------------------+
  192. | :ref:`bool<class_bool>` | :ref:`operator ><class_Array_operator_gt_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
  193. +-------------------------------+----------------------------------------------------------------------------------------------+
  194. | :ref:`bool<class_bool>` | :ref:`operator >=<class_Array_operator_gte_Array>`\ (\ right\: :ref:`Array<class_Array>`\ ) |
  195. +-------------------------------+----------------------------------------------------------------------------------------------+
  196. | :ref:`Variant<class_Variant>` | :ref:`operator []<class_Array_operator_idx_int>`\ (\ index\: :ref:`int<class_int>`\ ) |
  197. +-------------------------------+----------------------------------------------------------------------------------------------+
  198. .. rst-class:: classref-section-separator
  199. ----
  200. .. rst-class:: classref-descriptions-group
  201. Constructor Descriptions
  202. ------------------------
  203. .. _class_Array_constructor_Array:
  204. .. rst-class:: classref-constructor
  205. :ref:`Array<class_Array>` **Array**\ (\ ) :ref:`๐Ÿ”—<class_Array_constructor_Array>`
  206. Constructs an empty **Array**.
  207. .. rst-class:: classref-item-separator
  208. ----
  209. .. rst-class:: classref-constructor
  210. :ref:`Array<class_Array>` **Array**\ (\ base\: :ref:`Array<class_Array>`, type\: :ref:`int<class_int>`, class_name\: :ref:`StringName<class_StringName>`, script\: :ref:`Variant<class_Variant>`\ )
  211. Creates a typed array from the ``base`` array. A typed array can only contain elements of the given type, or that inherit from the given class, as described by this constructor's parameters:
  212. - ``type`` is the built-in :ref:`Variant<class_Variant>` type, as one the :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` constants.
  213. - ``class_name`` is the built-in class name (see :ref:`Object.get_class()<class_Object_method_get_class>`).
  214. - ``script`` is the associated script. It must be a :ref:`Script<class_Script>` instance or ``null``.
  215. If ``type`` is not :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`, ``class_name`` must be an empty :ref:`StringName<class_StringName>` and ``script`` must be ``null``.
  216. ::
  217. class_name Sword
  218. extends Node
  219. class Stats:
  220. pass
  221. func _ready():
  222. var a = Array([], TYPE_INT, "", null) # Array[int]
  223. var b = Array([], TYPE_OBJECT, "Node", null) # Array[Node]
  224. var c = Array([], TYPE_OBJECT, "Node", Sword) # Array[Sword]
  225. var d = Array([], TYPE_OBJECT, "RefCounted", Stats) # Array[Stats]
  226. The ``base`` array's elements are converted when necessary. If this is not possible or ``base`` is already typed, this constructor fails and returns an empty **Array**.
  227. In GDScript, this constructor is usually not necessary, as it is possible to create a typed array through static typing:
  228. ::
  229. var numbers: Array[float] = []
  230. var children: Array[Node] = [$Node, $Sprite2D, $RigidBody3D]
  231. var integers: Array[int] = [0.2, 4.5, -2.0]
  232. print(integers) # Prints [0, 4, -2]
  233. .. rst-class:: classref-item-separator
  234. ----
  235. .. rst-class:: classref-constructor
  236. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`Array<class_Array>`\ )
  237. Returns the same array as ``from``. If you need a copy of the array, use :ref:`duplicate()<class_Array_method_duplicate>`.
  238. .. rst-class:: classref-item-separator
  239. ----
  240. .. rst-class:: classref-constructor
  241. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedByteArray<class_PackedByteArray>`\ )
  242. Constructs an array from a :ref:`PackedByteArray<class_PackedByteArray>`.
  243. .. rst-class:: classref-item-separator
  244. ----
  245. .. rst-class:: classref-constructor
  246. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedColorArray<class_PackedColorArray>`\ )
  247. Constructs an array from a :ref:`PackedColorArray<class_PackedColorArray>`.
  248. .. rst-class:: classref-item-separator
  249. ----
  250. .. rst-class:: classref-constructor
  251. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedFloat32Array<class_PackedFloat32Array>`\ )
  252. Constructs an array from a :ref:`PackedFloat32Array<class_PackedFloat32Array>`.
  253. .. rst-class:: classref-item-separator
  254. ----
  255. .. rst-class:: classref-constructor
  256. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedFloat64Array<class_PackedFloat64Array>`\ )
  257. Constructs an array from a :ref:`PackedFloat64Array<class_PackedFloat64Array>`.
  258. .. rst-class:: classref-item-separator
  259. ----
  260. .. rst-class:: classref-constructor
  261. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedInt32Array<class_PackedInt32Array>`\ )
  262. Constructs an array from a :ref:`PackedInt32Array<class_PackedInt32Array>`.
  263. .. rst-class:: classref-item-separator
  264. ----
  265. .. rst-class:: classref-constructor
  266. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedInt64Array<class_PackedInt64Array>`\ )
  267. Constructs an array from a :ref:`PackedInt64Array<class_PackedInt64Array>`.
  268. .. rst-class:: classref-item-separator
  269. ----
  270. .. rst-class:: classref-constructor
  271. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedStringArray<class_PackedStringArray>`\ )
  272. Constructs an array from a :ref:`PackedStringArray<class_PackedStringArray>`.
  273. .. rst-class:: classref-item-separator
  274. ----
  275. .. rst-class:: classref-constructor
  276. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedVector2Array<class_PackedVector2Array>`\ )
  277. Constructs an array from a :ref:`PackedVector2Array<class_PackedVector2Array>`.
  278. .. rst-class:: classref-item-separator
  279. ----
  280. .. rst-class:: classref-constructor
  281. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedVector3Array<class_PackedVector3Array>`\ )
  282. Constructs an array from a :ref:`PackedVector3Array<class_PackedVector3Array>`.
  283. .. rst-class:: classref-item-separator
  284. ----
  285. .. rst-class:: classref-constructor
  286. :ref:`Array<class_Array>` **Array**\ (\ from\: :ref:`PackedVector4Array<class_PackedVector4Array>`\ )
  287. Constructs an array from a :ref:`PackedVector4Array<class_PackedVector4Array>`.
  288. .. rst-class:: classref-section-separator
  289. ----
  290. .. rst-class:: classref-descriptions-group
  291. Method Descriptions
  292. -------------------
  293. .. _class_Array_method_all:
  294. .. rst-class:: classref-method
  295. :ref:`bool<class_bool>` **all**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_all>`
  296. Calls the given :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``.
  297. The ``method`` should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a :ref:`bool<class_bool>`.
  298. .. tabs::
  299. .. code-tab:: gdscript
  300. func greater_than_5(number):
  301. return number > 5
  302. func _ready():
  303. print([6, 10, 6].all(greater_than_5)) # Prints true (3/3 elements evaluate to true).
  304. print([4, 10, 4].all(greater_than_5)) # Prints false (1/3 elements evaluate to true).
  305. print([4, 4, 4].all(greater_than_5)) # Prints false (0/3 elements evaluate to true).
  306. print([].all(greater_than_5)) # Prints true (0/0 elements evaluate to true).
  307. # Same as the first line above, but using a lambda function.
  308. print([6, 10, 6].all(func(element): return element > 5)) # Prints true
  309. .. code-tab:: csharp
  310. private static bool GreaterThan5(int number)
  311. {
  312. return number > 5;
  313. }
  314. public override void _Ready()
  315. {
  316. // Prints True (3/3 elements evaluate to true).
  317. GD.Print(new Godot.Collections.Array>int< { 6, 10, 6 }.All(GreaterThan5));
  318. // Prints False (1/3 elements evaluate to true).
  319. GD.Print(new Godot.Collections.Array>int< { 4, 10, 4 }.All(GreaterThan5));
  320. // Prints False (0/3 elements evaluate to true).
  321. GD.Print(new Godot.Collections.Array>int< { 4, 4, 4 }.All(GreaterThan5));
  322. // Prints True (0/0 elements evaluate to true).
  323. GD.Print(new Godot.Collections.Array>int< { }.All(GreaterThan5));
  324. // Same as the first line above, but using a lambda function.
  325. GD.Print(new Godot.Collections.Array>int< { 6, 10, 6 }.All(element => element > 5)); // Prints True
  326. }
  327. 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>`.
  328. \ **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).
  329. \ **Note:** For an empty array, this method `always <https://en.wikipedia.org/wiki/Vacuous_truth>`__ returns ``true``.
  330. .. rst-class:: classref-item-separator
  331. ----
  332. .. _class_Array_method_any:
  333. .. rst-class:: classref-method
  334. :ref:`bool<class_bool>` **any**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_any>`
  335. Calls the given :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``.
  336. The ``method`` should take one :ref:`Variant<class_Variant>` parameter (the current array element) and return a :ref:`bool<class_bool>`.
  337. ::
  338. func greater_than_5(number):
  339. return number > 5
  340. func _ready():
  341. print([6, 10, 6].any(greater_than_5)) # Prints true (3 elements evaluate to true).
  342. print([4, 10, 4].any(greater_than_5)) # Prints true (1 elements evaluate to true).
  343. print([4, 4, 4].any(greater_than_5)) # Prints false (0 elements evaluate to true).
  344. print([].any(greater_than_5)) # Prints false (0 elements evaluate to true).
  345. # Same as the first line above, but using a lambda function.
  346. print([6, 10, 6].any(func(number): return number > 5)) # Prints true
  347. 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>`.
  348. \ **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).
  349. \ **Note:** For an empty array, this method always returns ``false``.
  350. .. rst-class:: classref-item-separator
  351. ----
  352. .. _class_Array_method_append:
  353. .. rst-class:: classref-method
  354. |void| **append**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`๐Ÿ”—<class_Array_method_append>`
  355. Appends ``value`` at the end of the array (alias of :ref:`push_back()<class_Array_method_push_back>`).
  356. .. rst-class:: classref-item-separator
  357. ----
  358. .. _class_Array_method_append_array:
  359. .. rst-class:: classref-method
  360. |void| **append_array**\ (\ array\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_method_append_array>`
  361. Appends another ``array`` at the end of this array.
  362. ::
  363. var numbers = [1, 2, 3]
  364. var extra = [4, 5, 6]
  365. numbers.append_array(extra)
  366. print(numbers) # Prints [1, 2, 3, 4, 5, 6]
  367. .. rst-class:: classref-item-separator
  368. ----
  369. .. _class_Array_method_assign:
  370. .. rst-class:: classref-method
  371. |void| **assign**\ (\ array\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_method_assign>`
  372. Assigns elements of another ``array`` into the array. Resizes the array to match ``array``. Performs type conversions if the array is typed.
  373. .. rst-class:: classref-item-separator
  374. ----
  375. .. _class_Array_method_back:
  376. .. rst-class:: classref-method
  377. :ref:`Variant<class_Variant>` **back**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_back>`
  378. Returns the last element of the array. If the array is empty, fails and returns ``null``. See also :ref:`front()<class_Array_method_front>`.
  379. \ **Note:** Unlike with the ``[]`` operator (``array[-1]``), an error is generated without stopping project execution.
  380. .. rst-class:: classref-item-separator
  381. ----
  382. .. _class_Array_method_bsearch:
  383. .. rst-class:: classref-method
  384. :ref:`int<class_int>` **bsearch**\ (\ value\: :ref:`Variant<class_Variant>`, before\: :ref:`bool<class_bool>` = true\ ) |const| :ref:`๐Ÿ”—<class_Array_method_bsearch>`
  385. Returns the index of ``value`` in the sorted array. If it cannot be found, returns where ``value`` should be inserted to keep the array sorted. The algorithm used is `binary search <https://en.wikipedia.org/wiki/Binary_search_algorithm>`__.
  386. If ``before`` is ``true`` (as by default), the returned index comes before all existing elements equal to ``value`` in the array.
  387. ::
  388. var numbers = [2, 4, 8, 10]
  389. var idx = numbers.bsearch(7)
  390. numbers.insert(idx, 7)
  391. print(numbers) # Prints [2, 4, 7, 8, 10]
  392. var fruits = ["Apple", "Lemon", "Lemon", "Orange"]
  393. print(fruits.bsearch("Lemon", true)) # Prints 1, points at the first "Lemon".
  394. print(fruits.bsearch("Lemon", false)) # Prints 3, points at "Orange".
  395. \ **Note:** Calling :ref:`bsearch()<class_Array_method_bsearch>` on an *unsorted* array will result in unexpected behavior. Use :ref:`sort()<class_Array_method_sort>` before calling this method.
  396. .. rst-class:: classref-item-separator
  397. ----
  398. .. _class_Array_method_bsearch_custom:
  399. .. rst-class:: classref-method
  400. :ref:`int<class_int>` **bsearch_custom**\ (\ value\: :ref:`Variant<class_Variant>`, func\: :ref:`Callable<class_Callable>`, before\: :ref:`bool<class_bool>` = true\ ) |const| :ref:`๐Ÿ”—<class_Array_method_bsearch_custom>`
  401. Returns the index of ``value`` in the sorted array. If it cannot be found, returns where ``value`` should be inserted to keep the array sorted (using ``func`` for the comparisons). The algorithm used is `binary search <https://en.wikipedia.org/wiki/Binary_search_algorithm>`__.
  402. Similar to :ref:`sort_custom()<class_Array_method_sort_custom>`, ``func`` is called as many times as necessary, receiving one array element and ``value`` as arguments. The function should return ``true`` if the array element should be *behind* ``value``, otherwise it should return ``false``.
  403. If ``before`` is ``true`` (as by default), the returned index comes before all existing elements equal to ``value`` in the array.
  404. ::
  405. func sort_by_amount(a, b):
  406. if a[1] < b[1]:
  407. return true
  408. return false
  409. func _ready():
  410. var my_items = [["Tomato", 2], ["Kiwi", 5], ["Rice", 9]]
  411. var apple = ["Apple", 5]
  412. # "Apple" is inserted before "Kiwi".
  413. my_items.insert(my_items.bsearch_custom(apple, sort_by_amount, true), apple)
  414. var banana = ["Banana", 5]
  415. # "Banana" is inserted after "Kiwi".
  416. my_items.insert(my_items.bsearch_custom(banana, sort_by_amount, false), banana)
  417. # Prints [["Tomato", 2], ["Apple", 5], ["Kiwi", 5], ["Banana", 5], ["Rice", 9]]
  418. print(my_items)
  419. \ **Note:** Calling :ref:`bsearch_custom()<class_Array_method_bsearch_custom>` on an *unsorted* array will result in unexpected behavior. Use :ref:`sort_custom()<class_Array_method_sort_custom>` with ``func`` before calling this method.
  420. .. rst-class:: classref-item-separator
  421. ----
  422. .. _class_Array_method_clear:
  423. .. rst-class:: classref-method
  424. |void| **clear**\ (\ ) :ref:`๐Ÿ”—<class_Array_method_clear>`
  425. Removes all elements from the array. This is equivalent to using :ref:`resize()<class_Array_method_resize>` with a size of ``0``.
  426. .. rst-class:: classref-item-separator
  427. ----
  428. .. _class_Array_method_count:
  429. .. rst-class:: classref-method
  430. :ref:`int<class_int>` **count**\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_count>`
  431. Returns the number of times an element is in the array.
  432. To count how many elements in an array satisfy a condition, see :ref:`reduce()<class_Array_method_reduce>`.
  433. .. rst-class:: classref-item-separator
  434. ----
  435. .. _class_Array_method_duplicate:
  436. .. rst-class:: classref-method
  437. :ref:`Array<class_Array>` **duplicate**\ (\ deep\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`๐Ÿ”—<class_Array_method_duplicate>`
  438. Returns a new copy of the array.
  439. By default, a **shallow** copy is returned: all nested **Array**, :ref:`Dictionary<class_Dictionary>`, and :ref:`Resource<class_Resource>` elements are shared with the original array. Modifying any of those in one array will also affect them in the other.
  440. If ``deep`` is ``true``, a **deep** copy is returned: all nested arrays and dictionaries are also duplicated (recursively). Any :ref:`Resource<class_Resource>` is still shared with the original array, though.
  441. .. rst-class:: classref-item-separator
  442. ----
  443. .. _class_Array_method_duplicate_deep:
  444. .. rst-class:: classref-method
  445. :ref:`Array<class_Array>` **duplicate_deep**\ (\ deep_subresources_mode\: :ref:`int<class_int>` = 1\ ) |const| :ref:`๐Ÿ”—<class_Array_method_duplicate_deep>`
  446. Duplicates this array, deeply, like :ref:`duplicate()<class_Array_method_duplicate>`\ ``(true)``, with extra control over how subresources are handled.
  447. \ ``deep_subresources_mode`` must be one of the values from :ref:`DeepDuplicateMode<enum_Resource_DeepDuplicateMode>`. By default, only internal resources will be duplicated (recursively).
  448. .. rst-class:: classref-item-separator
  449. ----
  450. .. _class_Array_method_erase:
  451. .. rst-class:: classref-method
  452. |void| **erase**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`๐Ÿ”—<class_Array_method_erase>`
  453. Finds and removes the first occurrence of ``value`` from the array. If ``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.
  454. \ **Note:** This method shifts every element's index after the removed ``value`` back, which may have a noticeable performance cost, especially on larger arrays.
  455. \ **Note:** Erasing elements while iterating over arrays is **not** supported and will result in unpredictable behavior.
  456. .. rst-class:: classref-item-separator
  457. ----
  458. .. _class_Array_method_fill:
  459. .. rst-class:: classref-method
  460. |void| **fill**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`๐Ÿ”—<class_Array_method_fill>`
  461. Assigns the given ``value`` to all elements in the array.
  462. This method can often be combined with :ref:`resize()<class_Array_method_resize>` to create an array with a given size and initialized elements:
  463. .. tabs::
  464. .. code-tab:: gdscript
  465. var array = []
  466. array.resize(5)
  467. array.fill(2)
  468. print(array) # Prints [2, 2, 2, 2, 2]
  469. .. code-tab:: csharp
  470. Godot.Collections.Array array = [];
  471. array.Resize(5);
  472. array.Fill(2);
  473. GD.Print(array); // Prints [2, 2, 2, 2, 2]
  474. \ **Note:** If ``value`` is a :ref:`Variant<class_Variant>` passed by reference (:ref:`Object<class_Object>`-derived, **Array**, :ref:`Dictionary<class_Dictionary>`, etc.), the array will be filled with references to the same ``value``, which are not duplicates.
  475. .. rst-class:: classref-item-separator
  476. ----
  477. .. _class_Array_method_filter:
  478. .. rst-class:: classref-method
  479. :ref:`Array<class_Array>` **filter**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_filter>`
  480. Calls the given :ref:`Callable<class_Callable>` on each element in the array and returns a new, filtered **Array**.
  481. The ``method`` receives one of the array elements as an argument, and should return ``true`` to add the element to the filtered array, or ``false`` to exclude it.
  482. ::
  483. func is_even(number):
  484. return number % 2 == 0
  485. func _ready():
  486. print([1, 4, 5, 8].filter(is_even)) # Prints [4, 8]
  487. # Same as above, but using a lambda function.
  488. print([1, 4, 5, 8].filter(func(number): return number % 2 == 0))
  489. 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>`.
  490. .. rst-class:: classref-item-separator
  491. ----
  492. .. _class_Array_method_find:
  493. .. rst-class:: classref-method
  494. :ref:`int<class_int>` **find**\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = 0\ ) |const| :ref:`๐Ÿ”—<class_Array_method_find>`
  495. Returns the index of the **first** occurrence of ``what`` in this array, or ``-1`` if there are none. The search's start can be specified with ``from``, continuing to the end of the array.
  496. \ **Note:** If you just want to know whether the array contains ``what``, use :ref:`has()<class_Array_method_has>` (``Contains`` in C#). In GDScript, you may also use the ``in`` operator.
  497. \ **Note:** For performance reasons, the search is affected by ``what``'s :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>`. For example, ``7`` (:ref:`int<class_int>`) and ``7.0`` (:ref:`float<class_float>`) are not considered equal for this method.
  498. .. rst-class:: classref-item-separator
  499. ----
  500. .. _class_Array_method_find_custom:
  501. .. rst-class:: classref-method
  502. :ref:`int<class_int>` **find_custom**\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = 0\ ) |const| :ref:`๐Ÿ”—<class_Array_method_find_custom>`
  503. Returns the index of the **first** element in the array that causes ``method`` to return ``true``, or ``-1`` if there are none. The search's start can be specified with ``from``, continuing to the end of the array.
  504. \ ``method`` is a callable that takes an element of the array, and returns a :ref:`bool<class_bool>`.
  505. \ **Note:** If you just want to know whether the array contains *anything* that satisfies ``method``, use :ref:`any()<class_Array_method_any>`.
  506. .. tabs::
  507. .. code-tab:: gdscript
  508. func is_even(number):
  509. return number % 2 == 0
  510. func _ready():
  511. print([1, 3, 4, 7].find_custom(is_even.bind())) # Prints 2
  512. .. rst-class:: classref-item-separator
  513. ----
  514. .. _class_Array_method_front:
  515. .. rst-class:: classref-method
  516. :ref:`Variant<class_Variant>` **front**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_front>`
  517. Returns the first element of the array. If the array is empty, fails and returns ``null``. See also :ref:`back()<class_Array_method_back>`.
  518. \ **Note:** Unlike with the ``[]`` operator (``array[0]``), an error is generated without stopping project execution.
  519. .. rst-class:: classref-item-separator
  520. ----
  521. .. _class_Array_method_get:
  522. .. rst-class:: classref-method
  523. :ref:`Variant<class_Variant>` **get**\ (\ index\: :ref:`int<class_int>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_get>`
  524. Returns the element at the given ``index`` in the array. If ``index`` out-of-bounds or negative, this method fails and returns ``null``.
  525. This method is similar (but not identical) to the ``[]`` operator. Most notably, when this method fails, it doesn't pause project execution if run from the editor.
  526. .. rst-class:: classref-item-separator
  527. ----
  528. .. _class_Array_method_get_typed_builtin:
  529. .. rst-class:: classref-method
  530. :ref:`int<class_int>` **get_typed_builtin**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_get_typed_builtin>`
  531. Returns the built-in :ref:`Variant<class_Variant>` type of the typed array as a :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>` constant. If the array is not typed, returns :ref:`@GlobalScope.TYPE_NIL<class_@GlobalScope_constant_TYPE_NIL>`. See also :ref:`is_typed()<class_Array_method_is_typed>`.
  532. .. rst-class:: classref-item-separator
  533. ----
  534. .. _class_Array_method_get_typed_class_name:
  535. .. rst-class:: classref-method
  536. :ref:`StringName<class_StringName>` **get_typed_class_name**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_get_typed_class_name>`
  537. Returns the **built-in** class name of the typed array, if the built-in :ref:`Variant<class_Variant>` type :ref:`@GlobalScope.TYPE_OBJECT<class_@GlobalScope_constant_TYPE_OBJECT>`. Otherwise, returns an empty :ref:`StringName<class_StringName>`. See also :ref:`is_typed()<class_Array_method_is_typed>` and :ref:`Object.get_class()<class_Object_method_get_class>`.
  538. .. rst-class:: classref-item-separator
  539. ----
  540. .. _class_Array_method_get_typed_script:
  541. .. rst-class:: classref-method
  542. :ref:`Variant<class_Variant>` **get_typed_script**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_get_typed_script>`
  543. Returns the :ref:`Script<class_Script>` instance associated with this typed array, or ``null`` if it does not exist. See also :ref:`is_typed()<class_Array_method_is_typed>`.
  544. .. rst-class:: classref-item-separator
  545. ----
  546. .. _class_Array_method_has:
  547. .. rst-class:: classref-method
  548. :ref:`bool<class_bool>` **has**\ (\ value\: :ref:`Variant<class_Variant>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_has>`
  549. Returns ``true`` if the array contains the given ``value``.
  550. .. tabs::
  551. .. code-tab:: gdscript
  552. print(["inside", 7].has("inside")) # Prints true
  553. print(["inside", 7].has("outside")) # Prints false
  554. print(["inside", 7].has(7)) # Prints true
  555. print(["inside", 7].has("7")) # Prints false
  556. .. code-tab:: csharp
  557. Godot.Collections.Array arr = ["inside", 7];
  558. // By C# convention, this method is renamed to `Contains`.
  559. GD.Print(arr.Contains("inside")); // Prints True
  560. GD.Print(arr.Contains("outside")); // Prints False
  561. GD.Print(arr.Contains(7)); // Prints True
  562. GD.Print(arr.Contains("7")); // Prints False
  563. In GDScript, this is equivalent to the ``in`` operator:
  564. ::
  565. if 4 in [2, 4, 6, 8]:
  566. print("4 is here!") # Will be printed.
  567. \ **Note:** For performance reasons, the search is affected by the ``value``'s :ref:`Variant.Type<enum_@GlobalScope_Variant.Type>`. For example, ``7`` (:ref:`int<class_int>`) and ``7.0`` (:ref:`float<class_float>`) are not considered equal for this method.
  568. .. rst-class:: classref-item-separator
  569. ----
  570. .. _class_Array_method_hash:
  571. .. rst-class:: classref-method
  572. :ref:`int<class_int>` **hash**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_hash>`
  573. Returns a hashed 32-bit integer value representing the array and its contents.
  574. \ **Note:** Arrays with equal hash values are *not* guaranteed to be the same, as a result of hash collisions. On the countrary, arrays with different hash values are guaranteed to be different.
  575. .. rst-class:: classref-item-separator
  576. ----
  577. .. _class_Array_method_insert:
  578. .. rst-class:: classref-method
  579. :ref:`int<class_int>` **insert**\ (\ position\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`๐Ÿ”—<class_Array_method_insert>`
  580. Inserts a new element (``value``) at a given index (``position``) in the array. ``position`` should be between ``0`` and the array's :ref:`size()<class_Array_method_size>`. If negative, ``position`` is considered relative to the end of the array.
  581. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the other :ref:`Error<enum_@GlobalScope_Error>` constants if this method fails.
  582. \ **Note:** Every element's index after ``position`` needs to be shifted forward, which may have a noticeable performance cost, especially on larger arrays.
  583. .. rst-class:: classref-item-separator
  584. ----
  585. .. _class_Array_method_is_empty:
  586. .. rst-class:: classref-method
  587. :ref:`bool<class_bool>` **is_empty**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_is_empty>`
  588. Returns ``true`` if the array is empty (``[]``). See also :ref:`size()<class_Array_method_size>`.
  589. .. rst-class:: classref-item-separator
  590. ----
  591. .. _class_Array_method_is_read_only:
  592. .. rst-class:: classref-method
  593. :ref:`bool<class_bool>` **is_read_only**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_is_read_only>`
  594. Returns ``true`` if the array is read-only. See :ref:`make_read_only()<class_Array_method_make_read_only>`.
  595. In GDScript, arrays are automatically read-only if declared with the ``const`` keyword.
  596. .. rst-class:: classref-item-separator
  597. ----
  598. .. _class_Array_method_is_same_typed:
  599. .. rst-class:: classref-method
  600. :ref:`bool<class_bool>` **is_same_typed**\ (\ array\: :ref:`Array<class_Array>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_is_same_typed>`
  601. Returns ``true`` if this array is typed the same as the given ``array``. See also :ref:`is_typed()<class_Array_method_is_typed>`.
  602. .. rst-class:: classref-item-separator
  603. ----
  604. .. _class_Array_method_is_typed:
  605. .. rst-class:: classref-method
  606. :ref:`bool<class_bool>` **is_typed**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_is_typed>`
  607. Returns ``true`` if the array is typed. Typed arrays can only contain elements of a specific type, as defined by the typed array constructor. The methods of a typed array are still expected to return a generic :ref:`Variant<class_Variant>`.
  608. In GDScript, it is possible to define a typed array with static typing:
  609. ::
  610. var numbers: Array[float] = [0.2, 4.2, -2.0]
  611. print(numbers.is_typed()) # Prints true
  612. .. rst-class:: classref-item-separator
  613. ----
  614. .. _class_Array_method_make_read_only:
  615. .. rst-class:: classref-method
  616. |void| **make_read_only**\ (\ ) :ref:`๐Ÿ”—<class_Array_method_make_read_only>`
  617. Makes the array read-only. The array's elements cannot be overridden with different values, and their order cannot change. Does not apply to nested elements, such as dictionaries.
  618. In GDScript, arrays are automatically read-only if declared with the ``const`` keyword.
  619. .. rst-class:: classref-item-separator
  620. ----
  621. .. _class_Array_method_map:
  622. .. rst-class:: classref-method
  623. :ref:`Array<class_Array>` **map**\ (\ method\: :ref:`Callable<class_Callable>`\ ) |const| :ref:`๐Ÿ”—<class_Array_method_map>`
  624. Calls the given :ref:`Callable<class_Callable>` for each element in the array and returns a new array filled with values returned by the ``method``.
  625. The ``method`` should take one :ref:`Variant<class_Variant>` parameter (the current array element) and can return any :ref:`Variant<class_Variant>`.
  626. ::
  627. func double(number):
  628. return number * 2
  629. func _ready():
  630. print([1, 2, 3].map(double)) # Prints [2, 4, 6]
  631. # Same as above, but using a lambda function.
  632. print([1, 2, 3].map(func(element): return element * 2))
  633. 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>`.
  634. .. rst-class:: classref-item-separator
  635. ----
  636. .. _class_Array_method_max:
  637. .. rst-class:: classref-method
  638. :ref:`Variant<class_Variant>` **max**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_max>`
  639. Returns the maximum value contained in the array, if all elements can be compared. Otherwise, returns ``null``. See also :ref:`min()<class_Array_method_min>`.
  640. To find the maximum value using a custom comparator, you can use :ref:`reduce()<class_Array_method_reduce>`.
  641. .. rst-class:: classref-item-separator
  642. ----
  643. .. _class_Array_method_min:
  644. .. rst-class:: classref-method
  645. :ref:`Variant<class_Variant>` **min**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_min>`
  646. Returns the minimum value contained in the array, if all elements can be compared. Otherwise, returns ``null``. See also :ref:`max()<class_Array_method_max>`.
  647. .. rst-class:: classref-item-separator
  648. ----
  649. .. _class_Array_method_pick_random:
  650. .. rst-class:: classref-method
  651. :ref:`Variant<class_Variant>` **pick_random**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_pick_random>`
  652. Returns a random element from the array. Generates an error and returns ``null`` if the array is empty.
  653. .. tabs::
  654. .. code-tab:: gdscript
  655. # May print 1, 2, 3.25, or "Hi".
  656. print([1, 2, 3.25, "Hi"].pick_random())
  657. .. code-tab:: csharp
  658. Godot.Collections.Array array = [1, 2, 3.25f, "Hi"];
  659. GD.Print(array.PickRandom()); // May print 1, 2, 3.25, or "Hi".
  660. \ **Note:** Like many similar functions in the engine (such as :ref:`@GlobalScope.randi()<class_@GlobalScope_method_randi>` or :ref:`shuffle()<class_Array_method_shuffle>`), this method uses a common, global random seed. To get a predictable outcome from this method, see :ref:`@GlobalScope.seed()<class_@GlobalScope_method_seed>`.
  661. .. rst-class:: classref-item-separator
  662. ----
  663. .. _class_Array_method_pop_at:
  664. .. rst-class:: classref-method
  665. :ref:`Variant<class_Variant>` **pop_at**\ (\ position\: :ref:`int<class_int>`\ ) :ref:`๐Ÿ”—<class_Array_method_pop_at>`
  666. Removes and returns the element of the array at index ``position``. If negative, ``position`` is considered relative to the end of the array. Returns ``null`` if the array is empty. If ``position`` is out of bounds, an error message is also generated.
  667. \ **Note:** This method shifts every element's index after ``position`` back, which may have a noticeable performance cost, especially on larger arrays.
  668. .. rst-class:: classref-item-separator
  669. ----
  670. .. _class_Array_method_pop_back:
  671. .. rst-class:: classref-method
  672. :ref:`Variant<class_Variant>` **pop_back**\ (\ ) :ref:`๐Ÿ”—<class_Array_method_pop_back>`
  673. Removes and returns the last element of the array. Returns ``null`` if the array is empty, without generating an error. See also :ref:`pop_front()<class_Array_method_pop_front>`.
  674. .. rst-class:: classref-item-separator
  675. ----
  676. .. _class_Array_method_pop_front:
  677. .. rst-class:: classref-method
  678. :ref:`Variant<class_Variant>` **pop_front**\ (\ ) :ref:`๐Ÿ”—<class_Array_method_pop_front>`
  679. Removes and returns the first element of the array. Returns ``null`` if the array is empty, without generating an error. See also :ref:`pop_back()<class_Array_method_pop_back>`.
  680. \ **Note:** This method shifts every other element's index back, which may have a noticeable performance cost, especially on larger arrays.
  681. .. rst-class:: classref-item-separator
  682. ----
  683. .. _class_Array_method_push_back:
  684. .. rst-class:: classref-method
  685. |void| **push_back**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`๐Ÿ”—<class_Array_method_push_back>`
  686. Appends an element at the end of the array. See also :ref:`push_front()<class_Array_method_push_front>`.
  687. .. rst-class:: classref-item-separator
  688. ----
  689. .. _class_Array_method_push_front:
  690. .. rst-class:: classref-method
  691. |void| **push_front**\ (\ value\: :ref:`Variant<class_Variant>`\ ) :ref:`๐Ÿ”—<class_Array_method_push_front>`
  692. Adds an element at the beginning of the array. See also :ref:`push_back()<class_Array_method_push_back>`.
  693. \ **Note:** This method shifts every other element's index forward, which may have a noticeable performance cost, especially on larger arrays.
  694. .. rst-class:: classref-item-separator
  695. ----
  696. .. _class_Array_method_reduce:
  697. .. rst-class:: classref-method
  698. :ref:`Variant<class_Variant>` **reduce**\ (\ method\: :ref:`Callable<class_Callable>`, accum\: :ref:`Variant<class_Variant>` = null\ ) |const| :ref:`๐Ÿ”—<class_Array_method_reduce>`
  699. Calls the given :ref:`Callable<class_Callable>` for each element in array, accumulates the result in ``accum``, then returns it.
  700. The ``method`` takes two arguments: the current value of ``accum`` and the current array element. If ``accum`` is ``null`` (as by default), the iteration will start from the second element, with the first one used as initial value of ``accum``.
  701. ::
  702. func sum(accum, number):
  703. return accum + number
  704. func _ready():
  705. print([1, 2, 3].reduce(sum, 0)) # Prints 6
  706. print([1, 2, 3].reduce(sum, 10)) # Prints 16
  707. # Same as above, but using a lambda function.
  708. print([1, 2, 3].reduce(func(accum, number): return accum + number, 10))
  709. If :ref:`max()<class_Array_method_max>` is not desirable, this method may also be used to implement a custom comparator:
  710. ::
  711. func _ready():
  712. var arr = [Vector2i(5, 0), Vector2i(3, 4), Vector2i(1, 2)]
  713. var longest_vec = arr.reduce(func(max, vec): return vec if is_length_greater(vec, max) else max)
  714. print(longest_vec) # Prints (3, 4)
  715. func is_length_greater(a, b):
  716. return a.length() > b.length()
  717. This method can also be used to count how many elements in an array satisfy a certain condition, similar to :ref:`count()<class_Array_method_count>`:
  718. ::
  719. func is_even(number):
  720. return number % 2 == 0
  721. func _ready():
  722. var arr = [1, 2, 3, 4, 5]
  723. # If the current element is even, increment count, otherwise leave count the same.
  724. var even_count = arr.reduce(func(count, next): return count + 1 if is_even(next) else count, 0)
  725. print(even_count) # Prints 2
  726. 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>`.
  727. .. rst-class:: classref-item-separator
  728. ----
  729. .. _class_Array_method_remove_at:
  730. .. rst-class:: classref-method
  731. |void| **remove_at**\ (\ position\: :ref:`int<class_int>`\ ) :ref:`๐Ÿ”—<class_Array_method_remove_at>`
  732. Removes the element from the array at the given index (``position``). If the index is out of bounds, this method fails. If the index is negative, ``position`` is considered relative to the end of the array.
  733. If you need to return the removed element, use :ref:`pop_at()<class_Array_method_pop_at>`. To remove an element by value, use :ref:`erase()<class_Array_method_erase>` instead.
  734. \ **Note:** This method shifts every element's index after ``position`` back, which may have a noticeable performance cost, especially on larger arrays.
  735. \ **Note:** The ``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, use ``arr.resize(arr.size() - 1)``.
  736. .. rst-class:: classref-item-separator
  737. ----
  738. .. _class_Array_method_resize:
  739. .. rst-class:: classref-method
  740. :ref:`int<class_int>` **resize**\ (\ size\: :ref:`int<class_int>`\ ) :ref:`๐Ÿ”—<class_Array_method_resize>`
  741. Sets the array's number of elements to ``size``. If ``size`` is smaller than the array's current size, the elements at the end are removed. If ``size`` is greater, new default elements (usually ``null``) are added, depending on the array's type.
  742. Returns :ref:`@GlobalScope.OK<class_@GlobalScope_constant_OK>` on success, or one of the following :ref:`Error<enum_@GlobalScope_Error>` constants if this method fails: :ref:`@GlobalScope.ERR_LOCKED<class_@GlobalScope_constant_ERR_LOCKED>` if the array is read-only, :ref:`@GlobalScope.ERR_INVALID_PARAMETER<class_@GlobalScope_constant_ERR_INVALID_PARAMETER>` if the size is negative, or :ref:`@GlobalScope.ERR_OUT_OF_MEMORY<class_@GlobalScope_constant_ERR_OUT_OF_MEMORY>` if allocations fail. Use :ref:`size()<class_Array_method_size>` to find the actual size of the array after resize.
  743. \ **Note:** Calling this method once and assigning the new values is faster than calling :ref:`append()<class_Array_method_append>` for every new element.
  744. .. rst-class:: classref-item-separator
  745. ----
  746. .. _class_Array_method_reverse:
  747. .. rst-class:: classref-method
  748. |void| **reverse**\ (\ ) :ref:`๐Ÿ”—<class_Array_method_reverse>`
  749. Reverses the order of all elements in the array.
  750. .. rst-class:: classref-item-separator
  751. ----
  752. .. _class_Array_method_rfind:
  753. .. rst-class:: classref-method
  754. :ref:`int<class_int>` **rfind**\ (\ what\: :ref:`Variant<class_Variant>`, from\: :ref:`int<class_int>` = -1\ ) |const| :ref:`๐Ÿ”—<class_Array_method_rfind>`
  755. Returns the index of the **last** occurrence of ``what`` in this array, or ``-1`` if there are none. The search's start can be specified with ``from``, continuing to the beginning of the array. This method is the reverse of :ref:`find()<class_Array_method_find>`.
  756. .. rst-class:: classref-item-separator
  757. ----
  758. .. _class_Array_method_rfind_custom:
  759. .. rst-class:: classref-method
  760. :ref:`int<class_int>` **rfind_custom**\ (\ method\: :ref:`Callable<class_Callable>`, from\: :ref:`int<class_int>` = -1\ ) |const| :ref:`๐Ÿ”—<class_Array_method_rfind_custom>`
  761. Returns the index of the **last** element of the array that causes ``method`` to return ``true``, or ``-1`` if there are none. The search's start can be specified with ``from``, continuing to the beginning of the array. This method is the reverse of :ref:`find_custom()<class_Array_method_find_custom>`.
  762. .. rst-class:: classref-item-separator
  763. ----
  764. .. _class_Array_method_set:
  765. .. rst-class:: classref-method
  766. |void| **set**\ (\ index\: :ref:`int<class_int>`, value\: :ref:`Variant<class_Variant>`\ ) :ref:`๐Ÿ”—<class_Array_method_set>`
  767. Sets the value of the element at the given ``index`` to the given ``value``. This will not change the size of the array, it only changes the value at an index already in the array. This is the same as using the ``[]`` operator (``array[index] = value``).
  768. .. rst-class:: classref-item-separator
  769. ----
  770. .. _class_Array_method_shuffle:
  771. .. rst-class:: classref-method
  772. |void| **shuffle**\ (\ ) :ref:`๐Ÿ”—<class_Array_method_shuffle>`
  773. Shuffles all elements of the array in a random order.
  774. \ **Note:** Like many similar functions in the engine (such as :ref:`@GlobalScope.randi()<class_@GlobalScope_method_randi>` or :ref:`pick_random()<class_Array_method_pick_random>`), this method uses a common, global random seed. To get a predictable outcome from this method, see :ref:`@GlobalScope.seed()<class_@GlobalScope_method_seed>`.
  775. .. rst-class:: classref-item-separator
  776. ----
  777. .. _class_Array_method_size:
  778. .. rst-class:: classref-method
  779. :ref:`int<class_int>` **size**\ (\ ) |const| :ref:`๐Ÿ”—<class_Array_method_size>`
  780. Returns the number of elements in the array. Empty arrays (``[]``) always return ``0``. See also :ref:`is_empty()<class_Array_method_is_empty>`.
  781. .. rst-class:: classref-item-separator
  782. ----
  783. .. _class_Array_method_slice:
  784. .. rst-class:: classref-method
  785. :ref:`Array<class_Array>` **slice**\ (\ begin\: :ref:`int<class_int>`, end\: :ref:`int<class_int>` = 2147483647, step\: :ref:`int<class_int>` = 1, deep\: :ref:`bool<class_bool>` = false\ ) |const| :ref:`๐Ÿ”—<class_Array_method_slice>`
  786. Returns a new **Array** containing this array's elements, from index ``begin`` (inclusive) to ``end`` (exclusive), every ``step`` elements.
  787. If either ``begin`` or ``end`` are negative, their value is relative to the end of the array.
  788. If ``step`` is negative, this method iterates through the array in reverse, returning a slice ordered backwards. For this to work, ``begin`` must be greater than ``end``.
  789. If ``deep`` is ``true``, all nested **Array** and :ref:`Dictionary<class_Dictionary>` elements in the slice are duplicated from the original, recursively. See also :ref:`duplicate()<class_Array_method_duplicate>`.
  790. ::
  791. var letters = ["A", "B", "C", "D", "E", "F"]
  792. print(letters.slice(0, 2)) # Prints ["A", "B"]
  793. print(letters.slice(2, -2)) # Prints ["C", "D"]
  794. print(letters.slice(-2, 6)) # Prints ["E", "F"]
  795. print(letters.slice(0, 6, 2)) # Prints ["A", "C", "E"]
  796. print(letters.slice(4, 1, -1)) # Prints ["E", "D", "C"]
  797. .. rst-class:: classref-item-separator
  798. ----
  799. .. _class_Array_method_sort:
  800. .. rst-class:: classref-method
  801. |void| **sort**\ (\ ) :ref:`๐Ÿ”—<class_Array_method_sort>`
  802. Sorts the array in ascending order. The final order is dependent on the "less than" (``<``) comparison between elements.
  803. .. tabs::
  804. .. code-tab:: gdscript
  805. var numbers = [10, 5, 2.5, 8]
  806. numbers.sort()
  807. print(numbers) # Prints [2.5, 5, 8, 10]
  808. .. code-tab:: csharp
  809. Godot.Collections.Array numbers = [10, 5, 2.5, 8];
  810. numbers.Sort();
  811. GD.Print(numbers); // Prints [2.5, 5, 8, 10]
  812. \ **Note:** The sorting algorithm used is not `stable <https://en.wikipedia.org/wiki/Sorting_algorithm#Stability>`__. This means that equivalent elements (such as ``2`` and ``2.0``) may have their order changed when calling :ref:`sort()<class_Array_method_sort>`.
  813. .. rst-class:: classref-item-separator
  814. ----
  815. .. _class_Array_method_sort_custom:
  816. .. rst-class:: classref-method
  817. |void| **sort_custom**\ (\ func\: :ref:`Callable<class_Callable>`\ ) :ref:`๐Ÿ”—<class_Array_method_sort_custom>`
  818. Sorts the array using a custom :ref:`Callable<class_Callable>`.
  819. \ ``func`` is called as many times as necessary, receiving two array elements as arguments. The function should return ``true`` if the first element should be moved *before* the second one, otherwise it should return ``false``.
  820. ::
  821. func sort_ascending(a, b):
  822. if a[1] < b[1]:
  823. return true
  824. return false
  825. func _ready():
  826. var my_items = [["Tomato", 5], ["Apple", 9], ["Rice", 4]]
  827. my_items.sort_custom(sort_ascending)
  828. print(my_items) # Prints [["Rice", 4], ["Tomato", 5], ["Apple", 9]]
  829. # Sort descending, using a lambda function.
  830. my_items.sort_custom(func(a, b): return a[1] > b[1])
  831. print(my_items) # Prints [["Apple", 9], ["Tomato", 5], ["Rice", 4]]
  832. It may also be necessary to use this method to sort strings by natural order, with :ref:`String.naturalnocasecmp_to()<class_String_method_naturalnocasecmp_to>`, as in the following example:
  833. ::
  834. var files = ["newfile1", "newfile2", "newfile10", "newfile11"]
  835. files.sort_custom(func(a, b): return a.naturalnocasecmp_to(b) < 0)
  836. print(files) # Prints ["newfile1", "newfile2", "newfile10", "newfile11"]
  837. \ **Note:** In C#, this method is not supported.
  838. \ **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 calling this method.
  839. \ **Note:** You should not randomize the return value of ``func``, as the heapsort algorithm expects a consistent result. Randomizing the return value will result in unexpected behavior.
  840. .. rst-class:: classref-section-separator
  841. ----
  842. .. rst-class:: classref-descriptions-group
  843. Operator Descriptions
  844. ---------------------
  845. .. _class_Array_operator_neq_Array:
  846. .. rst-class:: classref-operator
  847. :ref:`bool<class_bool>` **operator !=**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_operator_neq_Array>`
  848. Returns ``true`` if the array's size or its elements are different than ``right``'s.
  849. .. rst-class:: classref-item-separator
  850. ----
  851. .. _class_Array_operator_sum_Array:
  852. .. rst-class:: classref-operator
  853. :ref:`Array<class_Array>` **operator +**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_operator_sum_Array>`
  854. Appends the ``right`` array to the left operand, creating a new **Array**. This is also known as an array concatenation.
  855. .. tabs::
  856. .. code-tab:: gdscript
  857. var array1 = ["One", 2]
  858. var array2 = [3, "Four"]
  859. print(array1 + array2) # Prints ["One", 2, 3, "Four"]
  860. .. code-tab:: csharp
  861. // Note that concatenation is not possible with C#'s native Array type.
  862. Godot.Collections.Array array1 = ["One", 2];
  863. Godot.Collections.Array array2 = [3, "Four"];
  864. GD.Print(array1 + array2); // Prints ["One", 2, 3, "Four"]
  865. \ **Note:** For existing arrays, :ref:`append_array()<class_Array_method_append_array>` is much more efficient than concatenation and assignment with the ``+=`` operator.
  866. .. rst-class:: classref-item-separator
  867. ----
  868. .. _class_Array_operator_lt_Array:
  869. .. rst-class:: classref-operator
  870. :ref:`bool<class_bool>` **operator <**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_operator_lt_Array>`
  871. Compares the elements of both arrays in order, starting from index ``0`` and ending on the last index in common between both arrays. For each pair of elements, returns ``true`` if this array's element is less than ``right``'s, ``false`` if this element is greater. Otherwise, continues to the next pair.
  872. If all searched elements are equal, returns ``true`` if this array's size is less than ``right``'s, otherwise returns ``false``.
  873. .. rst-class:: classref-item-separator
  874. ----
  875. .. _class_Array_operator_lte_Array:
  876. .. rst-class:: classref-operator
  877. :ref:`bool<class_bool>` **operator <=**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_operator_lte_Array>`
  878. Compares the elements of both arrays in order, starting from index ``0`` and ending on the last index in common between both arrays. For each pair of elements, returns ``true`` if this array's element is less than ``right``'s, ``false`` if this element is greater. Otherwise, continues to the next pair.
  879. If all searched elements are equal, returns ``true`` if this array's size is less or equal to ``right``'s, otherwise returns ``false``.
  880. .. rst-class:: classref-item-separator
  881. ----
  882. .. _class_Array_operator_eq_Array:
  883. .. rst-class:: classref-operator
  884. :ref:`bool<class_bool>` **operator ==**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_operator_eq_Array>`
  885. Compares the left operand **Array** against the ``right`` **Array**. Returns ``true`` if the sizes and contents of the arrays are equal, ``false`` otherwise.
  886. .. rst-class:: classref-item-separator
  887. ----
  888. .. _class_Array_operator_gt_Array:
  889. .. rst-class:: classref-operator
  890. :ref:`bool<class_bool>` **operator >**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_operator_gt_Array>`
  891. Compares the elements of both arrays in order, starting from index ``0`` and ending on the last index in common between both arrays. For each pair of elements, returns ``true`` if this array's element is greater than ``right``'s, ``false`` if this element is less. Otherwise, continues to the next pair.
  892. If all searched elements are equal, returns ``true`` if this array's size is greater than ``right``'s, otherwise returns ``false``.
  893. .. rst-class:: classref-item-separator
  894. ----
  895. .. _class_Array_operator_gte_Array:
  896. .. rst-class:: classref-operator
  897. :ref:`bool<class_bool>` **operator >=**\ (\ right\: :ref:`Array<class_Array>`\ ) :ref:`๐Ÿ”—<class_Array_operator_gte_Array>`
  898. Compares the elements of both arrays in order, starting from index ``0`` and ending on the last index in common between both arrays. For each pair of elements, returns ``true`` if this array's element is greater than ``right``'s, ``false`` if this element is less. Otherwise, continues to the next pair.
  899. If all searched elements are equal, returns ``true`` if this array's size is greater or equal to ``right``'s, otherwise returns ``false``.
  900. .. rst-class:: classref-item-separator
  901. ----
  902. .. _class_Array_operator_idx_int:
  903. .. rst-class:: classref-operator
  904. :ref:`Variant<class_Variant>` **operator []**\ (\ index\: :ref:`int<class_int>`\ ) :ref:`๐Ÿ”—<class_Array_operator_idx_int>`
  905. Returns the :ref:`Variant<class_Variant>` element at the specified ``index``. Arrays start at index 0. If ``index`` is greater or equal to ``0``, the element is fetched starting from the beginning of the array. If ``index`` is a negative value, the element is fetched starting from the end. Accessing an array out-of-bounds will cause a run-time error, pausing the project execution if run from the editor.
  906. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  907. .. |required| replace:: :abbr:`required (This method is required to be overridden when extending its base class.)`
  908. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  909. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  910. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  911. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  912. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`
  913. .. |bitfield| replace:: :abbr:`BitField (This value is an integer composed as a bitmask of the following flags.)`
  914. .. |void| replace:: :abbr:`void (No return value.)`