Question: What is a buffer in GameMaker?


Buffers in GameMaker are a way of storing binary data in a block of memory, which could be used for various purposes such as saving/loading files, sending/receiving data over a network, or manipulating large arrays of data more efficiently than with standard arrays.

A buffer can hold different types of data (like strings, integers, and floats) and allows you to control how that data is read and written on a byte-by-byte level, which is useful when dealing with file formats or protocols that GameMaker doesn't natively support.

Here's a basic example of creating a buffer, writing some data to it, and then reading it back:

// Create a new buffer var buffer = buffer_create(256, buffer_fixed, 1); // Write some data to the buffer at the start buffer_seek(buffer, buffer_seek_start, 0); buffer_write(buffer, buffer_u8, 255); // an unsigned 8-bit integer buffer_write(buffer, buffer_f32, 3.14159); // a 32-bit floating point number // Read the data back from the buffer buffer_seek(buffer, buffer_seek_start, 0); var myByte = buffer_read(buffer, buffer_u8); var myFloat = buffer_read(buffer, buffer_f32); // Always remember to delete the buffer when done to avoid memory leaks buffer_delete(buffer);

In this example, we created a fixed-size buffer with a capacity of 256 bytes, wrote an unsigned 8-bit integer and a 32-bit float to it, and then read those values back. Notice how we use buffer_seek to set the read/write position in the buffer before performing operations.

Buffer functions include buffer_create, buffer_delete, buffer_read, buffer_write, buffer_seek, and many others to manipulate the data within.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.