Buffer luau

From GiderosMobile
Revision as of 01:55, 6 February 2025 by MoKaLux (talk | contribs) (wip)

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.

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.create creates a buffer
buffer.fromstring creates a buffer from a string
buffer.tostring converts a buffer to a string
buffer.len returns the size of the buffer in bytes
buffer.read reads from the buffer
buffer.write writes to the buffer

Events

Constants