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.

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. The returned packed array of these are a copies, and changing it will not affect the original value. To update a built-in property you need to modify the returned array, and then assign it to the property again.

备注

通过 C# 使用该 API 时会有显著不同,详见 C# API 与 GDScript 的差异

教程

构造函数

PackedVector2Array

PackedVector2Array()

PackedVector2Array

PackedVector2Array(from: PackedVector2Array)

PackedVector2Array

PackedVector2Array(from: Array)

方法

bool

append(value: Vector2)

void

append_array(array: PackedVector2Array)

int

bsearch(value: Vector2, before: bool = true)

void

clear()

int

count(value: Vector2) const

PackedVector2Array

duplicate()

void

fill(value: Vector2)

int

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

Vector2

get(index: int) const

bool

has(value: Vector2) const

int

insert(at_index: int, value: Vector2)

bool

is_empty() const

bool

push_back(value: Vector2)

void

remove_at(index: int)

int

resize(new_size: int)

void

reverse()

int

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

void

set(index: int, value: Vector2)

int

size() const

PackedVector2Array

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

void

sort()

PackedByteArray

to_byte_array() const

运算符

bool

operator !=(right: PackedVector2Array)

PackedVector2Array

operator *(right: Transform2D)

PackedVector2Array

operator +(right: PackedVector2Array)

bool

operator ==(right: PackedVector2Array)

Vector2

operator [](index: int)


构造函数说明

PackedVector2Array PackedVector2Array() 🔗

构造空的 PackedVector2Array


PackedVector2Array PackedVector2Array(from: PackedVector2Array)

构造给定 PackedVector2Array 的副本。


PackedVector2Array PackedVector2Array(from: Array)

构造新的 PackedVector2Array。也可以传入需要转换的通用 Array

注意:使用元素初始化 PackedVector2Array 时,必须使用元素为 Vector2Array 进行初始化:

var array = PackedVector2Array([Vector2(12, 34), Vector2(56, 78)])

方法说明

bool append(value: Vector2) 🔗

向数组末尾追加一个元素(push_back 的别名)。


void append_array(array: PackedVector2Array) 🔗

在该数组的末尾追加一个 PackedVector2Array


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

使用二分法查找已有值的索引(如果该值尚未存在于数组中,则为保持排序顺序的插入索引)。传递 before 说明符是可选的。如果该参数为 false,则返回的索引位于数组中该值的所有已有的条目之后。

注意:在未排序的数组上调用 bsearch 会产生预料之外的行为。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


void clear() 🔗

清空数组。相当于调用 resize 时指定大小为 0


int count(value: Vector2) const 🔗

返回数组中某个元素出现的次数。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


PackedVector2Array duplicate() 🔗

创建该数组的副本,并将该副本返回。


void fill(value: Vector2) 🔗

将数组中的所有元素都设为给定的值。通常与 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. This is the same as using the [] operator (array[index]).


bool has(value: Vector2) const 🔗

如果数组中包含 value,则返回 true

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


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

在数组中给定的位置插入一个新元素。这个位置必须是有效的,或者是在数组的末端(idx == size())。


bool is_empty() const 🔗

该数组为空时,返回 true


bool push_back(value: Vector2) 🔗

在末尾插入一个 Vector2


void remove_at(index: int) 🔗

从数组中删除位于索引的元素。


int resize(new_size: int) 🔗

设置数组的大小。如果数组被增大,则保留数组末端的元素。如果数组被缩小,则将数组截断到新的大小。调用一次 resize 并分配新值比逐个添加新元素要快。


void reverse() 🔗

将数组中的元素逆序排列。


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

逆序搜索数组。还可以传递起始搜索位置索引。如果为负,则起始索引被视为相对于数组的结尾。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


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

更改给定索引处的 Vector2


int size() const 🔗

返回数组中元素的个数。


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

返回该 PackedVector2Array 的切片,是从 begin(含)到 end(不含)的全新 PackedVector2Array

beginend 的绝对值会按数组大小进行限制,所以 end 的默认值会切到数组大小为止(即 arr.slice(1)arr.slice(1, arr.size()) 的简写)。

如果 beginend 为负,则表示相对于数组的末尾(即 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) 🔗

返回一个新的 PackedVector2Array,该数组中的所有向量都通过给定的 Transform2D 变换矩阵进行逆变换(乘以),假设该变换的基是正交的(即旋转/反射可以,缩放/倾斜则不然)。

array * transform 相当于 transform.inverse() * array。请参阅 Transform2D.inverse

对于通过仿射变换的逆进行变换(例如缩放),可以使用 transform.affine_inverse() * array 代替。请参阅 Transform2D.affine_inverse


PackedVector2Array operator +(right: PackedVector2Array) 🔗

返回新的 PackedVector2Array,新数组的内容为此数组在末尾加上 right。为了提高性能,请考虑改用 append_array


bool operator ==(right: PackedVector2Array) 🔗

如果两个数组的内容相同,即对应索引号的 Vector2 相等,则返回 true


Vector2 operator [](index: int) 🔗

返回索引为 indexVector2。负数索引能从末尾开始访问元素。使用数组范围外的索引会导致出错。