buffer
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 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.
When passed through Roblox APIs, including sending a buffer through custom events, the identity of the buffer object is not preserved and the target will receive a copy. Similar to other limitations, the same buffer object cannot be used from multiple Actor scripts (Parallel Luau).
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.
Summary
Functions
Creates a buffer.
Creates a buffer from a string.
Converts a buffer to a string.
Returns the size of the buffer in bytes.
Reads an 8-bit signed integer from the buffer.
Reads an 8-bit unsigned integer from the buffer.
Reads a 16-bit signed integer from the buffer.
Reads a 16-bit unsigned integer from the buffer.
Reads a 32-bit signed integer from the buffer.
Reads a 32-bit unsigned integer from the buffer.
Reads a 32-bit floating-point value from the buffer.
Reads a 64-bit floating-point value from the buffer.
Writes an 8-bit signed integer to the buffer.
Writes an 8-bit unsigned integer to the buffer.
Writes a 16-bit signed integer to the buffer.
Writes a 16-bit unsigned integer to the buffer.
Writes a 32-bit signed integer to the buffer.
Writes a 32-bit unsigned integer to the buffer.
Writes a 32-bit floating-point value to the buffer.
Writes a 64-bit floating-point value to the buffer.
Reads a string from the buffer.
Writes a string to the buffer.
Copies bytes between buffers.
Sets a region of the buffer memory to some 8-bit unsigned integer value.
Functions
readf32
Reads the data from the buffer by reinterpreting bytes at the offset as a 32-bit floating-point value and converting it into a number. If the floating-point value matches any bit patterns that represent NaN (not a number), the returned value may be converted to a different quiet NaN representation.
Returns
readf64
Reads the data from the buffer by reinterpreting bytes at the offset as a 64-bit floating-point value and converting it into a number. If the floating-point value matches any bit patterns that represent NaN (not a number), the returned value may be converted to a different quiet NaN representation.
Returns
writei16
Writes data to the buffer by converting the number into a 16-bit signed integer and reinterpreting it as individual bytes.
Parameters
Returns
writei32
Writes data to the buffer by converting the number into a 32-bit signed integer and reinterpreting it as individual bytes.
Parameters
Returns
writeu32
Writes data to the buffer by converting the number into a 32-bit unsigned integer and reinterpreting it as individual bytes.
Parameters
Returns
writef32
Writes data to the buffer by converting the number into a 32-bit floating-point value and reinterpreting it as individual bytes.
Parameters
Returns
writef64
Writes data to the buffer by converting the number into a 64-bit floating-point value and reinterpreting it as individual bytes.
Parameters
Returns
writestring
Writes data from a string into the buffer at the specified offset. If an optional count is specified, only count bytes are taken from the string.
Parameters
Returns
copy
Copies count bytes from source starting at offset sourceOffset into the target at targetOffset.
It's possible for source and target to be the same. Copying an overlapping region inside the same buffer acts as if the source region is copied into a temporary buffer and then that buffer is copied over to the target.
Parameters
Buffer to copy data into.
Offset from the beginning of the buffer memory, starting from 0.
Buffer to take the data from.
Offset from the beginning of the buffer memory, starting from 0.
Number of bytes to copy. If omitted, the whole source data starting from sourceOffset is taken.
Returns
fill
Sets count bytes in the buffer starting at the specified offset to value.