|
@@ -4,7 +4,7 @@
|
|
A built-in data structure that holds a sequence of elements.
|
|
A built-in data structure that holds a sequence of elements.
|
|
</brief_description>
|
|
</brief_description>
|
|
<description>
|
|
<description>
|
|
- 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.).
|
|
|
|
|
|
+ An array data structure that can contain a sequence of elements of any [Variant] type by default. Values can optionally be constrained to a specific type by creating a [i]typed array[/i]. 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.).
|
|
[codeblocks]
|
|
[codeblocks]
|
|
[gdscript]
|
|
[gdscript]
|
|
var array = ["First", 2, 3, "Last"]
|
|
var array = ["First", 2, 3, "Last"]
|
|
@@ -15,6 +15,10 @@
|
|
array[1] = "Second"
|
|
array[1] = "Second"
|
|
print(array[1]) # Prints "Second"
|
|
print(array[1]) # Prints "Second"
|
|
print(array[-3]) # Prints "Second"
|
|
print(array[-3]) # Prints "Second"
|
|
|
|
+
|
|
|
|
+ # This typed array can only contain integers.
|
|
|
|
+ # Attempting to add any other type will result in an error.
|
|
|
|
+ var typed_array: Array[int] = [1, 2, 3]
|
|
[/gdscript]
|
|
[/gdscript]
|
|
[csharp]
|
|
[csharp]
|
|
Godot.Collections.Array array = ["First", 2, 3, "Last"];
|
|
Godot.Collections.Array array = ["First", 2, 3, "Last"];
|
|
@@ -25,6 +29,10 @@
|
|
array[1] = "Second";
|
|
array[1] = "Second";
|
|
GD.Print(array[1]); // Prints "Second"
|
|
GD.Print(array[1]); // Prints "Second"
|
|
GD.Print(array[^3]); // Prints "Second"
|
|
GD.Print(array[^3]); // Prints "Second"
|
|
|
|
+
|
|
|
|
+ // This typed array can only contain integers.
|
|
|
|
+ // Attempting to add any other type will result in an error.
|
|
|
|
+ Godot.Collections.Array>int< typedArray = [1, 2, 3];
|
|
[/csharp]
|
|
[/csharp]
|
|
[/codeblocks]
|
|
[/codeblocks]
|
|
[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].
|
|
[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].
|