Your first WebAssembly mod
A WebAssembly mod is the same Rust you write for a native mod, built to mod.wasm instead. The game runs it in a sandbox, with limits on time and memory, so a player needs no compiler and no trust prompt for native code. In this page you build the greeter from Your first native mod as a WebAssembly mod.
From Sunrise's modding bookead8a1e
You need cargo sunrise-mod (installed as in the content mod tutorial) and rustup: the first build installs the pinned Rust toolchain with rust-src.
1. Start from the native project
Make the project from the wasm template. It is the native project without the [native] table: a mod carries native code or WebAssembly, never both, and the build writes its own [wasm] table. The SDK build the mod is made for is pinned in Cargo.toml instead, under [package.metadata.sunrise] sdk (the pin).
cargo sunrise-mod new greeter --template wasm
cd greeterKeep the code in src/lib.rs as it is:
use mod_api::{self, Hooks, PlayerLoggedInEvent, ServerHost, Text};
#[derive(Default)]
struct Mod;
#[mod_api::sunrise_mod]
impl Hooks for Mod {
/// A player joined the server: greet them.
fn player_logged_in(&self, host: &mut dyn ServerHost, event: &mut PlayerLoggedInEvent<'_>) {
let line = format!("greeter says hello, {}!", event.name);
if let Some(player) = host.player(event.player) {
let _ = player.tell(host, Text::literal(line));
}
}
}2. Build
cargo sunrise-mod build --wasmThe tool compiles the crate with the pinned toolchain, writes mod.wasm beside sunrise-mod.toml, records it in a [wasm] table, and packs target/sunrise-mod/greeter.smod:
[wasm]
sha512 = "…" # mod.wasm's hash, checked when the game loads it
toolchain = "<version> <commit>" # every mod linked together is built by the same oneNever write [wasm] by hand; rebuild instead.
3. Play
Copy greeter.smod into the game folder's sunrise-mods/ (in the launcher: Open folder in the instance's ⋮ menu), and turn it on with a line mod.greeter:true in sunrise-mods.txt beside options.txt. The game loads it at start-up; join a world and the greeting appears. If the game refuses the mod (a wrong layout, a missing dependency, a limit), the Mods screen shows why in red.
What differs from native
Hooks, Parts, attachments, [dependencies] and the manifest mean the same on both targets. A WebAssembly mod cannot use the native route ([native.*] tables), and an API item marked Not on wasm in the API reference is unavailable to it. Values cross between the game and mod.wasm in one shared encoding; your own types cross when they derive Wire.
WebAssembly mods covers dependencies, memory, limits and the module cache.
Edit this page on GitHub · Spotted something wrong? Open an issue or ask on Discord.



