Writing a mod in Rust
The Rust SDK, the atl crate, is a port of the C++ SDK: the same API, script classes, split halves
and wire format, with Rust names. It is #![no_std]. Its default runtime feature brings an
allocator and a panic handler that traps the mod with the panic message.
The full API is in the Rust SDK reference.
#![no_std]extern crate alloc;use atl::prelude::*;
#[derive(Default)]struct Lamp { on: bool }
#[atl::class(methods(sv_toggle))]impl ShapeClass for Lamp { fn client_can_interact(&mut self, _: Character) -> bool { gui::interaction("Use", "Toggle"); true } fn client_on_interact(&mut self, _: Character, pressed: bool) { if pressed { self.network().send_to_server("sv_toggle", ()) } }}impl Lamp { fn sv_toggle(&mut self, _: Value, sender: Player) { self.on = !self.on; self.interactable().set_active(self.on); log::info!("{} toggled the lamp at {}", sender.name(), self.shape().world_position()); }}From C++ to Rust
Section titled "From C++ to Rust"| C++ | Rust |
|---|---|
shape.worldPosition() |
shape.world_position(). Names are snake_case, and type() is kind(). |
class Lamp : public ShapeClass and ATL_CLASS(Lamp) |
#[atl::class] impl ShapeClass for Lamp on a Default struct |
overriding client_onInteract |
fn client_on_interact, which the macro maps to client_onInteract |
.method<&Lamp::sv_toggle>("sv_toggle") |
#[atl::class(methods(sv_toggle))] |
ATL_SERVER_CLASS(Lamp, LampServer).on<&LampServer::onToggle>() |
#[atl::server("Lamp", on(on_toggle))] impl ServerShapeClass for LampServer |
| a message struct | #[derive(atl::Message)] on a struct or a field-less enum |
ATL_MOD(MyMod) |
impl Mod for MyMod and atl::export_mod!(MyMod) |
ATL_GLOBAL("sm.x.y").call<int>(a, b) |
global!("sm.x.y").call::<i32>((a, b)), with the arguments as a tuple |
log::info("{} at {}", n, p) |
log::info!("{} at {}", n, p) |
default arguments, atl::none |
every parameter is explicit, None for an optional one, options structs with ..Default::default() |
Optional<T>, Vector<T>, atl::String |
Option<T>, Vec<T>, String |
lambdas capturing this |
closures that reach the script object with with_mut::<Lamp>(|lamp| ...) |
Differences from C++
Section titled "Differences from C++"- Only the callbacks the impl block defines exist on the game-side class, as in C++. A callback on the wrong half, or a prefixed name on a split half, doesn't compile.
- A Rust object can't be borrowed twice. If a call reaches a script object while one of that
object's own callbacks is running (for example, a host's
send_to_serverdelivered right away), it runs as soon as that callback returns. Split halves are separate objects, so they never wait on each other. - Typed messages use the C++ SDK's format, so the limits, chunking, COBS and base64 (the
wire-base64feature) and the version checks all match. Type names in logs are Rust paths, likehello_rust::lamp::State. Strings in messages must be UTF-8. - Classes register from the module's
_initialize. Each macro puts its registration in.init_array, so classes can live in any module.
Building
Section titled "Building"rustup target add wasm32-unknown-unknown # oncecargo build --release --target wasm32-unknown-unknown # with crate-type = ["cdylib"]Put the resulting .wasm in your mod folder and run atl-build.