Struct godot::builtin::PackedVector2Array
pub struct PackedVector2Array { /* private fields */ }
Expand description
Implements Godot’s PackedVector2Array
type,
which is an efficient array of Vector2
s.
Note that, unlike Array
, this type has value semantics: each copy will be independent
of the original. Under the hood, Godot uses copy-on-write, so copies are still cheap
to make.
§Registering properties
You can use both #[var]
and #[export]
with packed arrays. However, since they use copy-on-write, GDScript (for #[var]
) and the
editor (for #[export]
) will effectively keep an independent copy of the array. Writes to the packed array from Rust are thus not
reflected on the other side – you may need to replace the entire array.
See also #godot/76150 for details.
§Thread safety
Usage is safe if the PackedVector2Array
is used on a single thread only. Concurrent reads on different threads are also safe,
but any writes must be externally synchronized. The Rust compiler will enforce this as
long as you use only Rust threads, but it cannot protect against concurrent modification
on other threads (e.g. created through GDScript).
Implementations§
§impl PackedVector2Array
impl PackedVector2Array
pub fn new() -> PackedVector2Array
pub fn new() -> PackedVector2Array
Constructs an empty array.
pub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the array. Equivalent of size()
in Godot.
pub fn to_vec(&self) -> Vec<Vector2>
pub fn to_vec(&self) -> Vec<Vector2>
Converts this array to a Rust vector, making a copy of its contents.
pub fn clear(&mut self)
pub fn clear(&mut self)
Clears the array, removing all elements.
pub fn resize(&mut self, size: usize)
pub fn resize(&mut self, size: usize)
Resizes the array to contain a different number of elements. If the new size is
smaller, elements are removed from the end. If the new size is larger, new elements
are set to Default::default()
.
pub fn subarray(&self, begin: usize, end: usize) -> PackedVector2Array
pub fn subarray(&self, begin: usize, end: usize) -> PackedVector2Array
Returns a sub-range begin..end
, as a new packed array.
This method is called slice()
in Godot.
The values of begin
(inclusive) and end
(exclusive) will be clamped to the array size.
To obtain Rust slices, see as_slice
and as_mut_slice
.
pub fn as_slice(&self) -> &[Vector2]
pub fn as_slice(&self) -> &[Vector2]
Returns a shared Rust slice of the array.
The resulting slice can be further subdivided or converted into raw pointers.
See also as_mut_slice
to get exclusive slices, and
subarray
to get a sub-array as a copy.
pub fn as_mut_slice(&mut self) -> &mut [Vector2]
pub fn as_mut_slice(&mut self) -> &mut [Vector2]
pub fn binary_search(&self, value: Vector2) -> usize
pub fn binary_search(&self, value: Vector2) -> usize
Finds the index of an existing value in a sorted array using binary search.
Equivalent of bsearch
in GDScript.
If the value is not present in the array, returns the insertion index that would maintain sorting order.
Calling binary_search
on an unsorted array results in unspecified behavior.
pub fn contains(&self, value: Vector2) -> bool
pub fn contains(&self, value: Vector2) -> bool
Returns true
if the array contains the given value. Equivalent of has
in
GDScript.
pub fn find(&self, value: Vector2, from: Option<usize>) -> Option<usize>
pub fn find(&self, value: Vector2, from: Option<usize>) -> Option<usize>
Searches the array for the first occurrence of a value and returns its index, or
None
if not found. Starts searching at index from
; pass None
to search the
entire array.
pub fn rfind(&self, value: Vector2, from: Option<usize>) -> Option<usize>
pub fn rfind(&self, value: Vector2, from: Option<usize>) -> Option<usize>
Searches the array backwards for the last occurrence of a value and returns its
index, or None
if not found. Starts searching at index from
; pass None
to
search the entire array.
pub fn push(&mut self, value: Vector2)
pub fn push(&mut self, value: Vector2)
Appends an element to the end of the array. Equivalent of append
and push_back
in GDScript.
pub fn insert(&mut self, index: usize, value: Vector2)
pub fn insert(&mut self, index: usize, value: Vector2)
Inserts a new element at a given index in the array. The index must be valid, or at
the end of the array (index == len()
).
Note: On large arrays, this method is much slower than push
as it will move all
the array’s elements after the inserted element. The larger the array, the slower
insert
will be.
pub fn remove(&mut self, index: usize) -> Vector2
pub fn remove(&mut self, index: usize) -> Vector2
Removes and returns the element at the specified index. Similar to remove_at
in
GDScript, but also returns the removed value.
On large arrays, this method is much slower than pop_back
as it will move all the array’s
elements after the removed element. The larger the array, the slower remove
will be.
§Panics
If index
is out of bounds.
pub fn fill(&mut self, value: Vector2)
pub fn fill(&mut self, value: Vector2)
Assigns the given value to all elements in the array. This can be used together
with resize
to create an array with a given size and initialized elements.
pub fn extend_array(&mut self, other: &PackedVector2Array)
pub fn extend_array(&mut self, other: &PackedVector2Array)
Appends another array at the end of this array. Equivalent of append_array
in
GDScript.
pub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses the order of the elements in the array.
pub fn sort(&mut self)
pub fn sort(&mut self)
Sorts the elements of the array in ascending order.
pub fn to_byte_array(&self) -> PackedByteArray
pub fn to_byte_array(&self) -> PackedByteArray
Returns a PackedByteArray
with each value encoded as bytes.
Trait Implementations§
§impl Clone for PackedVector2Array
impl Clone for PackedVector2Array
§fn clone(&self) -> PackedVector2Array
fn clone(&self) -> PackedVector2Array
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl Debug for PackedVector2Array
impl Debug for PackedVector2Array
§impl Default for PackedVector2Array
impl Default for PackedVector2Array
§fn default() -> PackedVector2Array
fn default() -> PackedVector2Array
§impl Display for PackedVector2Array
impl Display for PackedVector2Array
§impl Drop for PackedVector2Array
impl Drop for PackedVector2Array
§impl Export for PackedVector2Array
impl Export for PackedVector2Array
§fn default_export_info() -> PropertyHintInfo
fn default_export_info() -> PropertyHintInfo
§impl Extend<Vector2> for PackedVector2Array
impl Extend<Vector2> for PackedVector2Array
Extends aPackedVector2Array
with the contents of an iterator
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Vector2>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Vector2>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl From<&[Vector2]> for PackedVector2Array
impl From<&[Vector2]> for PackedVector2Array
Creates a PackedVector2Array
from the given slice.
§fn from(slice: &[Vector2]) -> PackedVector2Array
fn from(slice: &[Vector2]) -> PackedVector2Array
§impl<const N: usize> From<&[Vector2; N]> for PackedVector2Array
impl<const N: usize> From<&[Vector2; N]> for PackedVector2Array
Creates a PackedVector2Array
from the given Rust array.
§fn from(arr: &[Vector2; N]) -> PackedVector2Array
fn from(arr: &[Vector2; N]) -> PackedVector2Array
§impl From<&Array<Variant>> for PackedVector2Array
impl From<&Array<Variant>> for PackedVector2Array
§fn from(other: &Array<Variant>) -> PackedVector2Array
fn from(other: &Array<Variant>) -> PackedVector2Array
§impl From<&PackedVector2Array> for Array<Variant>
impl From<&PackedVector2Array> for Array<Variant>
§fn from(other: &PackedVector2Array) -> Array<Variant>
fn from(other: &PackedVector2Array) -> Array<Variant>
§impl FromGodot for PackedVector2Array
impl FromGodot for PackedVector2Array
§fn try_from_godot(
via: <PackedVector2Array as GodotConvert>::Via
) -> Result<PackedVector2Array, ConvertError>
fn try_from_godot( via: <PackedVector2Array as GodotConvert>::Via ) -> Result<PackedVector2Array, ConvertError>
§fn from_godot(via: Self::Via) -> Self
fn from_godot(via: Self::Via) -> Self
§fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
Variant
.§fn from_variant(variant: &Variant) -> Self
fn from_variant(variant: &Variant) -> Self
§impl FromIterator<Vector2> for PackedVector2Array
impl FromIterator<Vector2> for PackedVector2Array
Creates a PackedVector2Array
from an iterator.
§fn from_iter<I>(iter: I) -> PackedVector2Arraywhere
I: IntoIterator<Item = Vector2>,
fn from_iter<I>(iter: I) -> PackedVector2Arraywhere
I: IntoIterator<Item = Vector2>,
§impl GodotConvert for PackedVector2Array
impl GodotConvert for PackedVector2Array
§type Via = PackedVector2Array
type Via = PackedVector2Array
Self
is represented in Godot.§impl PartialEq for PackedVector2Array
impl PartialEq for PackedVector2Array
§fn eq(&self, other: &PackedVector2Array) -> bool
fn eq(&self, other: &PackedVector2Array) -> bool
self
and other
values to be equal, and is used
by ==
.