class_array.rst 83 KB

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