> For the complete documentation index, see [llms.txt](https://docs.gamesense.biz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gamesense.biz/api-reference/ffi.md).

# FFI

Work with typed binary buffers.

The built-in `ffi` module provides typed buffers for reading and writing binary data.

Load it using `require("ffi")`.

## ffi.buffer

`ffi.buffer(size: integer): ffi_buffer`

Allocates a buffer of `size` bytes.

```lua
local ffi = require("ffi")
local data = ffi.buffer(8)

data:set_u32(0, 256)
local value = data:get_u32(0)
```

## ffi\_buffer:size

`buffer:size(): integer`

Returns the buffer size in bytes.

## Reading and writing values

`get_*` methods read values; `set_*` methods write them. Offsets are **zero-based byte positions**.

| Type   | Read               | Write                     |
| ------ | ------------------ | ------------------------- |
| UInt8  | `:get_u8(offset)`  | `:set_u8(offset, value)`  |
| Int8   | `:get_i8(offset)`  | `:set_i8(offset, value)`  |
| UInt16 | `:get_u16(offset)` | `:set_u16(offset, value)` |
| Int16  | `:get_i16(offset)` | `:set_i16(offset, value)` |
| UInt32 | `:get_u32(offset)` | `:set_u32(offset, value)` |
| Int32  | `:get_i32(offset)` | `:set_i32(offset, value)` |
| Int64  | `:get_i64(offset)` | `:set_i64(offset, value)` |
| Float  | `:get_f32(offset)` | `:set_f32(offset, value)` |
| Double | `:get_f64(offset)` | `:set_f64(offset, value)` |

```lua
local buffer = require("ffi").buffer(8)
buffer:set_i32(0, -50)
buffer:set_f32(4, 1.25)

local count = buffer:get_i32(0)
local multiplier = buffer:get_f32(4)
```

Out-of-bounds accesses raise an error. The default maximum buffer size is **1 MiB**.

{% hint style="warning" %}
This is a restricted built-in FFI implementation, **not LuaJIT FFI**. Methods such as `ffi.cdef` and `ffi.cast` are unavailable.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.gamesense.biz/api-reference/ffi.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
