# Events

Register a function to run on an event with `client.set_event_callback`. The callback gets one table argument. Unknown event names raise an error at registration.

```lua
client.set_event_callback("paint", function(e)
  -- e is the event's table argument
end)
```

Remove it again with `client.unset_event_callback(name, fn)`. Register callbacks from the top level so each closure captures the script's own upvalues.

Every game adds its own events on top of these; see the game's page under **Games**.

## List of base events:

#### paint

Fired once per frame, on the game thread. The only event `renderer.*` may draw in. Runs on a small time and instruction budget.

**Examples:**

```lua
client.set_event_callback("paint", function()
  renderer.text(15, 15, 255, 255, 255, 255, nil, 0, "hello world")
end)
```

#### shutdown

Fired when the script is unloading, before its Lua state is closed. Use it to persist state or clear anything you set up. An error here is logged and the script unloads anyway.

**Examples:**

```lua
client.set_event_callback("shutdown", function()
  database.write("last_seen", client.unix_time())
end)
```

#### menu_open

Fired when the Lynx menu opens.

#### menu_close

Fired when the Lynx menu closes.

**Examples:**

```lua
client.set_event_callback("menu_close", function()
  client.log("menu closed")
end)
```