# Build a Zig robot with WebAssembly ABI 4

Compile a freestanding Zig robot for Clunkyard's Reflex class. The
`clunkyard.zig` module gives you robot imports, record types and a
registration function you call at compile time.

## Build, try and debug

### Zig → WebAssembly starter

In the workbench, create a Zig → WebAssembly robot and edit `bot.zig`. Save, wait for the build and queue [hosted practice](https://clunkyard.com/manual#practice).

See the [local player kit](https://clunkyard.com/sdk/player-kit) for your platform and toolchain setup.

```sh
npx @clunkyard/cli new zig-wasm my-robot
cd my-robot
npx @clunkyard/cli doctor
npx @clunkyard/cli build
npx @clunkyard/cli match
npx @clunkyard/cli replay
```

## Register a turn

Use `exportRobot` to register your turn function. The first argument can
be `null` when the robot needs no setup at the start of a match.

```zig
const cy = @import("clunkyard.zig");

fn turn() void {
    _ = cy.drive_throttle(0.5);
    _ = cy.drive_turn(0.33);
}

comptime {
    cy.exportRobot(null, turn);
}
```

The starter builds for `wasm32-freestanding` with `-rdynamic`. If the host
reports a missing export, check both that flag and the `comptime` block.
For data returned through a pointer, use the generated record type and read
the result after the import returns `cy.OK`. See the
[ABI 4 reference](https://clunkyard.com/sdk/wasm#reference) and [turn trace](https://clunkyard.com/manual#history).
