# Snippets

Small, self-contained pieces. For full worked examples see the game pages under **Games**.

## Watermark

```lua
-- draw ping-free info: the time and the frame the runtime is on
client.set_event_callback("paint", function()
  local w, h = client.screen_size()
  local hour, minute, second = client.system_time()
  local text = string.format("%02d:%02d:%02d | %d fps", hour, minute, second, 0)
  local tw, th = renderer.measure_text(nil, text)
  renderer.rectangle(w - tw - 22, 14, tw + 8, th + 8, 32, 32, 32, 200)
  renderer.text(w - tw - 18, 18, 235, 235, 235, 255, nil, 0, text)
end)
```

## A menu toggle with a callback

```lua
local toggle = ui.new_checkbox("Lua", "Main", "My feature")
ui.set_callback(toggle, function(item)
  ui.notify(ui.get(item) and "on" or "off")
end)
```

## Persisting a value

```lua
local runs = (database.read("runs") or 0) + 1
database.write("runs", runs)
client.log("loaded", runs, "times")
```

## An HTTP GET (needs the http capability)

```lua
if http then
  http.get("https://api.ipify.org?format=json", function(ok, res)
    if ok then client.log(json.decode(res.body).ip) end
  end)
end
```

## A delayed call

```lua
client.delay_call(2.0, function(msg) client.log(msg) end, "two seconds later")
```