Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
PackedVector2Array
Vector2 緊縮陣列。
說明
An array specifically designed to hold Vector2. Packs data tightly, so it saves memory for large array sizes.
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. PackedVector2Array versus Array[Vector2]). 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 Array.map(). Typed arrays are in turn faster to iterate on and modify than untyped arrays.
Note: Packed arrays are always passed by reference. To get a copy of an array that can be modified independently of the original array, use duplicate(). This is not the case for built-in properties and methods. In these cases the returned packed array is a copy, and changing it will not affect the original value. To update a built-in property of this type, modify the returned array and then assign it to the property again.
Note: In a boolean context, a packed array will evaluate to false if it's empty. Otherwise, a packed array will always evaluate to true.
備註
使用 C# 操作此 API 時有顯著差異,詳見 C# API 與 GDScript 的不同。
教學
建構子
PackedVector2Array(from: Array) |
方法
void |
append_array(array: PackedVector2Array) |
void |
clear() |
duplicate() const |
|
void |
|
is_empty() const |
|
void |
|
void |
reverse() |
void |
|
size() const |
|
void |
sort() |
to_byte_array() const |
運算子
operator !=(right: PackedVector2Array) |
|
operator *(right: Transform2D) |
|
operator +(right: PackedVector2Array) |
|
operator ==(right: PackedVector2Array) |
|
operator [](index: int) |
建構子說明
PackedVector2Array PackedVector2Array() 🔗
建構空的 PackedVector2Array。
PackedVector2Array PackedVector2Array(from: PackedVector2Array)
建構給定 PackedVector2Array 的副本。
PackedVector2Array PackedVector2Array(from: Array)
建構新的 PackedVector2Array。也可以傳入需要轉換的通用 Array。
注意:使用元素初始化 PackedVector2Array 時,必須使用元素為 Vector2 的 Array 進行初始化:
var array = PackedVector2Array([Vector2(12, 34), Vector2(56, 78)])
方法說明
向陣列末尾追加一個元素(push_back() 的別名)。
void append_array(array: PackedVector2Array) 🔗
在該陣列的末尾追加一個 PackedVector2Array。
int bsearch(value: Vector2, before: bool = true) const 🔗
使用二分法搜尋已有值的索引(如果該值尚未存在於陣列中,則為保持排序順序的插入索引)。傳遞 before 說明符是可選的。如果該參數為 false,則返回的索引位於陣列中該值的所有已有的條目之後。
注意:在未排序的陣列上呼叫 bsearch() 會產生預料之外的行為。
注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。
void clear() 🔗
清空陣列。相當於呼叫 resize() 時指定大小為 0。
int count(value: Vector2) const 🔗
返回陣列中某個元素出現的次數。
注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。
PackedVector2Array duplicate() const 🔗
建立該陣列的副本,並將該副本返回。
Removes the first occurrence of a value from the array and returns true. If the value does not exist in the array, nothing happens and false is returned. To remove an element by index, use remove_at() instead.
Note: Vectors with @GDScript.NAN elements don't behave the same as other vectors. Therefore, the results from this method may not be accurate if NaNs are included.
將陣列中的所有元素都設為給定的值。通常與 resize() 一起使用,建立給定大小的陣列並初始化元素。
int find(value: Vector2, from: int = 0) const 🔗
在陣列中搜索值並返回其索引,如果未找到則返回 -1 。可選地,可以傳遞起始搜索索引。
注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。
Vector2 get(index: int) const 🔗
Returns the Vector2 at the given index in the array. If index is out-of-bounds or negative, this method fails and returns Vector2(0, 0).
This method is similar (but not identical) to the [] operator. Most notably, when this method fails, it doesn't pause project execution if run from the editor.
bool has(value: Vector2) const 🔗
如果陣列中包含 value,則返回 true。
注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。
int insert(at_index: int, value: Vector2) 🔗
在陣列中給定的位置插入一個新元素。這個位置必須是有效的,或者是在陣列的末端(idx == size())。
該陣列為空時,返回 true。
bool push_back(value: Vector2) 🔗
在末尾插入一個 Vector2。
從陣列中刪除位於索引的元素。
Sets the size of the array. If the array is grown, reserves elements at the end of the array. If the array is shrunk, truncates the array to the new size. Calling resize() once and assigning the new values is faster than adding new elements one by one.
Returns @GlobalScope.OK on success, or one of the following Error constants if this method fails: @GlobalScope.ERR_INVALID_PARAMETER if the size is negative, or @GlobalScope.ERR_OUT_OF_MEMORY if allocations fail. Use size() to find the actual size of the array after resize.
void reverse() 🔗
將陣列中的元素逆序排列。
int rfind(value: Vector2, from: int = -1) const 🔗
逆序搜索陣列。還可以傳遞起始搜索位置索引。如果為負,則起始索引被視為相對於數組的結尾。
注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。
void set(index: int, value: Vector2) 🔗
更改給定索引處的 Vector2。
返回陣列中元素的個數。
PackedVector2Array slice(begin: int, end: int = 2147483647) const 🔗
返回該 PackedVector2Array 的切片,是從 begin(含)到 end(不含)的全新 PackedVector2Array。
begin 和 end 的絕對值會按陣列大小進行限制,所以 end 的預設值會切到陣列大小為止(即 arr.slice(1) 是 arr.slice(1, arr.size()) 的簡寫)。
如果 begin 或 end 為負,則表示相對於陣列的末尾(即 arr.slice(0, -2) 是 arr.slice(0, arr.size() - 2) 的簡寫)。
void sort() 🔗
昇冪排列陣列中的元素。
注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。
PackedByteArray to_byte_array() const 🔗
返回 PackedByteArray,每個向量都被編碼為位元組。
運算子說明
bool operator !=(right: PackedVector2Array) 🔗
如果陣列內容不同,則返回 true。
PackedVector2Array operator *(right: Transform2D) 🔗
Returns a new PackedVector2Array with all vectors in this array inversely transformed (multiplied) by the given Transform2D transformation matrix, under the assumption that the transformation basis is orthonormal (i.e. rotation/reflection is fine, scaling/skew is not).
array * transform is equivalent to transform.inverse() * array. See Transform2D.inverse().
For transforming by inverse of an affine transformation (e.g. with scaling) transform.affine_inverse() * array can be used instead. See Transform2D.affine_inverse().
PackedVector2Array operator +(right: PackedVector2Array) 🔗
返回新的 PackedVector2Array,新陣列的內容為此陣列在末尾加上 right。為了提高性能,請考慮改用 append_array()。
bool operator ==(right: PackedVector2Array) 🔗
如果兩個陣列的內容相同,即對應索引號的 Vector2 相等,則返回 true。
Vector2 operator [](index: int) 🔗
返回索引為 index 的 Vector2。負數索引能從末尾開始存取元素。使用陣列範圍外的索引會導致出錯。