diff options
Diffstat (limited to 'doc/classes/Array.xml')
-rw-r--r-- | doc/classes/Array.xml | 91 |
1 files changed, 81 insertions, 10 deletions
diff --git a/doc/classes/Array.xml b/doc/classes/Array.xml index 35656dbd70..ee21493434 100644 --- a/doc/classes/Array.xml +++ b/doc/classes/Array.xml @@ -40,6 +40,7 @@ [/codeblocks] [b]Note:[/b] Concatenating with the [code]+=[/code] operator will create a new array, which has a cost. If you want to append another array to an existing array, [method append_array] is more efficient. [b]Note:[/b] Arrays are always passed by reference. To get a copy of an array that can be modified independently of the original array, use [method duplicate]. + [b]Note:[/b] Erasing elements while iterating over arrays is [b]not[/b] supported and will result in unpredictable behavior. [b]Note:[/b] When declaring an array with [code]const[/code], the array itself can still be mutated by defining the values at individual indices or pushing/removing elements. Using [code]const[/code] will only prevent assigning the constant with another value after it was initialized. </description> <tutorials> @@ -53,6 +54,16 @@ </constructor> <constructor name="Array"> <return type="Array" /> + <param index="0" name="base" type="Array" /> + <param index="1" name="type" type="int" /> + <param index="2" name="class_name" type="StringName" /> + <param index="3" name="script" type="Variant" /> + <description> + Creates a typed array from the [param base] array. The base array can't be already typed. See [method set_typed] for more details. + </description> + </constructor> + <constructor name="Array"> + <return type="Array" /> <param index="0" name="from" type="Array" /> <description> Constructs an [Array] as a copy of the given [Array]. @@ -243,6 +254,7 @@ 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 [method remove_at] instead. [b]Note:[/b] This method acts in-place and doesn't return a value. [b]Note:[/b] 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. + [b]Note:[/b] Do not erase entries while iterating over the array. </description> </method> <method name="fill"> @@ -262,6 +274,7 @@ array.Fill(0); // Initialize the 10 elements to 0. [/csharp] [/codeblocks] + [b]Note:[/b] If [param value] is of a reference type ([Object]-derived, [Array], [Dictionary], etc.) then the array is filled with the references to the same object, i.e. no duplicates are created. </description> </method> <method name="filter" qualifiers="const"> @@ -289,18 +302,29 @@ Searches the array for a value and returns its index or [code]-1[/code] if not found. Optionally, the initial search index can be passed. </description> </method> - <method name="find_last" qualifiers="const"> + <method name="front" qualifiers="const"> + <return type="Variant" /> + <description> + Returns the first element of the array. Prints an error and returns [code]null[/code] if the array is empty. + [b]Note:[/b] Calling this function is not the same as writing [code]array[0][/code]. If the array is empty, accessing by index will pause project execution when running from the editor. + </description> + </method> + <method name="get_typed_builtin" qualifiers="const"> <return type="int" /> - <param index="0" name="value" type="Variant" /> <description> - Searches the array in reverse order for a value and returns its index or [code]-1[/code] if not found. + Returns the [enum Variant.Type] constant for a typed array. If the [Array] is not typed, returns [constant TYPE_NIL]. </description> </method> - <method name="front" qualifiers="const"> + <method name="get_typed_class_name" qualifiers="const"> + <return type="StringName" /> + <description> + Returns a class name of a typed [Array] of type [constant TYPE_OBJECT]. + </description> + </method> + <method name="get_typed_script" qualifiers="const"> <return type="Variant" /> <description> - Returns the first element of the array. Prints an error and returns [code]null[/code] if the array is empty. - [b]Note:[/b] Calling this function is not the same as writing [code]array[0][/code]. If the array is empty, accessing by index will pause project execution when running from the editor. + Returns the script associated with a typed array tied to a class name. </description> </method> <method name="has" qualifiers="const"> @@ -324,7 +348,6 @@ GD.Print(arr.Contains("7")); // False [/csharp] [/codeblocks] - [b]Note:[/b] This is equivalent to using the [code]in[/code] operator as follows: [codeblocks] [gdscript] @@ -366,6 +389,18 @@ Returns [code]true[/code] if the array is empty. </description> </method> + <method name="is_read_only" qualifiers="const"> + <return type="bool" /> + <description> + Returns [code]true[/code] if the array is read-only. See [method set_read_only]. Arrays are automatically read-only if declared with [code]const[/code] keyword. + </description> + </method> + <method name="is_typed" qualifiers="const"> + <return type="bool" /> + <description> + Returns [code]true[/code] if the array is typed. Typed arrays can only store elements of their associated type and provide type safety for the [code][][/code] operator. Methods of typed array still return [Variant]. + </description> + </method> <method name="map" qualifiers="const"> <return type="Array" /> <param index="0" name="method" type="Callable" /> @@ -395,6 +430,16 @@ Returns the minimum value contained in the array if all elements are of comparable types. If the elements can't be compared, [code]null[/code] is returned. </description> </method> + <method name="pick_random" qualifiers="const"> + <return type="Variant" /> + <description> + Returns a random value from the target array. + [codeblock] + var array: Array[int] = [1, 2, 3, 4] + print(array.pick_random()) # Prints either of the four numbers. + [/codeblock] + </description> + </method> <method name="pop_at"> <return type="Variant" /> <param index="0" name="position" type="int" /> @@ -479,6 +524,23 @@ 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. </description> </method> + <method name="set_read_only"> + <return type="void" /> + <param index="0" name="enable" type="bool" /> + <description> + 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. + </description> + </method> + <method name="set_typed"> + <return type="void" /> + <param index="0" name="type" type="int" /> + <param index="1" name="class_name" type="StringName" /> + <param index="2" name="script" type="Variant" /> + <description> + Makes the [Array] typed. The [param type] should be one of the [enum Variant.Type] constants. [param class_name] is optional and can only be provided for [constant TYPE_OBJECT]. [param script] can only be provided if [param class_name] is not empty. + The method fails if an array is already typed. + </description> + </method> <method name="shuffle"> <return type="void" /> <description> @@ -509,6 +571,7 @@ <return type="void" /> <description> Sorts the array. + [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 using [method sort]. [b]Note:[/b] 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: [codeblocks] [gdscript] @@ -533,7 +596,8 @@ <param index="0" name="func" type="Callable" /> <description> Sorts the array using a custom method. The custom method receives two arguments (a pair of elements from the array) and must return either [code]true[/code] or [code]false[/code]. For two elements [code]a[/code] and [code]b[/code], if the given method returns [code]true[/code], element [code]b[/code] will be after element [code]a[/code] in the array. - [b]Note:[/b] You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Doing so will result in unexpected behavior. + [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 using [method sort_custom]. + [b]Note:[/b] You cannot randomize the return value as the heapsort algorithm expects a deterministic result. Randomizing the return value will result in unexpected behavior. [codeblocks] [gdscript] func sort_ascending(a, b): @@ -556,6 +620,13 @@ [/codeblocks] </description> </method> + <method name="typed_assign"> + <return type="bool" /> + <param index="0" name="array" type="Array" /> + <description> + Assigns a different [Array] to this array reference. It the array is typed, the new array's type must be compatible and its elements will be automatically converted. + </description> + </method> </methods> <operators> <operator name="operator !="> @@ -576,14 +647,14 @@ <return type="bool" /> <param index="0" name="right" type="Array" /> <description> - Performs a comparison for each index between the left operand [Array] and the [param right] [Array], considering the highest common index of both arrays for this comparison: Returns [code]true[/code] on the first occurrence of an element that is less, or [code]false[/code] 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 [code]false[/code] if the left operand [Array] has less elements, otherwise it returns [code]true[/code]. + Performs a comparison for each index between the left operand [Array] and the [param right] [Array], considering the highest common index of both arrays for this comparison: Returns [code]true[/code] on the first occurrence of an element that is less, or [code]false[/code] 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 [code]false[/code] if the left operand [Array] has fewer elements, otherwise it returns [code]true[/code]. </description> </operator> <operator name="operator <="> <return type="bool" /> <param index="0" name="right" type="Array" /> <description> - Performs a comparison for each index between the left operand [Array] and the [param right] [Array], considering the highest common index of both arrays for this comparison: Returns [code]true[/code] on the first occurrence of an element that is less, or [code]false[/code] 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 [code]true[/code] if the left operand [Array] has less or the same number of elements, otherwise it returns [code]false[/code]. + Performs a comparison for each index between the left operand [Array] and the [param right] [Array], considering the highest common index of both arrays for this comparison: Returns [code]true[/code] on the first occurrence of an element that is less, or [code]false[/code] 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 [code]true[/code] if the left operand [Array] has the same number of elements or fewer, otherwise it returns [code]false[/code]. </description> </operator> <operator name="operator =="> |