> For the complete documentation index, see [llms.txt](https://docs.perfectcs2.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.perfectcs2.com/documentacao/callbacks.md).

# Callbacks

Funções chamadas a cada frame, tick ou mudança no menu.

```lua
callbacks.add(name, fn)
callbacks.remove(name, fn)
```

Registra uma função para rodar quando algo acontece. Um script pode registrar várias funções para o mesmo callback, e elas rodam na ordem em que foram adicionadas. `remove` precisa da **mesma** função que foi passada no `add`.

| Nome            | Quando roda                                                                                                   | Argumentos                                      |
| --------------- | ------------------------------------------------------------------------------------------------------------- | ----------------------------------------------- |
| `"draw"`        | A cada frame. É o único lugar onde [`render`](/documentacao/api/render.md) funciona.                          | —                                               |
| `"create_move"` | A cada comando que o jogo envia (64 por segundo). Permite mudar movimento, mira e botões.                     | [`cmd`](/documentacao/callbacks/create-move.md) |
| `"frame_stage"` | Em cada etapa do frame do jogo                                                                                | `stage` (número)                                |
| `"menu_open"`   | Quando o menu abre                                                                                            | —                                               |
| `"menu_close"`  | Quando o menu fecha                                                                                           | —                                               |
| `"unload"`      | Quando o script vai parar: desligado, recarregado ou o Perfect fechando. Não roda se o script parou por erro. | —                                               |

Um nome que não está na lista dá erro. Eventos do jogo, como kills e rodadas, usam [`events.on`](/documentacao/events.md).

```lua
local function on_draw()
    render.text(10, 10, "on", color(0, 255, 0))
end

callbacks.add("draw", on_draw)

callbacks.add("menu_close", function()
    callbacks.remove("draw", on_draw) -- stops drawing once the menu closes
end)
```

{% hint style="danger" %}
Um erro dentro de qualquer callback para o script inteiro. Cada chamada tem até 10 ms (veja [Limites](/comecando/limites.md)).
{% 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.perfectcs2.com/documentacao/callbacks.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.
