> 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/pt-br/api-reference/ffi.md).

# FFI

Leitura e gravação de buffers binários tipados.

O módulo `ffi` integrado fornece buffers tipados para leitura e gravação de dados binários.

O módulo é carregado com `require("ffi")`.

## ffi.buffer

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

Aloca um buffer de `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`

Retorna o tamanho do buffer em bytes.

## Leitura e gravação

Métodos `get_*` leem valores; métodos `set_*` gravam valores. Os deslocamentos são **posições em bytes, começando em zero**.

| Tipo   | Leitura            | Gravação                  |
| ------ | ------------------ | ------------------------- |
| 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)
```

Acessos fora dos limites do buffer causam erros. O tamanho máximo padrão de um buffer é **1 MiB**.

{% hint style="warning" %}
Esta é uma implementação FFI integrada e restrita, **não o LuaJIT FFI**. Funções como `ffi.cdef` e `ffi.cast` não estão disponíveis.
{% 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/pt-br/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.
