# Bevy

Compile Bevy games to WebAssembly with Trunk or wasm-pack and wire the SDK from Rust.

Source: https://docs.wavedash.com/engines/bevy

<GithubLink href="https://github.com/wvdsh/examples/tree/main/example-bevy" />
<PlaytestLink href="https://wavedash.com/playtest/bevy-example/b1b9c1bc-601d-40ab-9f25-3567eb6ddbab" />

Bevy compiles to WebAssembly via `wasm-bindgen` and `wasm-pack` (or `trunk`). The output is a set of static files — HTML, JS, WASM, and assets — that Wavedash can host.

## Build for web

Using Trunk (recommended):

```bash
trunk build --release
```

The output is in the `dist/` folder. If using `wasm-pack` directly:

```bash
wasm-pack build --target web --release
```

Then bundle the output with an HTML shell that loads the generated JS and WASM.

## SDK integration

Wavedash injects `Wavedash` into the page before your WASM module loads. Call it from the JavaScript side using `wasm-bindgen` imports:

```rust
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = Wavedash)]
    fn getUser() -> JsValue;

    #[wasm_bindgen(js_namespace = Wavedash)]
    fn loadComplete();
}
```

Or keep SDK calls in JavaScript and invoke them from your HTML shell:

<Urgent>
**Calling `Wavedash.init()` is required.** Your game stays hidden behind the Wavedash loading screen until you do. Call it once your game is ready to play.
</Urgent>

```javascript
import init from "./pkg/my_game.js";
await init();
Wavedash.init({ debug: true });
Wavedash.loadComplete();
```

## Load progress

Report progress from your JavaScript entrypoint while the WASM module downloads and initializes:

```javascript
Wavedash.updateLoadProgressZeroToOne(0.3); // WASM downloading
const wasm = await init();
Wavedash.updateLoadProgressZeroToOne(0.9); // initializing
wasm.start();
Wavedash.loadComplete();
```

## wavedash.toml

```toml
game_id = "YOUR_GAME_ID_HERE"
upload_dir = "./dist"
entrypoint = "index.html"
```

## SDK features

From the JavaScript side of your HTML shell, `Wavedash` exposes leaderboards, achievements, stats, and user data:

```javascript
const user = Wavedash.getUser();

const lb = await Wavedash.getLeaderboard("high-scores");
await Wavedash.uploadLeaderboardScore(lb.data.id, score, true);

Wavedash.setAchievement("first_win", true);
```

To call these from Rust, add corresponding `wasm-bindgen` externs in the SDK integration block above.

<Warning>
Bevy's default window size and asset paths assume a desktop environment. Mount your canvas into a container that fills the viewport and keep asset paths relative so the build works from any subpath.
</Warning>
