Buffer luau

From GiderosMobile
Revision as of 08:49, 6 February 2025 by MoKaLux (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform linux.png
Available since: Gideros 2025.1

Description

This is the Luau buffer library implemented in Gideros.

Buffers are not just fast, they also consume less memory. In Luau, every value is 16 bytes long, whatever it is. If you just need an array of bytes, using a buffer instead of a table saves 16x the memory.

A few Gideros calls have been updated to accept buffers as input, since it is also more efficient to use buffer data directly than to grab each array value individually. Specifically, you can now construct textures from buffers and even provide raw texture data, not necessarily RGBA

A buffer is an object that represents a fixed-size mutable block of memory. The buffer library provides functions for creation and manipulation of buffer objects, providing all its functions inside the global buffer variable.

Buffer is intended to be used as a low-level binary data storage structure, replacing the uses of string.pack() and string.unpack(). Use cases include reading and writing existing binary formats, working with data in a more compact form, serialization to custom binary formats, and general work with native memory types like fixed-length integers and floats.

Many of the functions accept an offset in bytes from the start of the buffer. Offset of 0 from the start of the buffer memory block accesses the first byte. All offsets, counts and sizes should be non-negative integer numbers. If the bytes that are accessed by any read or write operation are outside the buffer memory, an error is thrown.

The read and write methods that work with integers and floats use little-endian encoding.

Example

-- creates a buffer initialized to the contents of the string
local str = "Hello Gideros!"
b = buffer.fromstring(str)
print(buffer.len(b))
-- returns the buffer data as a string
print(buffer.tostring(b))

Disclaimer

Documentation is based on create.roblox.com documentation.

https://luau.org/library#buffer-library
https://create.roblox.com/docs/reference/engine/libraries/buffer

Methods

buffer.copy copies bytes between buffers
buffer.create creates a buffer
buffer.extract extracts from a buffer
buffer.fill sets a region of the buffer memory to some 8-bit unsigned integer value
buffer.fromstring creates a buffer from a string
buffer.len returns the size of the buffer in bytes
buffer.read reads from the buffer
buffer.readstring reads a string from the buffer
buffer.resize resizes the buffer
buffer.setarrayaccess sets the buffer array access
buffer.tostring converts a buffer to a string
buffer.write writes to the buffer
buffer.writestring writes a string to the buffer

Events

Constants