class_array.rst 85 KB

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