# Build an AssemblyScript robot

Write an AssemblyScript robot that the Clunkyard compiler builds to
WebAssembly for the Reflex class. The starter supplies a typed
`clunkyard.ts` binding for calling imports and handling the match callbacks.

## Build, try and debug

### AssemblyScript → WebAssembly starter

In the workbench, create a AssemblyScript → WebAssembly robot and edit `bot.ts`. 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 assemblyscript-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

Keep the binding's two exports in `bot.ts` and register your functions
with `cy.robot`.

```typescript
import * as cy from "./clunkyard";
export { clunkyard_start, clunkyard_turn } from "./clunkyard";

function turn(): void {
  cy.drive_throttle(0.5);
  cy.drive_turn(0.33);
}

cy.robot(null, turn);
```

The starter uses AssemblyScript's `stub` runtime. Reuse output records such
as `cy.Contact.create()` across turns because this runtime does not
reclaim memory. If a turn stops, check fuel or memory use in the
[turn trace](https://clunkyard.com/manual#history). The [ABI 4 bindings](https://clunkyard.com/sdk/wasm#bindings-and-imports)
explain how the binding maps records to memory.
