class_array.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. .. Generated automatically by doc/tools/makerst.py in Godot's source tree.
  2. .. DO NOT EDIT THIS FILE, but the Array.xml source instead.
  3. .. The source is found in doc/classes or modules/<name>/doc_classes.
  4. .. _class_Array:
  5. Array
  6. =====
  7. **Category:** Built-In Types
  8. Brief Description
  9. -----------------
  10. Generic array datatype.
  11. Member Functions
  12. ----------------
  13. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  14. | :ref:`Array<class_array>` | :ref:`Array<class_Array_Array>` **(** :ref:`PoolColorArray<class_poolcolorarray>` from **)** |
  15. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. | :ref:`Array<class_array>` | :ref:`Array<class_Array_Array>` **(** :ref:`PoolVector3Array<class_poolvector3array>` from **)** |
  17. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  18. | :ref:`Array<class_array>` | :ref:`Array<class_Array_Array>` **(** :ref:`PoolVector2Array<class_poolvector2array>` from **)** |
  19. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  20. | :ref:`Array<class_array>` | :ref:`Array<class_Array_Array>` **(** :ref:`PoolStringArray<class_poolstringarray>` from **)** |
  21. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  22. | :ref:`Array<class_array>` | :ref:`Array<class_Array_Array>` **(** :ref:`PoolRealArray<class_poolrealarray>` from **)** |
  23. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  24. | :ref:`Array<class_array>` | :ref:`Array<class_Array_Array>` **(** :ref:`PoolIntArray<class_poolintarray>` from **)** |
  25. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. | :ref:`Array<class_array>` | :ref:`Array<class_Array_Array>` **(** :ref:`PoolByteArray<class_poolbytearray>` from **)** |
  27. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  28. | void | :ref:`append<class_Array_append>` **(** var value **)** |
  29. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  30. | var | :ref:`back<class_Array_back>` **(** **)** |
  31. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  32. | :ref:`int<class_int>` | :ref:`bsearch<class_Array_bsearch>` **(** var value, :ref:`bool<class_bool>` before=True **)** |
  33. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  34. | :ref:`int<class_int>` | :ref:`bsearch_custom<class_Array_bsearch_custom>` **(** var value, :ref:`Object<class_object>` obj, :ref:`String<class_string>` func, :ref:`bool<class_bool>` before=True **)** |
  35. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  36. | void | :ref:`clear<class_Array_clear>` **(** **)** |
  37. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  38. | :ref:`int<class_int>` | :ref:`count<class_Array_count>` **(** var value **)** |
  39. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  40. | :ref:`Array<class_array>` | :ref:`duplicate<class_Array_duplicate>` **(** **)** |
  41. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  42. | :ref:`bool<class_bool>` | :ref:`empty<class_Array_empty>` **(** **)** |
  43. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  44. | void | :ref:`erase<class_Array_erase>` **(** var value **)** |
  45. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  46. | :ref:`int<class_int>` | :ref:`find<class_Array_find>` **(** var what, :ref:`int<class_int>` from=0 **)** |
  47. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  48. | :ref:`int<class_int>` | :ref:`find_last<class_Array_find_last>` **(** var value **)** |
  49. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  50. | var | :ref:`front<class_Array_front>` **(** **)** |
  51. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  52. | :ref:`bool<class_bool>` | :ref:`has<class_Array_has>` **(** var value **)** |
  53. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  54. | :ref:`int<class_int>` | :ref:`hash<class_Array_hash>` **(** **)** |
  55. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  56. | void | :ref:`insert<class_Array_insert>` **(** :ref:`int<class_int>` position, var value **)** |
  57. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  58. | void | :ref:`invert<class_Array_invert>` **(** **)** |
  59. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  60. | var | :ref:`pop_back<class_Array_pop_back>` **(** **)** |
  61. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  62. | var | :ref:`pop_front<class_Array_pop_front>` **(** **)** |
  63. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  64. | void | :ref:`push_back<class_Array_push_back>` **(** var value **)** |
  65. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  66. | void | :ref:`push_front<class_Array_push_front>` **(** var value **)** |
  67. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  68. | void | :ref:`remove<class_Array_remove>` **(** :ref:`int<class_int>` position **)** |
  69. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  70. | void | :ref:`resize<class_Array_resize>` **(** :ref:`int<class_int>` size **)** |
  71. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  72. | :ref:`int<class_int>` | :ref:`rfind<class_Array_rfind>` **(** var what, :ref:`int<class_int>` from=-1 **)** |
  73. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  74. | :ref:`int<class_int>` | :ref:`size<class_Array_size>` **(** **)** |
  75. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  76. | void | :ref:`sort<class_Array_sort>` **(** **)** |
  77. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  78. | void | :ref:`sort_custom<class_Array_sort_custom>` **(** :ref:`Object<class_object>` obj, :ref:`String<class_string>` func **)** |
  79. +----------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  80. Description
  81. -----------
  82. Generic array, contains several elements of any type, accessible by numerical index starting at 0. Negative indices can be used to count from the right, like in Python. Arrays are always passed by reference.
  83. Member Function Description
  84. ---------------------------
  85. .. _class_Array_Array:
  86. - :ref:`Array<class_array>` **Array** **(** :ref:`PoolColorArray<class_poolcolorarray>` from **)**
  87. Construct an array from a :ref:`PoolColorArray<class_poolcolorarray>`.
  88. .. _class_Array_Array:
  89. - :ref:`Array<class_array>` **Array** **(** :ref:`PoolVector3Array<class_poolvector3array>` from **)**
  90. Construct an array from a :ref:`PoolVector3Array<class_poolvector3array>`.
  91. .. _class_Array_Array:
  92. - :ref:`Array<class_array>` **Array** **(** :ref:`PoolVector2Array<class_poolvector2array>` from **)**
  93. Construct an array from a :ref:`PoolVector2Array<class_poolvector2array>`.
  94. .. _class_Array_Array:
  95. - :ref:`Array<class_array>` **Array** **(** :ref:`PoolStringArray<class_poolstringarray>` from **)**
  96. Construct an array from a :ref:`PoolStringArray<class_poolstringarray>`.
  97. .. _class_Array_Array:
  98. - :ref:`Array<class_array>` **Array** **(** :ref:`PoolRealArray<class_poolrealarray>` from **)**
  99. Construct an array from a :ref:`PoolRealArray<class_poolrealarray>`.
  100. .. _class_Array_Array:
  101. - :ref:`Array<class_array>` **Array** **(** :ref:`PoolIntArray<class_poolintarray>` from **)**
  102. Construct an array from a :ref:`PoolIntArray<class_poolintarray>`.
  103. .. _class_Array_Array:
  104. - :ref:`Array<class_array>` **Array** **(** :ref:`PoolByteArray<class_poolbytearray>` from **)**
  105. Construct an array from a :ref:`PoolByteArray<class_poolbytearray>`.
  106. .. _class_Array_append:
  107. - void **append** **(** var value **)**
  108. Append an element at the end of the array (alias of :ref:`push_back<class_Array_push_back>`).
  109. .. _class_Array_back:
  110. - var **back** **(** **)**
  111. Returns the last element of the array if the array is not empty (size>0).
  112. .. _class_Array_bsearch:
  113. - :ref:`int<class_int>` **bsearch** **(** var value, :ref:`bool<class_bool>` before=True **)**
  114. 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. Note that calling bsearch on an unsorted array results in unexpected behavior.
  115. .. _class_Array_bsearch_custom:
  116. - :ref:`int<class_int>` **bsearch_custom** **(** var value, :ref:`Object<class_object>` obj, :ref:`String<class_string>` func, :ref:`bool<class_bool>` before=True **)**
  117. 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. Note that calling bsearch on an unsorted array results in unexpected behavior.
  118. .. _class_Array_clear:
  119. - void **clear** **(** **)**
  120. Clear the array (resize to 0).
  121. .. _class_Array_count:
  122. - :ref:`int<class_int>` **count** **(** var value **)**
  123. Return the amount of times an element is in the array.
  124. .. _class_Array_duplicate:
  125. - :ref:`Array<class_array>` **duplicate** **(** **)**
  126. Returns a copy of this ``Array``.
  127. .. _class_Array_empty:
  128. - :ref:`bool<class_bool>` **empty** **(** **)**
  129. Return true if the array is empty (size==0).
  130. .. _class_Array_erase:
  131. - void **erase** **(** var value **)**
  132. Remove the first occurrence of a value from the array.
  133. .. _class_Array_find:
  134. - :ref:`int<class_int>` **find** **(** var what, :ref:`int<class_int>` from=0 **)**
  135. Searches the array for a value and returns its index or -1 if not found. Optionally, the initial search index can be passed.
  136. .. _class_Array_find_last:
  137. - :ref:`int<class_int>` **find_last** **(** var value **)**
  138. Searches the array in reverse order for a value and returns its index or -1 if not found.
  139. .. _class_Array_front:
  140. - var **front** **(** **)**
  141. Returns the first element of the array if the array is not empty (size>0).
  142. .. _class_Array_has:
  143. - :ref:`bool<class_bool>` **has** **(** var value **)**
  144. Return true if the array contains given value.
  145. ::
  146. [ "inside", 7 ].has("inside") == true
  147. [ "inside", 7 ].has("outside") == false
  148. [ "inside", 7 ].has(7) == true
  149. [ "inside", 7 ].has("7") == false
  150. .. _class_Array_hash:
  151. - :ref:`int<class_int>` **hash** **(** **)**
  152. Return a hashed integer value representing the array contents.
  153. .. _class_Array_insert:
  154. - void **insert** **(** :ref:`int<class_int>` position, var value **)**
  155. Insert a new element at a given position in the array. The position must be valid, or at the end of the array (pos==size()).
  156. .. _class_Array_invert:
  157. - void **invert** **(** **)**
  158. Reverse the order of the elements in the array (so first element will now be the last) and return reference to the array.
  159. .. _class_Array_pop_back:
  160. - var **pop_back** **(** **)**
  161. Remove the last element of the array.
  162. .. _class_Array_pop_front:
  163. - var **pop_front** **(** **)**
  164. Remove the first element of the array.
  165. .. _class_Array_push_back:
  166. - void **push_back** **(** var value **)**
  167. Append an element at the end of the array.
  168. .. _class_Array_push_front:
  169. - void **push_front** **(** var value **)**
  170. Add an element at the beginning of the array.
  171. .. _class_Array_remove:
  172. - void **remove** **(** :ref:`int<class_int>` position **)**
  173. Remove an element from the array by index.
  174. .. _class_Array_resize:
  175. - void **resize** **(** :ref:`int<class_int>` size **)**
  176. Resize the array to contain a different number of elements. If the array size is smaller, elements are cleared, if bigger, new elements are Null.
  177. .. _class_Array_rfind:
  178. - :ref:`int<class_int>` **rfind** **(** var what, :ref:`int<class_int>` from=-1 **)**
  179. 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.
  180. .. _class_Array_size:
  181. - :ref:`int<class_int>` **size** **(** **)**
  182. Return the amount of elements in the array.
  183. .. _class_Array_sort:
  184. - void **sort** **(** **)**
  185. Sort the array using natural order and return reference to the array.
  186. .. _class_Array_sort_custom:
  187. - void **sort_custom** **(** :ref:`Object<class_object>` obj, :ref:`String<class_string>` func **)**
  188. Sort the array using a custom method and return reference to the array. The arguments are an object that holds the method and the name of such method. The custom method receives two arguments (a pair of elements from the array) and must return true if the first argument is less than the second, and return false otherwise. Note: you cannot randomize the return value as the heapsort algorithm expects a deterministic result. Doing so will result in unexpected behavior.
  189. ::
  190. class MyCustomSorter:
  191. static func sort(a, b):
  192. if a[0] < b[0]:
  193. return true
  194. return false
  195. var my_items = [[5, "Potato"], [9, "Rice"], [4, "Tomato"]]
  196. my_items.sort_custom(MyCustomSorter, "sort")