Array.xml 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="Array" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
  3. <brief_description>
  4. A built-in data structure that holds a sequence of elements.
  5. </brief_description>
  6. <description>
  7. An array data structure that can contain a sequence of elements of any [Variant] type. Elements are accessed by a numerical index starting at [code]0[/code]. Negative indices are used to count from the back ([code]-1[/code] is the last element, [code]-2[/code] is the second to last, etc.).
  8. [codeblocks]
  9. [gdscript]
  10. var array = ["First", 2, 3, "Last"]
  11. print(array[0]) # Prints "First"
  12. print(array[2]) # Prints 3
  13. print(array[-1]) # Prints "Last"
  14. array[1] = "Second"
  15. print(array[1]) # Prints "Second"
  16. print(array[-3]) # Prints "Second"
  17. [/gdscript]
  18. [csharp]
  19. Godot.Collections.Array array = ["First", 2, 3, "Last"];
  20. GD.Print(array[0]); // Prints "First"
  21. GD.Print(array[2]); // Prints 3
  22. GD.Print(array[^1]); // Prints "Last"
  23. array[1] = "Second";
  24. GD.Print(array[1]); // Prints "Second"
  25. GD.Print(array[^3]); // Prints "Second"
  26. [/csharp]
  27. [/codeblocks]
  28. [b]Note:[/b] Arrays are always passed by [b]reference[/b]. To get a copy of an array that can be modified independently of the original array, use [method duplicate].
  29. [b]Note:[/b] Erasing elements while iterating over arrays is [b]not[/b] supported and will result in unpredictable behavior.
  30. [b]Differences between packed arrays, typed arrays, and untyped arrays:[/b] Packed arrays are generally faster to iterate on and modify compared to a typed array of the same type (e.g. [PackedInt64Array] versus [code]Array[int][/code]). 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 [method Array.map]. Typed arrays are in turn faster to iterate on and modify than untyped arrays.
  31. </description>
  32. <tutorials>
  33. </tutorials>
  34. <constructors>
  35. <constructor name="Array">
  36. <return type="Array" />
  37. <description>
  38. Constructs an empty [Array].
  39. </description>
  40. </constructor>
  41. <constructor name="Array">
  42. <return type="Array" />
  43. <param index="0" name="base" type="Array" />
  44. <param index="1" name="type" type="int" />
  45. <param index="2" name="class_name" type="StringName" />
  46. <param index="3" name="script" type="Variant" />
  47. <description>
  48. Creates a typed array from the [param 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:
  49. - [param type] is the built-in [Variant] type, as one the [enum Variant.Type] constants.
  50. - [param class_name] is the built-in class name (see [method Object.get_class]).
  51. - [param script] is the associated script. It must be a [Script] instance or [code]null[/code].
  52. If [param type] is not [constant TYPE_OBJECT], [param class_name] must be an empty [StringName] and [param script] must be [code]null[/code].
  53. [codeblock]
  54. class_name Sword
  55. extends Node
  56. class Stats:
  57. pass
  58. func _ready():
  59. var a = Array([], TYPE_INT, "", null) # Array[int]
  60. var b = Array([], TYPE_OBJECT, "Node", null) # Array[Node]
  61. var c = Array([], TYPE_OBJECT, "Node", Sword) # Array[Sword]
  62. var d = Array([], TYPE_OBJECT, "RefCounted", Stats) # Array[Stats]
  63. [/codeblock]
  64. The [param base] array's elements are converted when necessary. If this is not possible or [param base] is already typed, this constructor fails and returns an empty [Array].
  65. In GDScript, this constructor is usually not necessary, as it is possible to create a typed array through static typing:
  66. [codeblock]
  67. var numbers: Array[float] = []
  68. var children: Array[Node] = [$Node, $Sprite2D, $RigidBody3D]
  69. var integers: Array[int] = [0.2, 4.5, -2.0]
  70. print(integers) # Prints [0, 4, -2]
  71. [/codeblock]
  72. </description>
  73. </constructor>
  74. <constructor name="Array">
  75. <return type="Array" />
  76. <param index="0" name="from" type="Array" />
  77. <description>
  78. Returns the same array as [param from]. If you need a copy of the array, use [method duplicate].
  79. </description>
  80. </constructor>
  81. <constructor name="Array">
  82. <return type="Array" />
  83. <param index="0" name="from" type="PackedByteArray" />
  84. <description>
  85. Constructs an array from a [PackedByteArray].
  86. </description>
  87. </constructor>
  88. <constructor name="Array">
  89. <return type="Array" />
  90. <param index="0" name="from" type="PackedColorArray" />
  91. <description>
  92. Constructs an array from a [PackedColorArray].
  93. </description>
  94. </constructor>
  95. <constructor name="Array">
  96. <return type="Array" />
  97. <param index="0" name="from" type="PackedFloat32Array" />
  98. <description>
  99. Constructs an array from a [PackedFloat32Array].
  100. </description>
  101. </constructor>
  102. <constructor name="Array">
  103. <return type="Array" />
  104. <param index="0" name="from" type="PackedFloat64Array" />
  105. <description>
  106. Constructs an array from a [PackedFloat64Array].
  107. </description>
  108. </constructor>
  109. <constructor name="Array">
  110. <return type="Array" />
  111. <param index="0" name="from" type="PackedInt32Array" />
  112. <description>
  113. Constructs an array from a [PackedInt32Array].
  114. </description>
  115. </constructor>
  116. <constructor name="Array">
  117. <return type="Array" />
  118. <param index="0" name="from" type="PackedInt64Array" />
  119. <description>
  120. Constructs an array from a [PackedInt64Array].
  121. </description>
  122. </constructor>
  123. <constructor name="Array">
  124. <return type="Array" />
  125. <param index="0" name="from" type="PackedStringArray" />
  126. <description>
  127. Constructs an array from a [PackedStringArray].
  128. </description>
  129. </constructor>
  130. <constructor name="Array">
  131. <return type="Array" />
  132. <param index="0" name="from" type="PackedVector2Array" />
  133. <description>
  134. Constructs an array from a [PackedVector2Array].
  135. </description>
  136. </constructor>
  137. <constructor name="Array">
  138. <return type="Array" />
  139. <param index="0" name="from" type="PackedVector3Array" />
  140. <description>
  141. Constructs an array from a [PackedVector3Array].
  142. </description>
  143. </constructor>
  144. <constructor name="Array">
  145. <return type="Array" />
  146. <param index="0" name="from" type="PackedVector4Array" />
  147. <description>
  148. Constructs an array from a [PackedVector4Array].
  149. </description>
  150. </constructor>
  151. </constructors>
  152. <methods>
  153. <method name="all" qualifiers="const">
  154. <return type="bool" />
  155. <param index="0" name="method" type="Callable" />
  156. <description>
  157. Calls the given [Callable] on each element in the array and returns [code]true[/code] if the [Callable] returns [code]true[/code] for [i]all[/i] elements in the array. If the [Callable] returns [code]false[/code] for one array element or more, this method returns [code]false[/code].
  158. The [param method] should take one [Variant] parameter (the current array element) and return a [bool].
  159. [codeblocks]
  160. [gdscript]
  161. func greater_than_5(number):
  162. return number &gt; 5
  163. func _ready():
  164. print([6, 10, 6].all(greater_than_5)) # Prints true (3/3 elements evaluate to true).
  165. print([4, 10, 4].all(greater_than_5)) # Prints false (1/3 elements evaluate to true).
  166. print([4, 4, 4].all(greater_than_5)) # Prints false (0/3 elements evaluate to true).
  167. print([].all(greater_than_5)) # Prints true (0/0 elements evaluate to true).
  168. # Same as the first line above, but using a lambda function.
  169. print([6, 10, 6].all(func(element): return element &gt; 5)) # Prints true
  170. [/gdscript]
  171. [csharp]
  172. private static bool GreaterThan5(int number)
  173. {
  174. return number &gt; 5;
  175. }
  176. public override void _Ready()
  177. {
  178. // Prints True (3/3 elements evaluate to true).
  179. GD.Print(new Godot.Collections.Array&gt;int&lt; { 6, 10, 6 }.All(GreaterThan5));
  180. // Prints False (1/3 elements evaluate to true).
  181. GD.Print(new Godot.Collections.Array&gt;int&lt; { 4, 10, 4 }.All(GreaterThan5));
  182. // Prints False (0/3 elements evaluate to true).
  183. GD.Print(new Godot.Collections.Array&gt;int&lt; { 4, 4, 4 }.All(GreaterThan5));
  184. // Prints True (0/0 elements evaluate to true).
  185. GD.Print(new Godot.Collections.Array&gt;int&lt; { }.All(GreaterThan5));
  186. // Same as the first line above, but using a lambda function.
  187. GD.Print(new Godot.Collections.Array&gt;int&lt; { 6, 10, 6 }.All(element =&gt; element &gt; 5)); // Prints True
  188. }
  189. [/csharp]
  190. [/codeblocks]
  191. See also [method any], [method filter], [method map] and [method reduce].
  192. [b]Note:[/b] Unlike relying on the size of an array returned by [method filter], this method will return as early as possible to improve performance (especially with large arrays).
  193. [b]Note:[/b] For an empty array, this method [url=https://en.wikipedia.org/wiki/Vacuous_truth]always[/url] returns [code]true[/code].
  194. </description>
  195. </method>
  196. <method name="any" qualifiers="const">
  197. <return type="bool" />
  198. <param index="0" name="method" type="Callable" />
  199. <description>
  200. Calls the given [Callable] on each element in the array and returns [code]true[/code] if the [Callable] returns [code]true[/code] for [i]one or more[/i] elements in the array. If the [Callable] returns [code]false[/code] for all elements in the array, this method returns [code]false[/code].
  201. The [param method] should take one [Variant] parameter (the current array element) and return a [bool].
  202. [codeblock]
  203. func greater_than_5(number):
  204. return number &gt; 5
  205. func _ready():
  206. print([6, 10, 6].any(greater_than_5)) # Prints true (3 elements evaluate to true).
  207. print([4, 10, 4].any(greater_than_5)) # Prints true (1 elements evaluate to true).
  208. print([4, 4, 4].any(greater_than_5)) # Prints false (0 elements evaluate to true).
  209. print([].any(greater_than_5)) # Prints false (0 elements evaluate to true).
  210. # Same as the first line above, but using a lambda function.
  211. print([6, 10, 6].any(func(number): return number &gt; 5)) # Prints true
  212. [/codeblock]
  213. See also [method all], [method filter], [method map] and [method reduce].
  214. [b]Note:[/b] Unlike relying on the size of an array returned by [method filter], this method will return as early as possible to improve performance (especially with large arrays).
  215. [b]Note:[/b] For an empty array, this method always returns [code]false[/code].
  216. </description>
  217. </method>
  218. <method name="append">
  219. <return type="void" />
  220. <param index="0" name="value" type="Variant" />
  221. <description>
  222. Appends [param value] at the end of the array (alias of [method push_back]).
  223. </description>
  224. </method>
  225. <method name="append_array">
  226. <return type="void" />
  227. <param index="0" name="array" type="Array" />
  228. <description>
  229. Appends another [param array] at the end of this array.
  230. [codeblock]
  231. var numbers = [1, 2, 3]
  232. var extra = [4, 5, 6]
  233. numbers.append_array(extra)
  234. print(numbers) # Prints [1, 2, 3, 4, 5, 6]
  235. [/codeblock]
  236. </description>
  237. </method>
  238. <method name="assign">
  239. <return type="void" />
  240. <param index="0" name="array" type="Array" />
  241. <description>
  242. Assigns elements of another [param array] into the array. Resizes the array to match [param array]. Performs type conversions if the array is typed.
  243. </description>
  244. </method>
  245. <method name="back" qualifiers="const">
  246. <return type="Variant" />
  247. <description>
  248. Returns the last element of the array. If the array is empty, fails and returns [code]null[/code]. See also [method front].
  249. [b]Note:[/b] Unlike with the [code][][/code] operator ([code]array[-1][/code]), an error is generated without stopping project execution.
  250. </description>
  251. </method>
  252. <method name="bsearch" qualifiers="const">
  253. <return type="int" />
  254. <param index="0" name="value" type="Variant" />
  255. <param index="1" name="before" type="bool" default="true" />
  256. <description>
  257. Returns the index of [param value] in the sorted array. If it cannot be found, returns where [param value] should be inserted to keep the array sorted. The algorithm used is [url=https://en.wikipedia.org/wiki/Binary_search_algorithm]binary search[/url].
  258. If [param before] is [code]true[/code] (as by default), the returned index comes before all existing elements equal to [param value] in the array.
  259. [codeblock]
  260. var numbers = [2, 4, 8, 10]
  261. var idx = numbers.bsearch(7)
  262. numbers.insert(idx, 7)
  263. print(numbers) # Prints [2, 4, 7, 8, 10]
  264. var fruits = ["Apple", "Lemon", "Lemon", "Orange"]
  265. print(fruits.bsearch("Lemon", true)) # Prints 1, points at the first "Lemon".
  266. print(fruits.bsearch("Lemon", false)) # Prints 3, points at "Orange".
  267. [/codeblock]
  268. [b]Note:[/b] Calling [method bsearch] on an [i]unsorted[/i] array will result in unexpected behavior. Use [method sort] before calling this method.
  269. </description>
  270. </method>
  271. <method name="bsearch_custom" qualifiers="const">
  272. <return type="int" />
  273. <param index="0" name="value" type="Variant" />
  274. <param index="1" name="func" type="Callable" />
  275. <param index="2" name="before" type="bool" default="true" />
  276. <description>
  277. Returns the index of [param value] in the sorted array. If it cannot be found, returns where [param value] should be inserted to keep the array sorted (using [param func] for the comparisons). The algorithm used is [url=https://en.wikipedia.org/wiki/Binary_search_algorithm]binary search[/url].
  278. Similar to [method sort_custom], [param func] is called as many times as necessary, receiving one array element and [param value] as arguments. The function should return [code]true[/code] if the array element should be [i]behind[/i] [param value], otherwise it should return [code]false[/code].
  279. If [param before] is [code]true[/code] (as by default), the returned index comes before all existing elements equal to [param value] in the array.
  280. [codeblock]
  281. func sort_by_amount(a, b):
  282. if a[1] &lt; b[1]:
  283. return true
  284. return false
  285. func _ready():
  286. var my_items = [["Tomato", 2], ["Kiwi", 5], ["Rice", 9]]
  287. var apple = ["Apple", 5]
  288. # "Apple" is inserted before "Kiwi".
  289. my_items.insert(my_items.bsearch_custom(apple, sort_by_amount, true), apple)
  290. var banana = ["Banana", 5]
  291. # "Banana" is inserted after "Kiwi".
  292. my_items.insert(my_items.bsearch_custom(banana, sort_by_amount, false), banana)
  293. # Prints [["Tomato", 2], ["Apple", 5], ["Kiwi", 5], ["Banana", 5], ["Rice", 9]]
  294. print(my_items)
  295. [/codeblock]
  296. [b]Note:[/b] Calling [method bsearch_custom] on an [i]unsorted[/i] array will result in unexpected behavior. Use [method sort_custom] with [param func] before calling this method.
  297. </description>
  298. </method>
  299. <method name="clear">
  300. <return type="void" />
  301. <description>
  302. Removes all elements from the array. This is equivalent to using [method resize] with a size of [code]0[/code].
  303. </description>
  304. </method>
  305. <method name="count" qualifiers="const">
  306. <return type="int" />
  307. <param index="0" name="value" type="Variant" />
  308. <description>
  309. Returns the number of times an element is in the array.
  310. To count how many elements in an array satisfy a condition, see [method reduce].
  311. </description>
  312. </method>
  313. <method name="duplicate" qualifiers="const">
  314. <return type="Array" />
  315. <param index="0" name="deep" type="bool" default="false" />
  316. <description>
  317. Returns a new copy of the array.
  318. By default, a [b]shallow[/b] copy is returned: all nested [Array], [Dictionary], and [Resource] elements are shared with the original array. Modifying them in one array will also affect them in the other.
  319. If [param deep] is [code]true[/code], a [b]deep[/b] copy is returned: all nested arrays and dictionaries are also duplicated (recursively). Any [Resource] is still shared with the original array, though.
  320. </description>
  321. </method>
  322. <method name="duplicate_deep" qualifiers="const">
  323. <return type="Array" />
  324. <param index="0" name="deep_subresources_mode" type="int" default="1" />
  325. <description>
  326. Duplicates this array, deeply, like [method duplicate][code](true)[/code], with extra control over how subresources are handled.
  327. [param deep_subresources_mode] must be one of the values from [enum Resource.ResourceDeepDuplicateMode]. By default, only internal resources will be duplicated (recursively).
  328. </description>
  329. </method>
  330. <method name="erase">
  331. <return type="void" />
  332. <param index="0" name="value" type="Variant" />
  333. <description>
  334. Finds and removes the first occurrence of [param value] from the array. If [param value] does not exist in the array, nothing happens. To remove an element by index, use [method remove_at] instead.
  335. [b]Note:[/b] This method shifts every element's index after the removed [param value] back, which may have a noticeable performance cost, especially on larger arrays.
  336. [b]Note:[/b] Erasing elements while iterating over arrays is [b]not[/b] supported and will result in unpredictable behavior.
  337. </description>
  338. </method>
  339. <method name="fill">
  340. <return type="void" />
  341. <param index="0" name="value" type="Variant" />
  342. <description>
  343. Assigns the given [param value] to all elements in the array.
  344. This method can often be combined with [method resize] to create an array with a given size and initialized elements:
  345. [codeblocks]
  346. [gdscript]
  347. var array = []
  348. array.resize(5)
  349. array.fill(2)
  350. print(array) # Prints [2, 2, 2, 2, 2]
  351. [/gdscript]
  352. [csharp]
  353. Godot.Collections.Array array = [];
  354. array.Resize(5);
  355. array.Fill(2);
  356. GD.Print(array); // Prints [2, 2, 2, 2, 2]
  357. [/csharp]
  358. [/codeblocks]
  359. [b]Note:[/b] If [param value] is a [Variant] passed by reference ([Object]-derived, [Array], [Dictionary], etc.), the array will be filled with references to the same [param value], which are not duplicates.
  360. </description>
  361. </method>
  362. <method name="filter" qualifiers="const">
  363. <return type="Array" />
  364. <param index="0" name="method" type="Callable" />
  365. <description>
  366. Calls the given [Callable] on each element in the array and returns a new, filtered [Array].
  367. The [param method] receives one of the array elements as an argument, and should return [code]true[/code] to add the element to the filtered array, or [code]false[/code] to exclude it.
  368. [codeblock]
  369. func is_even(number):
  370. return number % 2 == 0
  371. func _ready():
  372. print([1, 4, 5, 8].filter(is_even)) # Prints [4, 8]
  373. # Same as above, but using a lambda function.
  374. print([1, 4, 5, 8].filter(func(number): return number % 2 == 0))
  375. [/codeblock]
  376. See also [method any], [method all], [method map] and [method reduce].
  377. </description>
  378. </method>
  379. <method name="find" qualifiers="const">
  380. <return type="int" />
  381. <param index="0" name="what" type="Variant" />
  382. <param index="1" name="from" type="int" default="0" />
  383. <description>
  384. Returns the index of the [b]first[/b] occurrence of [param what] in this array, or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the end of the array.
  385. [b]Note:[/b] If you just want to know whether the array contains [param what], use [method has] ([code]Contains[/code] in C#). In GDScript, you may also use the [code]in[/code] operator.
  386. [b]Note:[/b] For performance reasons, the search is affected by [param what]'s [enum Variant.Type]. For example, [code]7[/code] ([int]) and [code]7.0[/code] ([float]) are not considered equal for this method.
  387. </description>
  388. </method>
  389. <method name="find_custom" qualifiers="const">
  390. <return type="int" />
  391. <param index="0" name="method" type="Callable" />
  392. <param index="1" name="from" type="int" default="0" />
  393. <description>
  394. Returns the index of the [b]first[/b] element in the array that causes [param method] to return [code]true[/code], or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the end of the array.
  395. [param method] is a callable that takes an element of the array, and returns a [bool].
  396. [b]Note:[/b] If you just want to know whether the array contains [i]anything[/i] that satisfies [param method], use [method any].
  397. [codeblocks]
  398. [gdscript]
  399. func is_even(number):
  400. return number % 2 == 0
  401. func _ready():
  402. print([1, 3, 4, 7].find_custom(is_even.bind())) # Prints 2
  403. [/gdscript]
  404. [/codeblocks]
  405. </description>
  406. </method>
  407. <method name="front" qualifiers="const">
  408. <return type="Variant" />
  409. <description>
  410. Returns the first element of the array. If the array is empty, fails and returns [code]null[/code]. See also [method back].
  411. [b]Note:[/b] Unlike with the [code][][/code] operator ([code]array[0][/code]), an error is generated without stopping project execution.
  412. </description>
  413. </method>
  414. <method name="get" qualifiers="const">
  415. <return type="Variant" />
  416. <param index="0" name="index" type="int" />
  417. <description>
  418. Returns the element at the given [param index] in the array. This is the same as using the [code][][/code] operator ([code]array[index][/code]).
  419. </description>
  420. </method>
  421. <method name="get_typed_builtin" qualifiers="const">
  422. <return type="int" />
  423. <description>
  424. Returns the built-in [Variant] type of the typed array as a [enum Variant.Type] constant. If the array is not typed, returns [constant TYPE_NIL]. See also [method is_typed].
  425. </description>
  426. </method>
  427. <method name="get_typed_class_name" qualifiers="const">
  428. <return type="StringName" />
  429. <description>
  430. Returns the [b]built-in[/b] class name of the typed array, if the built-in [Variant] type [constant TYPE_OBJECT]. Otherwise, returns an empty [StringName]. See also [method is_typed] and [method Object.get_class].
  431. </description>
  432. </method>
  433. <method name="get_typed_script" qualifiers="const">
  434. <return type="Variant" />
  435. <description>
  436. Returns the [Script] instance associated with this typed array, or [code]null[/code] if it does not exist. See also [method is_typed].
  437. </description>
  438. </method>
  439. <method name="has" qualifiers="const" keywords="includes, contains">
  440. <return type="bool" />
  441. <param index="0" name="value" type="Variant" />
  442. <description>
  443. Returns [code]true[/code] if the array contains the given [param value].
  444. [codeblocks]
  445. [gdscript]
  446. print(["inside", 7].has("inside")) # Prints true
  447. print(["inside", 7].has("outside")) # Prints false
  448. print(["inside", 7].has(7)) # Prints true
  449. print(["inside", 7].has("7")) # Prints false
  450. [/gdscript]
  451. [csharp]
  452. Godot.Collections.Array arr = ["inside", 7];
  453. // By C# convention, this method is renamed to `Contains`.
  454. GD.Print(arr.Contains("inside")); // Prints True
  455. GD.Print(arr.Contains("outside")); // Prints False
  456. GD.Print(arr.Contains(7)); // Prints True
  457. GD.Print(arr.Contains("7")); // Prints False
  458. [/csharp]
  459. [/codeblocks]
  460. In GDScript, this is equivalent to the [code]in[/code] operator:
  461. [codeblock]
  462. if 4 in [2, 4, 6, 8]:
  463. print("4 is here!") # Will be printed.
  464. [/codeblock]
  465. [b]Note:[/b] For performance reasons, the search is affected by the [param value]'s [enum Variant.Type]. For example, [code]7[/code] ([int]) and [code]7.0[/code] ([float]) are not considered equal for this method.
  466. </description>
  467. </method>
  468. <method name="hash" qualifiers="const">
  469. <return type="int" />
  470. <description>
  471. Returns a hashed 32-bit integer value representing the array and its contents.
  472. [b]Note:[/b] Arrays with equal hash values are [i]not[/i] guaranteed to be the same, as a result of hash collisions. On the countrary, arrays with different hash values are guaranteed to be different.
  473. </description>
  474. </method>
  475. <method name="insert">
  476. <return type="int" />
  477. <param index="0" name="position" type="int" />
  478. <param index="1" name="value" type="Variant" />
  479. <description>
  480. Inserts a new element ([param value]) at a given index ([param position]) in the array. [param position] should be between [code]0[/code] and the array's [method size]. If negative, [param position] is considered relative to the end of the array.
  481. Returns [constant OK] on success, or one of the other [enum Error] constants if this method fails.
  482. [b]Note:[/b] Every element's index after [param position] needs to be shifted forward, which may have a noticeable performance cost, especially on larger arrays.
  483. </description>
  484. </method>
  485. <method name="is_empty" qualifiers="const">
  486. <return type="bool" />
  487. <description>
  488. Returns [code]true[/code] if the array is empty ([code][][/code]). See also [method size].
  489. </description>
  490. </method>
  491. <method name="is_read_only" qualifiers="const">
  492. <return type="bool" />
  493. <description>
  494. Returns [code]true[/code] if the array is read-only. See [method make_read_only].
  495. In GDScript, arrays are automatically read-only if declared with the [code]const[/code] keyword.
  496. </description>
  497. </method>
  498. <method name="is_same_typed" qualifiers="const">
  499. <return type="bool" />
  500. <param index="0" name="array" type="Array" />
  501. <description>
  502. Returns [code]true[/code] if this array is typed the same as the given [param array]. See also [method is_typed].
  503. </description>
  504. </method>
  505. <method name="is_typed" qualifiers="const">
  506. <return type="bool" />
  507. <description>
  508. Returns [code]true[/code] 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 [Variant].
  509. In GDScript, it is possible to define a typed array with static typing:
  510. [codeblock]
  511. var numbers: Array[float] = [0.2, 4.2, -2.0]
  512. print(numbers.is_typed()) # Prints true
  513. [/codeblock]
  514. </description>
  515. </method>
  516. <method name="make_read_only">
  517. <return type="void" />
  518. <description>
  519. 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.
  520. In GDScript, arrays are automatically read-only if declared with the [code]const[/code] keyword.
  521. </description>
  522. </method>
  523. <method name="map" qualifiers="const">
  524. <return type="Array" />
  525. <param index="0" name="method" type="Callable" />
  526. <description>
  527. Calls the given [Callable] for each element in the array and returns a new array filled with values returned by the [param method].
  528. The [param method] should take one [Variant] parameter (the current array element) and can return any [Variant].
  529. [codeblock]
  530. func double(number):
  531. return number * 2
  532. func _ready():
  533. print([1, 2, 3].map(double)) # Prints [2, 4, 6]
  534. # Same as above, but using a lambda function.
  535. print([1, 2, 3].map(func(element): return element * 2))
  536. [/codeblock]
  537. See also [method filter], [method reduce], [method any] and [method all].
  538. </description>
  539. </method>
  540. <method name="max" qualifiers="const">
  541. <return type="Variant" />
  542. <description>
  543. Returns the maximum value contained in the array, if all elements can be compared. Otherwise, returns [code]null[/code]. See also [method min].
  544. To find the maximum value using a custom comparator, you can use [method reduce].
  545. </description>
  546. </method>
  547. <method name="min" qualifiers="const">
  548. <return type="Variant" />
  549. <description>
  550. Returns the minimum value contained in the array, if all elements can be compared. Otherwise, returns [code]null[/code]. See also [method max].
  551. </description>
  552. </method>
  553. <method name="pick_random" qualifiers="const">
  554. <return type="Variant" />
  555. <description>
  556. Returns a random element from the array. Generates an error and returns [code]null[/code] if the array is empty.
  557. [codeblocks]
  558. [gdscript]
  559. # May print 1, 2, 3.25, or "Hi".
  560. print([1, 2, 3.25, "Hi"].pick_random())
  561. [/gdscript]
  562. [csharp]
  563. Godot.Collections.Array array = [1, 2, 3.25f, "Hi"];
  564. GD.Print(array.PickRandom()); // May print 1, 2, 3.25, or "Hi".
  565. [/csharp]
  566. [/codeblocks]
  567. [b]Note:[/b] Like many similar functions in the engine (such as [method @GlobalScope.randi] or [method shuffle]), this method uses a common, global random seed. To get a predictable outcome from this method, see [method @GlobalScope.seed].
  568. </description>
  569. </method>
  570. <method name="pop_at">
  571. <return type="Variant" />
  572. <param index="0" name="position" type="int" />
  573. <description>
  574. Removes and returns the element of the array at index [param position]. If negative, [param position] is considered relative to the end of the array. Returns [code]null[/code] if the array is empty. If [param position] is out of bounds, an error message is also generated.
  575. [b]Note:[/b] This method shifts every element's index after [param position] back, which may have a noticeable performance cost, especially on larger arrays.
  576. </description>
  577. </method>
  578. <method name="pop_back">
  579. <return type="Variant" />
  580. <description>
  581. Removes and returns the last element of the array. Returns [code]null[/code] if the array is empty, without generating an error. See also [method pop_front].
  582. </description>
  583. </method>
  584. <method name="pop_front">
  585. <return type="Variant" />
  586. <description>
  587. Removes and returns the first element of the array. Returns [code]null[/code] if the array is empty, without generating an error. See also [method pop_back].
  588. [b]Note:[/b] This method shifts every other element's index back, which may have a noticeable performance cost, especially on larger arrays.
  589. </description>
  590. </method>
  591. <method name="push_back">
  592. <return type="void" />
  593. <param index="0" name="value" type="Variant" />
  594. <description>
  595. Appends an element at the end of the array. See also [method push_front].
  596. </description>
  597. </method>
  598. <method name="push_front">
  599. <return type="void" />
  600. <param index="0" name="value" type="Variant" />
  601. <description>
  602. Adds an element at the beginning of the array. See also [method push_back].
  603. [b]Note:[/b] This method shifts every other element's index forward, which may have a noticeable performance cost, especially on larger arrays.
  604. </description>
  605. </method>
  606. <method name="reduce" qualifiers="const">
  607. <return type="Variant" />
  608. <param index="0" name="method" type="Callable" />
  609. <param index="1" name="accum" type="Variant" default="null" />
  610. <description>
  611. Calls the given [Callable] for each element in array, accumulates the result in [param accum], then returns it.
  612. The [param method] takes two arguments: the current value of [param accum] and the current array element. If [param accum] is [code]null[/code] (as by default), the iteration will start from the second element, with the first one used as initial value of [param accum].
  613. [codeblock]
  614. func sum(accum, number):
  615. return accum + number
  616. func _ready():
  617. print([1, 2, 3].reduce(sum, 0)) # Prints 6
  618. print([1, 2, 3].reduce(sum, 10)) # Prints 16
  619. # Same as above, but using a lambda function.
  620. print([1, 2, 3].reduce(func(accum, number): return accum + number, 10))
  621. [/codeblock]
  622. If [method max] is not desirable, this method may also be used to implement a custom comparator:
  623. [codeblock]
  624. func _ready():
  625. var arr = [Vector2i(5, 0), Vector2i(3, 4), Vector2i(1, 2)]
  626. var longest_vec = arr.reduce(func(max, vec): return vec if is_length_greater(vec, max) else max)
  627. print(longest_vec) # Prints (3, 4)
  628. func is_length_greater(a, b):
  629. return a.length() &gt; b.length()
  630. [/codeblock]
  631. This method can also be used to count how many elements in an array satisfy a certain condition, similar to [method count]:
  632. [codeblock]
  633. func is_even(number):
  634. return number % 2 == 0
  635. func _ready():
  636. var arr = [1, 2, 3, 4, 5]
  637. # If the current element is even, increment count, otherwise leave count the same.
  638. var even_count = arr.reduce(func(count, next): return count + 1 if is_even(next) else count, 0)
  639. print(even_count) # Prints 2
  640. [/codeblock]
  641. See also [method map], [method filter], [method any], and [method all].
  642. </description>
  643. </method>
  644. <method name="remove_at">
  645. <return type="void" />
  646. <param index="0" name="position" type="int" />
  647. <description>
  648. Removes the element from the array at the given index ([param position]). If the index is out of bounds, this method fails. If the index is negative, [param position] is considered relative to the end of the array.
  649. If you need to return the removed element, use [method pop_at]. To remove an element by value, use [method erase] instead.
  650. [b]Note:[/b] This method shifts every element's index after [param position] back, which may have a noticeable performance cost, especially on larger arrays.
  651. [b]Note:[/b] The [param position] cannot be negative. To remove an element relative to the end of the array, use [code]arr.remove_at(arr.size() - (i + 1))[/code]. To remove the last element from the array, use [code]arr.resize(arr.size() - 1)[/code].
  652. </description>
  653. </method>
  654. <method name="resize">
  655. <return type="int" />
  656. <param index="0" name="size" type="int" />
  657. <description>
  658. Sets the array's number of elements to [param size]. If [param size] is smaller than the array's current size, the elements at the end are removed. If [param size] is greater, new default elements (usually [code]null[/code]) are added, depending on the array's type.
  659. Returns [constant OK] on success, or one of the following [enum Error] constants if this method fails: [constant ERR_LOCKED] if the array is read-only, [constant ERR_INVALID_PARAMETER] if the size is negative, or [constant ERR_OUT_OF_MEMORY] if allocations fail. Use [method size] to find the actual size of the array after resize.
  660. [b]Note:[/b] Calling this method once and assigning the new values is faster than calling [method append] for every new element.
  661. </description>
  662. </method>
  663. <method name="reverse">
  664. <return type="void" />
  665. <description>
  666. Reverses the order of all elements in the array.
  667. </description>
  668. </method>
  669. <method name="rfind" qualifiers="const">
  670. <return type="int" />
  671. <param index="0" name="what" type="Variant" />
  672. <param index="1" name="from" type="int" default="-1" />
  673. <description>
  674. Returns the index of the [b]last[/b] occurrence of [param what] in this array, or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the beginning of the array. This method is the reverse of [method find].
  675. </description>
  676. </method>
  677. <method name="rfind_custom" qualifiers="const">
  678. <return type="int" />
  679. <param index="0" name="method" type="Callable" />
  680. <param index="1" name="from" type="int" default="-1" />
  681. <description>
  682. Returns the index of the [b]last[/b] element of the array that causes [param method] to return [code]true[/code], or [code]-1[/code] if there are none. The search's start can be specified with [param from], continuing to the beginning of the array. This method is the reverse of [method find_custom].
  683. </description>
  684. </method>
  685. <method name="set">
  686. <return type="void" />
  687. <param index="0" name="index" type="int" />
  688. <param index="1" name="value" type="Variant" />
  689. <description>
  690. Sets the value of the element at the given [param index] to the given [param 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 [code][][/code] operator ([code]array[index] = value[/code]).
  691. </description>
  692. </method>
  693. <method name="shuffle">
  694. <return type="void" />
  695. <description>
  696. Shuffles all elements of the array in a random order.
  697. [b]Note:[/b] Like many similar functions in the engine (such as [method @GlobalScope.randi] or [method pick_random]), this method uses a common, global random seed. To get a predictable outcome from this method, see [method @GlobalScope.seed].
  698. </description>
  699. </method>
  700. <method name="size" qualifiers="const">
  701. <return type="int" />
  702. <description>
  703. Returns the number of elements in the array. Empty arrays ([code][][/code]) always return [code]0[/code]. See also [method is_empty].
  704. </description>
  705. </method>
  706. <method name="slice" qualifiers="const">
  707. <return type="Array" />
  708. <param index="0" name="begin" type="int" />
  709. <param index="1" name="end" type="int" default="2147483647" />
  710. <param index="2" name="step" type="int" default="1" />
  711. <param index="3" name="deep" type="bool" default="false" />
  712. <description>
  713. Returns a new [Array] containing this array's elements, from index [param begin] (inclusive) to [param end] (exclusive), every [param step] elements.
  714. If either [param begin] or [param end] are negative, their value is relative to the end of the array.
  715. If [param step] is negative, this method iterates through the array in reverse, returning a slice ordered backwards. For this to work, [param begin] must be greater than [param end].
  716. If [param deep] is [code]true[/code], all nested [Array] and [Dictionary] elements in the slice are duplicated from the original, recursively. See also [method duplicate]).
  717. [codeblock]
  718. var letters = ["A", "B", "C", "D", "E", "F"]
  719. print(letters.slice(0, 2)) # Prints ["A", "B"]
  720. print(letters.slice(2, -2)) # Prints ["C", "D"]
  721. print(letters.slice(-2, 6)) # Prints ["E", "F"]
  722. print(letters.slice(0, 6, 2)) # Prints ["A", "C", "E"]
  723. print(letters.slice(4, 1, -1)) # Prints ["E", "D", "C"]
  724. [/codeblock]
  725. </description>
  726. </method>
  727. <method name="sort">
  728. <return type="void" />
  729. <description>
  730. Sorts the array in ascending order. The final order is dependent on the "less than" ([code]&lt;[/code]) comparison between elements.
  731. [codeblocks]
  732. [gdscript]
  733. var numbers = [10, 5, 2.5, 8]
  734. numbers.sort()
  735. print(numbers) # Prints [2.5, 5, 8, 10]
  736. [/gdscript]
  737. [csharp]
  738. Godot.Collections.Array numbers = [10, 5, 2.5, 8];
  739. numbers.Sort();
  740. GD.Print(numbers); // Prints [2.5, 5, 8, 10]
  741. [/csharp]
  742. [/codeblocks]
  743. [b]Note:[/b] The sorting algorithm used is not [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url]. This means that equivalent elements (such as [code]2[/code] and [code]2.0[/code]) may have their order changed when calling [method sort].
  744. </description>
  745. </method>
  746. <method name="sort_custom">
  747. <return type="void" />
  748. <param index="0" name="func" type="Callable" />
  749. <description>
  750. Sorts the array using a custom [Callable].
  751. [param func] is called as many times as necessary, receiving two array elements as arguments. The function should return [code]true[/code] if the first element should be moved [i]before[/i] the second one, otherwise it should return [code]false[/code].
  752. [codeblock]
  753. func sort_ascending(a, b):
  754. if a[1] &lt; b[1]:
  755. return true
  756. return false
  757. func _ready():
  758. var my_items = [["Tomato", 5], ["Apple", 9], ["Rice", 4]]
  759. my_items.sort_custom(sort_ascending)
  760. print(my_items) # Prints [["Rice", 4], ["Tomato", 5], ["Apple", 9]]
  761. # Sort descending, using a lambda function.
  762. my_items.sort_custom(func(a, b): return a[1] &gt; b[1])
  763. print(my_items) # Prints [["Apple", 9], ["Tomato", 5], ["Rice", 4]]
  764. [/codeblock]
  765. It may also be necessary to use this method to sort strings by natural order, with [method String.naturalnocasecmp_to], as in the following example:
  766. [codeblock]
  767. var files = ["newfile1", "newfile2", "newfile10", "newfile11"]
  768. files.sort_custom(func(a, b): return a.naturalnocasecmp_to(b) &lt; 0)
  769. print(files) # Prints ["newfile1", "newfile2", "newfile10", "newfile11"]
  770. [/codeblock]
  771. [b]Note:[/b] In C#, this method is not supported.
  772. [b]Note:[/b] The sorting algorithm used is not [url=https://en.wikipedia.org/wiki/Sorting_algorithm#Stability]stable[/url]. This means that values considered equal may have their order changed when calling this method.
  773. [b]Note:[/b] You should not randomize the return value of [param func], as the heapsort algorithm expects a consistent result. Randomizing the return value will result in unexpected behavior.
  774. </description>
  775. </method>
  776. </methods>
  777. <operators>
  778. <operator name="operator !=">
  779. <return type="bool" />
  780. <param index="0" name="right" type="Array" />
  781. <description>
  782. Returns [code]true[/code] if the array's size or its elements are different than [param right]'s.
  783. </description>
  784. </operator>
  785. <operator name="operator +">
  786. <return type="Array" />
  787. <param index="0" name="right" type="Array" />
  788. <description>
  789. Appends the [param right] array to the left operand, creating a new [Array]. This is also known as an array concatenation.
  790. [codeblocks]
  791. [gdscript]
  792. var array1 = ["One", 2]
  793. var array2 = [3, "Four"]
  794. print(array1 + array2) # Prints ["One", 2, 3, "Four"]
  795. [/gdscript]
  796. [csharp]
  797. // Note that concatenation is not possible with C#'s native Array type.
  798. Godot.Collections.Array array1 = ["One", 2];
  799. Godot.Collections.Array array2 = [3, "Four"];
  800. GD.Print(array1 + array2); // Prints ["One", 2, 3, "Four"]
  801. [/csharp]
  802. [/codeblocks]
  803. [b]Note:[/b] For existing arrays, [method append_array] is much more efficient than concatenation and assignment with the [code]+=[/code] operator.
  804. </description>
  805. </operator>
  806. <operator name="operator &lt;">
  807. <return type="bool" />
  808. <param index="0" name="right" type="Array" />
  809. <description>
  810. Compares the elements of both arrays in order, starting from index [code]0[/code] and ending on the last index in common between both arrays. For each pair of elements, returns [code]true[/code] if this array's element is less than [param right]'s, [code]false[/code] if this element is greater. Otherwise, continues to the next pair.
  811. If all searched elements are equal, returns [code]true[/code] if this array's size is less than [param right]'s, otherwise returns [code]false[/code].
  812. </description>
  813. </operator>
  814. <operator name="operator &lt;=">
  815. <return type="bool" />
  816. <param index="0" name="right" type="Array" />
  817. <description>
  818. Compares the elements of both arrays in order, starting from index [code]0[/code] and ending on the last index in common between both arrays. For each pair of elements, returns [code]true[/code] if this array's element is less than [param right]'s, [code]false[/code] if this element is greater. Otherwise, continues to the next pair.
  819. If all searched elements are equal, returns [code]true[/code] if this array's size is less or equal to [param right]'s, otherwise returns [code]false[/code].
  820. </description>
  821. </operator>
  822. <operator name="operator ==">
  823. <return type="bool" />
  824. <param index="0" name="right" type="Array" />
  825. <description>
  826. Compares the left operand [Array] against the [param right] [Array]. Returns [code]true[/code] if the sizes and contents of the arrays are equal, [code]false[/code] otherwise.
  827. </description>
  828. </operator>
  829. <operator name="operator &gt;">
  830. <return type="bool" />
  831. <param index="0" name="right" type="Array" />
  832. <description>
  833. Compares the elements of both arrays in order, starting from index [code]0[/code] and ending on the last index in common between both arrays. For each pair of elements, returns [code]true[/code] if this array's element is greater than [param right]'s, [code]false[/code] if this element is less. Otherwise, continues to the next pair.
  834. If all searched elements are equal, returns [code]true[/code] if this array's size is greater than [param right]'s, otherwise returns [code]false[/code].
  835. </description>
  836. </operator>
  837. <operator name="operator &gt;=">
  838. <return type="bool" />
  839. <param index="0" name="right" type="Array" />
  840. <description>
  841. Compares the elements of both arrays in order, starting from index [code]0[/code] and ending on the last index in common between both arrays. For each pair of elements, returns [code]true[/code] if this array's element is greater than [param right]'s, [code]false[/code] if this element is less. Otherwise, continues to the next pair.
  842. If all searched elements are equal, returns [code]true[/code] if this array's size is greater or equal to [param right]'s, otherwise returns [code]false[/code].
  843. </description>
  844. </operator>
  845. <operator name="operator []">
  846. <return type="Variant" />
  847. <param index="0" name="index" type="int" />
  848. <description>
  849. Returns the [Variant] element at the specified [param index]. Arrays start at index 0. If [param index] is greater or equal to [code]0[/code], the element is fetched starting from the beginning of the array. If [param 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.
  850. </description>
  851. </operator>
  852. </operators>
  853. </class>