Writing a mod in C++
The C++ SDK is a set of headers. Include <atl.hpp>. It mirrors the sm.* API with C++ types
and needs no libc or libc++.
#include <atl.hpp>using namespace sm;
class Lamp : public ShapeClass {public: bool client_canInteract(Character) override { gui::interaction("Use", "Toggle"); // [E] Toggle return true; } void client_onInteract(Character, bool pressed) override { if (pressed) network().sendToServer("sv_toggle"); } void sv_toggle(atl::Value, Player sender) { on_ = !on_; interactable().setActive(on_); log::info("{} toggled the lamp at {}", sender.name(), shape().worldPosition()); }private: bool on_ = false;};ATL_CLASS(Lamp).method<&Lamp::sv_toggle>("sv_toggle");From Lua to C++
Section titled "From Lua to C++"| Lua | C++ |
|---|---|
shape:getWorldPosition() |
shape.worldPosition(). Getters drop get, setters keep set. |
sm.vec3.new(1, 2, 3) |
Vec3{1, 2, 3}, a value type with its math done in WASM. Same for Quat, Color, Uuid. |
local ok, r = sm.physics.raycast(a, b) |
if (auto hit = physics::raycast(a, b)) hit->point |
sm.physics.applyImpulse(body, f) |
body.applyImpulse(f) |
body:getWorldAabb() (two values) |
body.worldAabb().min and .max |
sm.gui.setInteractionText("", sm.gui.getKeyBinding("Use", true), "Toggle") |
gui::interaction("Use", "Toggle") |
sm.log.info(("%d at %s"):format(n, tostring(p))) |
log::info("{} at {}", n, p) |
return true, false from client_onEquippedUpdate |
return InputBlock{.primary = true} |
| a result that can be nil | tests false: if (auto i = shape.interactable()) |
The same rules run through the whole reference:
- Multiple return values become a struct:
world.cellBounds().maxX. - Results that may be missing become
Optional:physics::distanceRaycast. - Long lists of optional parameters become an options struct:
projectile::attack(uuid, damage, pos, vel, player, {.delay = 2}).
Mod-wide code
Section titled "Mod-wide code"Code that runs without anything placed in the world derives from atl::Mod:
class MyMod : public atl::Mod {public: void server_onCreate() override { log::info("loaded"); } void server_onFixedUpdate(double dt) override { }};ATL_MOD(MyMod);The loader calls these once per tick on each side, however many players are connected.
Functions without a wrapper
Section titled "Functions without a wrapper"Anything added in a game update can be called by its Lua path:
int difficulty = ATL_GLOBAL("sm.game.getDifficulty").call<int>();ATL_GLOBAL looks the path up once and caches it. What's wrapped
lists what already has a typed form.
Errors
Section titled "Errors"A Lua error raised by a call traps the mod with the Lua message, the same as an uncaught error
in a Lua script. Use tryCall to handle it yourself.
Building
Section titled "Building"clang++ --target=wasm32-unknown-unknown -std=c++20 -O2 -nostdlib -fno-exceptions -fno-rtti \ -fno-threadsafe-statics -Wl,--no-entry -Wl,--gc-sections -I path/to/atl-headers -o mod.wasm mod.cppThen let atl-build turn your mod folder into a loadable mod.