Buffer
Getting Started
Module : Buffer
Overview
The Buffer library is a powerful extension of Roblox's native buffer type that adds new data types and functionality for binary data manipulation. This library is particularly useful for data serialization, network communication, and optimized storage.
- 24-bit interger (3 byte)
- 40-bit interger (5 byte)
- 48-bit interger (6 byte)
- 54-bit interger (7 byte)
- 16-bit float (2 byte)
you will discover everything about it in this documentation.
Constructor API
Lets write a create a simple buffer from the library
with the created BufferComponent Object you can write or read any type of value.
.from accept a buffer and return a new BufferComponent Object please note that all data inside are not lost.
.fromString accept a string and return a new BufferComponent Object please note that all data inside the converted string are not lost.
now that you know how to create a BufferComponent Object let me show how to use it.
Allocating Memory
from the created BufferComponent Object you can allocate memory when needed.
this will allocate new memory space to buffer for exemple if the buffer has a size of 10 and you allocate 5 the new buffer size will be 15 bytes
Writing Data
for writing data inside the buffer i recommend you to look at the Constants module inside the BufferModule to look at min and max interger value
this will write a Signed Interger 24-bit (3 byte) to the buffer
other type exist for writing string such as:
- String8 - (max 8 len)
- String16 - (max 16 len)
- String32 - (max 32 len)
- String64 - (max 64 len)
please note that these function need a len argument after passing the string to write.
Component API
Write Methods
Read Methods
Utility Methods
allocate(size: number)
Allocates additional memory space to the buffer.
GetOffset()
Returns the current write cursor position.
GetInstanceOffset()
Returns the current position in the instance buffer.
GetBuffer()
Returns the underlying native buffer.
GetInstanceBuffer()
Returns the array of stored instances.
GetBufferSize()
Returns the total size of the buffer.
clear()
Resets the buffer while maintaining its size.
Destroy()
Destroys the BufferComponent and frees memory.
Usages
Player Data Serialization
Reading Serialized Data
CFrame Compression
Supported Data Types
Signed Integers
- I8: -128 to 127 (1 byte)
- I16: -32,768 to 32,767 (2 bytes)
- I24: -8,388,608 to 8,388,607 (3 bytes)
- I32: -2,147,483,648 to 2,147,483,647 (4 bytes)
- I40: -549,755,813,888 to 549,755,813,887 (5 bytes)
- I48: -140,737,488,355,328 to 140,737,488,355,327 (6 bytes)
- I54: -9,007,199,254,740,992 to 9,007,199,254,740,991 (7 bytes)
Unsigned Integers
- U1: 0 to 1 (1 byte)
- U8: 0 to 255 (1 byte)
- U16: 0 to 65,535 (2 bytes)
- U24: 0 to 16,777,215 (3 bytes)
- U32: 0 to 4,294,967,295 (4 bytes)
- U40: 0 to 1,099,511,627,775 (5 bytes)
- U48: 0 to 281,474,976,710,655 (6 bytes)
- U54: 0 to 18,014,398,509,481,984 (7 bytes)
Roblox Types
- Vector2 (16 bytes)
- Vector3 (24 bytes)
- Vector2int16 (4 bytes)
- Vector3int16 (6 bytes)
- CFrame (92 bytes)
- LossyCFrame (48 bytes) - Compressed version with precision loss
- UDim (8 bytes)
- UDim2 (16 bytes)
- Color3 (12 bytes)
- Rect (32 bytes)
- Region3 (116 bytes)
- Region3int16 (12 bytes)
Other Types
- String (variable length)
- String8/16/32/64 (limited length)
- Bool1 (1 byte)
- Bool8 (1 byte for 8 booleans)
- Instance (stored separately)
Important Notes
-
Memory Management: Numeric values are automatically clamped to the min/max values of the type.
-
Offset: The offset is automatically incremented after each write. For reading, you can specify a custom offset.
-
Instances: Instances are stored in a separate buffer and are not serialized in the main buffer.
-
Performance: Custom types (I24, I40, I48, I54, etc.) are less performant than native types as they require multiple bitwise operations.
-
Precision: F16 and LossyCFrame sacrifice precision to save space.
License
See the LICENSE file for information about using and redistributing this library.