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...
PackedInt32Array
32 位元整數緊縮陣列。
說明
An array specifically designed to hold 32-bit integer values. Packs data tightly, so it saves memory for large array sizes.
Note: This type stores signed 32-bit integers, which means it can take values in the interval [-2^31, 2^31 - 1], i.e. [-2147483648, 2147483647]. Exceeding those bounds will wrap around. In comparison, int uses signed 64-bit integers which can hold much larger values. If you need to pack 64-bit integers tightly, see PackedInt64Array.
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 的不同。
建構子
PackedInt32Array(from: PackedInt32Array) |
|
PackedInt32Array(from: Array) |
方法
void |
append_array(array: PackedInt32Array) |
void |
clear() |
duplicate() const |
|
void |
|
is_empty() const |
|
void |
|
void |
reverse() |
void |
|
size() const |
|
void |
sort() |
to_byte_array() const |
運算子
operator !=(right: PackedInt32Array) |
|
operator +(right: PackedInt32Array) |
|
operator ==(right: PackedInt32Array) |
|
operator [](index: int) |
建構子說明
PackedInt32Array PackedInt32Array() 🔗
建構空的 PackedInt32Array。
PackedInt32Array PackedInt32Array(from: PackedInt32Array)
建構給定 PackedInt32Array 的副本。
PackedInt32Array PackedInt32Array(from: Array)
建構新 PackedInt32Array。你還可以傳入通用 Array 進行轉換。
方法說明
向陣列末尾追加一個元素(push_back() 的別名)。
void append_array(array: PackedInt32Array) 🔗
在該陣列的末尾追加一個 PackedInt32Array。
int bsearch(value: int, before: bool = true) const 🔗
使用二進法搜尋已有值的索引(如果該值尚未存在於陣列中,則為保持排序順序的插入索引)。傳遞 before 說明符是可選的。如果該參數為 false,則返回的索引位於陣列中該值的所有已有的條目之後。
注意:在未排序的陣列上呼叫 bsearch() 會產生預料之外的行為。
void clear() 🔗
清空陣列。相當於呼叫 resize() 時指定大小為 0。
返回元素在陣列中出現的次數。
PackedInt32Array 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.
將陣列中的所有元素都設為給定的值。通常與 resize() 一起使用,建立給定大小的陣列並初始化元素。
int find(value: int, from: int = 0) const 🔗
在陣列中搜索值並返回其索引,如果未找到則返回 -1 。可選地,可以傳遞起始搜索索引。
Returns the 32-bit integer at the given index in the array. If index is out-of-bounds or negative, this method fails and returns 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.
如果該陣列包含 value,則返回 true。
int insert(at_index: int, value: int) 🔗
在陣列中的給定位置插入新的整數。位置必須有效,或者位於陣列末尾(idx == size())。
該陣列為空時,返回 true。
將一個值新增到陣列中。
從陣列中刪除位於索引的元素。
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: int, from: int = -1) const 🔗
逆序搜索陣列。還可以傳遞起始搜索位置索引。如果為負,則起始索引被視為相對於數組的結尾。
void set(index: int, value: int) 🔗
更改給定索引處的整數。
返回陣列中元素的個數。
PackedInt32Array slice(begin: int, end: int = 2147483647) const 🔗
返回該 PackedInt32Array 的切片,是從 begin(含)到 end(不含)的全新 PackedInt32Array。
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() 🔗
將該陣列中的元素按昇冪排列。
PackedByteArray to_byte_array() const 🔗
Returns a copy of the data converted to a PackedByteArray, where each element has been encoded as 4 bytes.
The size of the new array will be int32_array.size() * 4.
運算子說明
bool operator !=(right: PackedInt32Array) 🔗
如果陣列內容不同,則返回 true。
PackedInt32Array operator +(right: PackedInt32Array) 🔗
返回新的 PackedInt32Array,新陣列的內容為此陣列在末尾加上 right。為了提高性能,請考慮改用 append_array()。
bool operator ==(right: PackedInt32Array) 🔗
如果兩個陣列的內容相同,即對應索引號的整數相等,則返回 true。
返回索引 index 處的 int。負數索引可以從末尾開始存取元素。使用超出數組範圍的索引會導致出錯。
注意,int 型別為 64 位元,與該陣列中所儲存的值不同。