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.

PackedStringArray

String 緊縮陣列。

說明

An array specifically designed to hold Strings. Packs data tightly, so it saves memory for large array sizes.

If you want to join the strings in the array, use String.join().

var string_array = PackedStringArray(["hello", "world"])
var string = " ".join(string_array)
print(string) # "hello world"

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. PackedStringArray versus Array[String]). 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 的不同

教學

建構子

PackedStringArray

PackedStringArray()

PackedStringArray

PackedStringArray(from: PackedStringArray)

PackedStringArray

PackedStringArray(from: Array)

方法

bool

append(value: String)

void

append_array(array: PackedStringArray)

int

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

void

clear()

int

count(value: String) const

PackedStringArray

duplicate() const

bool

erase(value: String)

void

fill(value: String)

int

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

String

get(index: int) const

bool

has(value: String) const

int

insert(at_index: int, value: String)

bool

is_empty() const

bool

push_back(value: String)

void

remove_at(index: int)

int

resize(new_size: int)

void

reverse()

int

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

void

set(index: int, value: String)

int

size() const

PackedStringArray

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

void

sort()

PackedByteArray

to_byte_array() const

運算子

bool

operator !=(right: PackedStringArray)

PackedStringArray

operator +(right: PackedStringArray)

bool

operator ==(right: PackedStringArray)

String

operator [](index: int)


建構子說明

PackedStringArray PackedStringArray() 🔗

建構空的 PackedStringArray


PackedStringArray PackedStringArray(from: PackedStringArray)

建構給定 PackedStringArray 的副本。


PackedStringArray PackedStringArray(from: Array)

建構新 PackedStringArray。你還可以傳入通用 Array 進行轉換。


方法說明

bool append(value: String) 🔗

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


void append_array(array: PackedStringArray) 🔗

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


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

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

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


void clear() 🔗

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


int count(value: String) const 🔗

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


PackedStringArray duplicate() const 🔗

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


bool erase(value: String) 🔗

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: String) 🔗

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


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

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


String get(index: int) const 🔗

Returns the String at the given index in the array. If index is out-of-bounds or negative, this method fails and returns an empty string.

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

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


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

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


bool is_empty() const 🔗

該陣列為空時,返回 true


bool push_back(value: String) 🔗

在陣列的末尾追加字串元素。


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

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


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

更改給定索引處的 String


int size() const 🔗

返回陣列中元素的個數。


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

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

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 🔗

Returns a PackedByteArray with each string encoded as UTF-8. Strings are null terminated.


運算子說明

bool operator !=(right: PackedStringArray) 🔗

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


PackedStringArray operator +(right: PackedStringArray) 🔗

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


bool operator ==(right: PackedStringArray) 🔗

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


String operator [](index: int) 🔗

返回索引為 indexString。負數索引能從末尾開始存取元素。使用陣列範圍外的索引會導致出錯。