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.

PackedColorArray

Color 緊縮陣列。

說明

An array specifically designed to hold Color. 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. PackedColorArray versus Array[Color]). 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 的不同

建構子

PackedColorArray

PackedColorArray()

PackedColorArray

PackedColorArray(from: PackedColorArray)

PackedColorArray

PackedColorArray(from: Array)

方法

bool

append(value: Color)

void

append_array(array: PackedColorArray)

int

bsearch(value: Color, before: bool = true) const

void

clear()

int

count(value: Color) const

PackedColorArray

duplicate() const

bool

erase(value: Color)

void

fill(value: Color)

int

find(value: Color, from: int = 0) const

Color

get(index: int) const

bool

has(value: Color) const

int

insert(at_index: int, value: Color)

bool

is_empty() const

bool

push_back(value: Color)

void

remove_at(index: int)

int

resize(new_size: int)

void

reverse()

int

rfind(value: Color, from: int = -1) const

void

set(index: int, value: Color)

int

size() const

PackedColorArray

slice(begin: int, end: int = 2147483647) const

void

sort()

PackedByteArray

to_byte_array() const

運算子

bool

operator !=(right: PackedColorArray)

PackedColorArray

operator +(right: PackedColorArray)

bool

operator ==(right: PackedColorArray)

Color

operator [](index: int)


建構子說明

PackedColorArray PackedColorArray() 🔗

建構空的 PackedColorArray


PackedColorArray PackedColorArray(from: PackedColorArray)

建構給定 PackedColorArray 的副本。


PackedColorArray PackedColorArray(from: Array)

建構新的 PackedColorArray。你也可以傳一個通用 Array 進行轉換。

注意:使用元素初始化 PackedColorArray 時,必須使用元素為 ColorArray

var array = PackedColorArray([Color(0.1, 0.2, 0.3), Color(0.4, 0.5, 0.6)])

方法說明

bool append(value: Color) 🔗

向陣列末尾追加一個元素(push_back() 的別名)。


void append_array(array: PackedColorArray) 🔗

在該陣列的末尾追加一個 PackedColorArray


int bsearch(value: Color, before: bool = true) const 🔗

使用二進法搜尋已有值的索引(如果該值尚未存在於陣列中,則為保持排序順序的插入索引)。傳遞 before 說明符是可選的。如果該參數為 false,則返回的索引位於陣列中該值的所有已有的條目之後。

注意:在未排序的陣列上呼叫 bsearch() 會產生預料之外的行為。


void clear() 🔗

清空陣列。相當於呼叫 resize() 時指定大小為 0


int count(value: Color) const 🔗

返回元素在陣列中出現的次數。


PackedColorArray duplicate() const 🔗

建立該陣列的副本,並將該副本返回。


bool erase(value: Color) 🔗

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.


void fill(value: Color) 🔗

將陣列中的所有元素都設為給定的值。通常與 resize() 一起使用,建立給定大小的陣列並初始化元素。


int find(value: Color, from: int = 0) const 🔗

在陣列中搜索值並返回其索引,如果未找到則返回 -1 。可選地,可以傳遞起始搜索索引。


Color get(index: int) const 🔗

Returns the Color at the given index in the array. If index is out-of-bounds or negative, this method fails and returns Color(0, 0, 0, 1).

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: Color) const 🔗

如果該陣列包含 value,則返回 true


int insert(at_index: int, value: Color) 🔗

在陣列中給定的位置插入一個新元素。這個位置必須是有效的,或者是在陣列的末端(idx == size())。


bool is_empty() const 🔗

該陣列為空時,返回 true


bool push_back(value: Color) 🔗

將一個值新增到陣列中。


void remove_at(index: int) 🔗

從陣列中刪除位於索引的元素。


int resize(new_size: int) 🔗

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: Color, from: int = -1) const 🔗

逆序搜索陣列。還可以傳遞起始搜索位置索引。如果為負,則起始索引被視為相對於數組的結尾。


void set(index: int, value: Color) 🔗

更改給定索引處的 Color


int size() const 🔗

返回陣列中元素的個數。


PackedColorArray slice(begin: int, end: int = 2147483647) const 🔗

返回該 PackedColorArray 的切片,是從 begin(含)到 end(不含)的全新 PackedColorArray

beginend 的絕對值會按陣列大小進行限制,所以 end 的預設值會切到陣列大小為止(即 arr.slice(1)arr.slice(1, arr.size()) 的簡寫)。

如果 beginend 為負,則表示相對於陣列的末尾(即 arr.slice(0, -2)arr.slice(0, arr.size() - 2) 的簡寫)。


void sort() 🔗

將該陣列中的元素按昇冪排列。


PackedByteArray to_byte_array() const 🔗

返回 PackedByteArray,每個顏色都被編碼為位元組。


運算子說明

bool operator !=(right: PackedColorArray) 🔗

如果陣列內容不同,則返回 true


PackedColorArray operator +(right: PackedColorArray) 🔗

返回新的 PackedColorArray,新陣列的內容為此陣列在末尾加上 right。為了提高性能,請考慮改用 append_array()


bool operator ==(right: PackedColorArray) 🔗

如果兩個陣列的內容相同,即對應索引號的 Color 相等,則返回 true


Color operator [](index: int) 🔗

返回索引 index 處的Color。負數索引可以從末端開始存取元素。使用超出陣列範圍的索引將導致出錯。