buffer

Show Deprecated

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

Functions

create

Creates a buffer of the requested size with all bytes initialized to 0. Size limit is 1 GiB, or 1,073,741,824 bytes. Keep in mind that larger buffers might fail to allocate if device is running low on memory.

Parameters

size: number

Size of the buffer.

Returns

fromstring

Creates a buffer initialized to the contents of the string. The size of the buffer equals the length of the string.

Parameters

str: string

Returns

tostring

Returns the buffer data as a string.

Parameters

Returns

Returns the size of the buffer in bytes.

Parameters

Returns

readi8

Reads the data from the buffer by reinterpreting bytes at the offset as an 8-bit signed integer and converting it into a number.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

Returns

readu8

Reads the data from the buffer by reinterpreting bytes at the offset as an 8-bit unsigned integer and converting it into a number.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

Returns

readi16

Reads the data from the buffer by reinterpreting bytes at the offset as a 16-bit signed integer and converting it into a number.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

Returns

readu16

Reads the data from the buffer by reinterpreting bytes at the offset as a 16-bit unsigned integer and converting it into a number.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

Returns

readi32

Reads the data from the buffer by reinterpreting bytes at the offset as a 32-bit signed integer and converting it into a number.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

Returns

readu32

Reads the data from the buffer by reinterpreting bytes at the offset as a 32-bit unsigned integer and converting it into a number.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

Returns

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.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

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.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

Returns

writei8

void

Writes data to the buffer by converting the number into an 8-bit signed integer and writing a single byte.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

An integer number in range [-128, 127].

Returns

void

writeu8

void

Writes data to the buffer by converting the number into an 8-bit unsigned integer and writing a single byte.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

An integer number in range [0, 255].

Returns

void

writei16

void

Writes data to the buffer by converting the number into a 16-bit signed integer and reinterpreting it as individual bytes.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

An integer number in range [-32,768, 32,767].

Returns

void

writeu16

void

Writes data to the buffer by converting the number into a 16-bit unsigned integer and reinterpreting it as individual bytes.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

An integer number in range [0, 65,535].

Returns

void

writei32

void

Writes data to the buffer by converting the number into a 32-bit signed integer and reinterpreting it as individual bytes.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

An integer number in range [-2,147,483,648, 2,147,483,647].

Returns

void

writeu32

void

Writes data to the buffer by converting the number into a 32-bit unsigned integer and reinterpreting it as individual bytes.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

An integer number in range [0, 4,294,967,295].

Returns

void

writef32

void

Writes data to the buffer by converting the number into a 32-bit floating-point value and reinterpreting it as individual bytes.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

A single-precision floating-point number.

Returns

void

writef64

void

Writes data to the buffer by converting the number into a 64-bit floating-point value and reinterpreting it as individual bytes.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

A double-precision floating-point number.

Returns

void

readstring

Reads a string of length count from the buffer at the specified offset.

Parameters

offset: number

Offset from the beginning of the buffer memory, starting from 0.

count: number

Length to read.

Returns

writestring

void

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

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: string

Data to write.

count: number

Number of bytes to take from the string. This value cannot be larger than the string length.

Returns

void

copy

void

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

target: buffer

Buffer to copy data into.

targetOffset: number

Offset from the beginning of the buffer memory, starting from 0.

source: buffer

Buffer to take the data from.

sourceOffset: number

Offset from the beginning of the buffer memory, starting from 0.

Default Value: 0
count: number

Number of bytes to copy. If omitted, the whole source data starting from sourceOffset is taken.

Returns

void

fill

void

Sets count bytes in the buffer starting at the specified offset to value.

Parameters

Buffer to write the data into.

offset: number

Offset from the beginning of the buffer memory, starting from 0.

value: number

An integer number in range [0, 255].

count: number

Number of bytes to write. If omitted, all bytes after the specified offset are set.

Returns

void