Skip to content
Basalt Proxy

Zig

Write, build and package a Basalt Proxy plugin in Zig, with its SDK conventions and a complete first plugin.

Choose Zig for explicit buffers, slices and allocator control. Kiln targets wasm32 and provides the basalt_proxy module. Keep handlers short and memory use bounded even when your own Zig code uses allocation.

What Kiln installs#

Nothing by hand. Kiln uses a zig on your PATH from version 0.14, and otherwise downloads Zig 0.17.1 (about 55 MB, 407 MB unpacked) into a per-user cache on the first build, on Linux, macOS and Windows. The language moves between releases, so the pinned version is the one the SDK is built against. See Kiln.

Project layout#

bash
kiln new greeter --lang zig --target proxy
greeter/
  plugin.toml
  .gitignore
  resources/config.toml
  resources/lang/en_us.toml
  src/main.zig

src/main.zig is the root of your plugin. Other files are ordinary Zig files that you @import from it. The module is written to build/plugin.wasm; build/.kiln/ holds the SDK copy and scratch files and is rebuilt each time.

The entry point#

Write pub fn init() void in src/main.zig. Kiln generates a small root file that exports what the proxy needs and calls your init once, after the module loads; that is where you register listeners, commands and tasks.

A first plugin#

This plugin greets a player when they join and adds a /greeter hello [times] command. It is the whole of src/main.zig.

zig
const basalt = @import("basalt_proxy");

fn onJoin(ctx: basalt.core.Ctx) void {
    const player = basalt.events.playerOf(ctx);
    if (player == 0) return;
    var name: [64]u8 = undefined;
    var args = basalt.core.Args.init();
    _ = args.add("name", basalt.players.name(player, &name) orelse "player");
    _ = basalt.players.send(player, "<green>Welcome, {name}!", &args);
}

fn hello(ctx: basalt.core.Ctx) void {
    const times = basalt.commands.argI64(ctx, "times") orelse 1;
    var i: i64 = 0;
    while (i < times) : (i += 1) {
        _ = basalt.commands.reply(ctx, "<yellow>Hello from greeter!", null);
    }
}

pub fn init() void {
    basalt.log.info("greeter loaded");
    _ = basalt.events.on("player.join", basalt.core.Priority.normal, onJoin);
    _ = basalt.commands.register("greeter hello [times: int 1..5]", "", hello);
}

events.on listens to an event, see Events; commands.register registers a command from a spec such as greeter hello [times: int 1..5], see Commands. The text is MiniMessage, and {name} is filled from the arguments.

How the SDK names things#

Pages in this section write calls in the C++ form (players::send); the Zig spelling is in The SDK.

  • Where things live: const basalt = @import("basalt_proxy"); gives one namespace per area, basalt.players, basalt.events, basalt.values, and so on. Functions are camelCase and some drop the area name: players::player_name is players.name, events::event_player is events.playerOf, commands::command_arg_i64 is commands.argI64, config::config_i64 is config.getI64.
  • Strings in are slices, []const u8, so a string literal works directly.
  • Strings out go into a []u8 buffer you pass, and the call gives back ?[]const u8, a slice of that buffer, or null on failure.
  • Optional arguments are optionals: pass null, or &args, for ?*const core.Args.
  • Handles are core.Player and core.Ctx, both i32. 0 is never a valid handle.
  • Args is core.Args: Args.init(), then add(key, value), which returns the Args so pairs chain. It holds 1024 bytes in total, and a pair that does not fit is dropped.
  • Constants are core.Priority.lowest to core.Priority.highest, core.Level and core.Status for the result codes.
  • Results: a call that only does something returns an i32 status. Zig does not let you ignore a value, so write _ = basalt.players.send(...) when you do not check it.
  • Numbers to text: core.formatI64(value, &buf), or std.fmt.bufPrint from the standard library.
  • Handlers are function values, *const fn (ctx: core.Ctx) void. They cannot capture variables, so keep state in file-level vars.

Building#

bash
kiln build

compiles for wasm32-freestanding in ReleaseSmall and writes build/plugin.wasm, then checks it. kiln pack writes build/greeter-0.1.0.bppkg for the proxy's plugins/ folder, see Kiln.

Store network state#

Player handles remain valid through backend changes while the player stays connected to this proxy. Use the per-player data API for values that should follow the player across those changes. Declare players_data = true in the manifest. Use storage = true for plugin-global keys. Both persist in the proxy database and are isolated by plugin id.

The proxy storage calls differ from the server's document-storage API: use the typed player-data calls or admin key/value calls described in Configuration and storage. Do not substitute a namespace in a server-only document example and assume the operation exists.

Runtime rules#

Ordinary callbacks share the proxy plugin thread and have a 50 ms wall-clock budget. Initialisation has 2 seconds. Packet callbacks have their own bounded wait and execution budget. Keep decisions short and handle errors explicitly; a slow callback delays other plugins and can disable its own instance.

Tasks count 50 ms steps, not backend world ticks. A player handle stays on the proxy session when the player changes server, but becomes stale on disconnect. Keep context handles only for the duration of a callback. Read Execution model before adding timers or packet listeners.

target = "proxy" selects SDK imports and .bppkg packaging. Java and Kotlin compiled to WebAssembly are native WASM plugins, separate from jars run by other plugin runtimes.

Where to go next#

Guide Purpose
Quickstart Build, install and verify a complete first plugin
The SDK Areas, spelling, handles, strings and error conventions
Values Structured replies and buffers
Events Observe or decide login, chat and routing
Commands Register and answer a proxy command
Storage Preserve player or global state

Edit this page