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

# FFI

Типизированные буферы и работа с двоичными данными.

`ffi` предоставляет типизированные буферы для чтения и записи бинарных данных. Библиотека подключается через `require("ffi")`.

## ffi.buffer

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

Создаёт буфер размером `size` байт.

```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`

Возвращает размер буфера в байтах.

## Чтение и запись

`get_*` читает значение, `set_*` записывает. Смещение `offset` задаётся **в байтах, с нуля**.

| Тип    | Чтение             | Запись                    |
| ------ | ------------------ | ------------------------- |
| 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)
```

Выход за границы буфера приводит к ошибке. Максимальный размер одного буфера по умолчанию — **1 MiB**.

**Важно:** это встроенный FFI API с безопасными буферами, а не стандартный LuaJIT FFI. Функции `ffi.cdef` и `ffi.cast` здесь недоступны.


---

# 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/ru/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.
