class_array.rst 75 KB

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