Index · PackedByteArray

PackedByteArray

A packed array of bytes.

Class Description

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

PackedByteArray also provides methods to encode/decode various types to/from bytes. The way values are encoded is an implementation detail and shouldn't be relied upon when interacting with external apps.

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.

Methods

func append(value: int) -> bool

Appends an element at the end of the array (alias of push_back()).

func append_array(array: PackedByteArray) -> void

Appends a PackedByteArray at the end of this array.

func bsearch(value: int, before: bool = true) -> int

Finds the index of an existing value (or the insertion index that maintains sorting order, if the value is not yet present in the array) using binary search. Optionally, a before specifier can be passed. If false, the returned index comes after all existing entries of the value in the array.

Note: Calling bsearch() on an unsorted array results in unexpected behavior.

func bswap16(offset: int = 0, count: int = -1) -> void

Swaps the byte order of count 16-bit segments of the array starting at offset. Swap is done in-place. If count is less than zero, all segments to the end of array are processed, if processed data size is not a multiple of 2, the byte after the last processed 16-bit segment is not modified.

func bswap32(offset: int = 0, count: int = -1) -> void

Swaps the byte order of count 32-bit segments of the array starting at offset. Swap is done in-place. If count is less than zero, all segments to the end of array are processed, if processed data size is not a multiple of 4, bytes after the last processed 32-bit segment are not modified.

func bswap64(offset: int = 0, count: int = -1) -> void

Swaps the byte order of count 64-bit segments of the array starting at offset. Swap is done in-place. If count is less than zero, all segments to the end of array are processed, if processed data size is not a multiple of 8, bytes after the last processed 64-bit segment are not modified.

func clear() -> void

Clears the array. This is equivalent to using resize() with a size of 0.

func compress(compression_mode: int = 0) -> PackedByteArray

Returns a new PackedByteArray with the data compressed. Set the compression mode using one of FileAccess.CompressionMode's constants.

func count(value: int) -> int

Returns the number of times an element is in the array.

func decode_double(byte_offset: int) -> float

Decodes a 64-bit floating-point number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0.0 if a valid number can't be decoded.

func decode_float(byte_offset: int) -> float

Decodes a 32-bit floating-point number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0.0 if a valid number can't be decoded.

func decode_half(byte_offset: int) -> float

Decodes a 16-bit floating-point number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0.0 if a valid number can't be decoded.

func decode_s8(byte_offset: int) -> int

Decodes a 8-bit signed integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_s16(byte_offset: int) -> int

Decodes a 16-bit signed integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_s32(byte_offset: int) -> int

Decodes a 32-bit signed integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_s64(byte_offset: int) -> int

Decodes a 64-bit signed integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_u8(byte_offset: int) -> int

Decodes a 8-bit unsigned integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_u16(byte_offset: int) -> int

Decodes a 16-bit unsigned integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_u32(byte_offset: int) -> int

Decodes a 32-bit unsigned integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_u64(byte_offset: int) -> int

Decodes a 64-bit unsigned integer number from the bytes starting at byte_offset. Fails if the byte count is insufficient. Returns 0 if a valid number can't be decoded.

func decode_var(byte_offset: int, allow_objects: bool = false) -> Variant

Decodes a Variant from the bytes starting at byte_offset. Returns null if a valid variant can't be decoded or the value is Object-derived and allow_objects is false.

func decode_var_size(byte_offset: int, allow_objects: bool = false) -> int

Decodes a size of a Variant from the bytes starting at byte_offset. Requires at least 4 bytes of data starting at the offset, otherwise fails.

func decompress(buffer_size: int, compression_mode: int = 0) -> PackedByteArray

Returns a new PackedByteArray with the data decompressed. Set buffer_size to the size of the uncompressed data. Set the compression mode using one of FileAccess.CompressionMode's constants.

Note: Decompression is not guaranteed to work with data not compressed by Godot, for example if data compressed with the deflate compression mode lacks a checksum or header.

func decompress_dynamic(max_output_size: int, compression_mode: int = 0) -> PackedByteArray

Returns a new PackedByteArray with the data decompressed. Set the compression mode using one of FileAccess.CompressionMode's constants. This method only accepts brotli, gzip, and deflate compression modes.

This method is potentially slower than decompress(), as it may have to re-allocate its output buffer multiple times while decompressing, whereas decompress() knows it's output buffer size from the beginning.

GZIP has a maximal compression ratio of 1032:1, meaning it's very possible for a small compressed payload to decompress to a potentially very large output. To guard against this, you may provide a maximum size this function is allowed to allocate in bytes via max_output_size. Passing -1 will allow for unbounded output. If any positive value is passed, and the decompression exceeds that amount in bytes, then an error will be returned.

Note: Decompression is not guaranteed to work with data not compressed by Godot, for example if data compressed with the deflate compression mode lacks a checksum or header.

func duplicate() -> PackedByteArray

Creates a copy of the array, and returns it.

func encode_double(byte_offset: int, value: float) -> void

Encodes a 64-bit floating-point number as bytes at the index of byte_offset bytes. The array must have at least 8 bytes of allocated space, starting at the offset.

func encode_float(byte_offset: int, value: float) -> void

Encodes a 32-bit floating-point number as bytes at the index of byte_offset bytes. The array must have at least 4 bytes of space, starting at the offset.

func encode_half(byte_offset: int, value: float) -> void

Encodes a 16-bit floating-point number as bytes at the index of byte_offset bytes. The array must have at least 2 bytes of space, starting at the offset.

func encode_s8(byte_offset: int, value: int) -> void

Encodes a 8-bit signed integer number (signed byte) at the index of byte_offset bytes. The array must have at least 1 byte of space, starting at the offset.

func encode_s16(byte_offset: int, value: int) -> void

Encodes a 16-bit signed integer number as bytes at the index of byte_offset bytes. The array must have at least 2 bytes of space, starting at the offset.

func encode_s32(byte_offset: int, value: int) -> void

Encodes a 32-bit signed integer number as bytes at the index of byte_offset bytes. The array must have at least 4 bytes of space, starting at the offset.

func encode_s64(byte_offset: int, value: int) -> void

Encodes a 64-bit signed integer number as bytes at the index of byte_offset bytes. The array must have at least 8 bytes of space, starting at the offset.

func encode_u8(byte_offset: int, value: int) -> void

Encodes a 8-bit unsigned integer number (byte) at the index of byte_offset bytes. The array must have at least 1 byte of space, starting at the offset.

func encode_u16(byte_offset: int, value: int) -> void

Encodes a 16-bit unsigned integer number as bytes at the index of byte_offset bytes. The array must have at least 2 bytes of space, starting at the offset.

func encode_u32(byte_offset: int, value: int) -> void

Encodes a 32-bit unsigned integer number as bytes at the index of byte_offset bytes. The array must have at least 4 bytes of space, starting at the offset.

func encode_u64(byte_offset: int, value: int) -> void

Encodes a 64-bit unsigned integer number as bytes at the index of byte_offset bytes. The array must have at least 8 bytes of space, starting at the offset.

func encode_var(byte_offset: int, value: Variant, allow_objects: bool = false) -> int

Encodes a Variant at the index of byte_offset bytes. A sufficient space must be allocated, depending on the encoded variant's size. If allow_objects is false, Object-derived values are not permitted and will instead be serialized as ID-only.

func erase(value: int) -> bool

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.

func fill(value: int) -> void

Assigns the given value to all elements in the array. This can typically be used together with resize() to create an array with a given size and initialized elements.

func find(value: int, from: int = 0) -> int

Searches the array for a value and returns its index or -1 if not found. Optionally, the initial search index can be passed.

func get(index: int) -> int

Returns the byte 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.

func get_string_from_ascii() -> String

Converts ASCII/Latin-1 encoded array to String. Fast alternative to get_string_from_utf8() if the content is ASCII/Latin-1 only. Unlike the UTF-8 function this function maps every byte to a character in the array. Multibyte sequences will not be interpreted correctly. For parsing user input always use get_string_from_utf8(). This is the inverse of String.to_ascii_buffer().

func get_string_from_multibyte_char(encoding: String = "") -> String

Converts system multibyte code page encoded array to String. If conversion fails, empty string is returned. This is the inverse of String.to_multibyte_char_buffer().

The values permitted for encoding are system dependent. If encoding is empty string, system default encoding is used.

- For Windows, see Code Page Identifiers .NET names.

- For macOS and Linux/BSD, see libiconv library documentation and iconv --list for a list of supported encodings.

func get_string_from_utf8() -> String

Converts UTF-8 encoded array to String. Slower than get_string_from_ascii() but supports UTF-8 encoded data. Use this function if you are unsure about the source of the data. For user input this function should always be preferred. Returns empty string if source array is not valid UTF-8 string. This is the inverse of String.to_utf8_buffer().

func get_string_from_utf16() -> String

Converts UTF-16 encoded array to String. If the BOM is missing, little-endianness is assumed. Returns empty string if source array is not valid UTF-16 string. This is the inverse of String.to_utf16_buffer().

func get_string_from_utf32() -> String

Converts UTF-32 encoded array to String. Returns empty string if source array is not valid UTF-32 string. This is the inverse of String.to_utf32_buffer().

func get_string_from_wchar() -> String

Converts wide character (wchar_t, UTF-16 on Windows, UTF-32 on other platforms) encoded array to String. Returns empty string if source array is not valid wide string. This is the inverse of String.to_wchar_buffer().

func has(value: int) -> bool

Returns true if the array contains value.

func has_encoded_var(byte_offset: int, allow_objects: bool = false) -> bool

Returns true if a valid Variant value can be decoded at the byte_offset. Returns false otherwise or when the value is Object-derived and allow_objects is false.

func hex_encode() -> String

Returns a hexadecimal representation of this array as a String.

var array = PackedByteArray([11, 46, 255])
print(array.hex_encode()) # Prints "0b2eff"
func insert(at_index: int, value: int) -> int

Inserts a new element at a given position in the array. The position must be valid, or at the end of the array (idx == size()).

func is_empty() -> bool

Returns true if the array is empty.

func push_back(value: int) -> bool

Appends an element at the end of the array.

func remove_at(index: int) -> void

Removes an element from the array by index.

func resize(new_size: int) -> 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 OK on success, or one of the following Error constants if this method fails: ERR_INVALID_PARAMETER if the size is negative, or ERR_OUT_OF_MEMORY if allocations fail. Use size() to find the actual size of the array after resize.

func reverse() -> void

Reverses the order of the elements in the array.

func rfind(value: int, from: int = -1) -> int

Searches the array in reverse order. Optionally, a start search index can be passed. If negative, the start index is considered relative to the end of the array.

func set(index: int, value: int) -> void

Changes the byte at the given index.

func size() -> int

Returns the number of elements in the array.

func slice(begin: int, end: int = 2147483647) -> PackedByteArray

Returns the slice of the PackedByteArray, from begin (inclusive) to end (exclusive), as a new PackedByteArray.

The absolute value of begin and end will be clamped to the array size, so the default value for end makes it slice to the size of the array by default (i.e. arr.slice(1) is a shorthand for arr.slice(1, arr.size())).

If either begin or end are negative, they will be relative to the end of the array (i.e. arr.slice(0, -2) is a shorthand for arr.slice(0, arr.size() - 2)).

func sort() -> void

Sorts the elements of the array in ascending order.

func to_color_array() -> PackedColorArray

Returns a copy of the data converted to a PackedColorArray, where each block of 16 bytes has been converted to a Color variant.

Note: The size of the input array must be a multiple of 16 (size of four 32-bit float variables). The size of the new array will be byte_array.size() / 16. If the original data can't be converted to Color variants, the resulting data is undefined.

func to_float32_array() -> PackedFloat32Array

Returns a copy of the data converted to a PackedFloat32Array, where each block of 4 bytes has been converted to a 32-bit float (C++ [code skip-lint]float[/code]).

The size of the input array must be a multiple of 4 (size of 32-bit float). The size of the new array will be byte_array.size() / 4.

If the original data can't be converted to 32-bit floats, the resulting data is undefined.

func to_float64_array() -> PackedFloat64Array

Returns a copy of the data converted to a PackedFloat64Array, where each block of 8 bytes has been converted to a 64-bit float (C++ double, Godot float).

The size of the input array must be a multiple of 8 (size of 64-bit double). The size of the new array will be byte_array.size() / 8.

If the original data can't be converted to 64-bit floats, the resulting data is undefined.

func to_int32_array() -> PackedInt32Array

Returns a copy of the data converted to a PackedInt32Array, where each block of 4 bytes has been converted to a signed 32-bit integer (C++ int32_t).

The size of the input array must be a multiple of 4 (size of 32-bit integer). The size of the new array will be byte_array.size() / 4.

If the original data can't be converted to signed 32-bit integers, the resulting data is undefined.

func to_int64_array() -> PackedInt64Array

Returns a copy of the data converted to a PackedInt64Array, where each block of 8 bytes has been converted to a signed 64-bit integer (C++ int64_t, Godot int).

The size of the input array must be a multiple of 8 (size of 64-bit integer). The size of the new array will be byte_array.size() / 8.

If the original data can't be converted to signed 64-bit integers, the resulting data is undefined.

func to_vector2_array() -> PackedVector2Array

Returns a copy of the data converted to a PackedVector2Array, where each block of 8 bytes or 16 bytes (32-bit or 64-bit) has been converted to a Vector2 variant.

Note: The size of the input array must be a multiple of 8 or 16 (depending on the build settings, see Vector2 for more details). The size of the new array will be byte_array.size() / (8 or 16). If the original data can't be converted to Vector2 variants, the resulting data is undefined.

func to_vector3_array() -> PackedVector3Array

Returns a copy of the data converted to a PackedVector3Array, where each block of 12 or 24 bytes (32-bit or 64-bit) has been converted to a Vector3 variant.

Note: The size of the input array must be a multiple of 12 or 24 (depending on the build settings, see Vector3 for more details). The size of the new array will be byte_array.size() / (12 or 24). If the original data can't be converted to Vector3 variants, the resulting data is undefined.

func to_vector4_array() -> PackedVector4Array

Returns a copy of the data converted to a PackedVector4Array, where each block of 16 or 32 bytes (32-bit or 64-bit) has been converted to a Vector4 variant.

Note: The size of the input array must be a multiple of 16 or 32 (depending on the build settings, see Vector4 for more details). The size of the new array will be byte_array.size() / (16 or 32). If the original data can't be converted to Vector4 variants, the resulting data is undefined.