Writing a mod in TinyGo
The TinyGo SDK mirrors the C++ SDK in Go: the same API, script classes, split halves and wire
format. Import atl and atl/sm, plus the atl/sm/... packages you use, such as
atl/sm/gui or atl/sm/physics. The full API is in the
TinyGo SDK reference.
package main
import ( "atl" "atl/sm" "atl/sm/gui" "atl/sm/log")
type Lamp struct { sm.ShapeClass on bool}
func (l *Lamp) ClientCanInteract(sm.Character) bool { gui.Interaction("Use", "Toggle") return true}
func (l *Lamp) ClientOnInteract(_ sm.Character, pressed bool) { if pressed { l.Network().SendToServer("sv_toggle", nil) }}
func (l *Lamp) SvToggle(_ atl.Value, sender sm.Player) { l.on = !l.on l.Interactable().SetActive(l.on) log.Info("{} toggled the lamp at {}", sender.Name(), l.Shape().WorldPosition())}
func init() { sm.Class[Lamp]("Lamp").Method("sv_toggle", sm.Fn2((*Lamp).SvToggle))}
func main() {}From C++ to Go
Section titled "From C++ to Go"| C++ | Go |
|---|---|
class Lamp : public ShapeClass |
a struct that embeds sm.ShapeClass |
ATL_CLASS(Lamp) |
sm.Class[Lamp]("Lamp"), usually in init() |
overriding client_onInteract |
a method ClientOnInteract. Callbacks keep the game's names in Go case. |
.method<&Lamp::sv_toggle>("sv_toggle") |
.Method("sv_toggle", sm.Fn2((*Lamp).SvToggle)). Fn0 to Fn4 take 0 to 4 arguments, FnR0 to FnR4 also return a value. |
shape.worldPosition() |
shape.WorldPosition() |
log::info("{} at {}", n, p) |
log.Info("{} at {}", n, p) |
ATL_MOD(MyMod) |
atl.SetMod(&MyMod{}) |
Optional<T>, Vector<T>, atl::String |
a value and a bool, slices, string |
As in C++, only the callbacks a type defines exist on the game-side class.
Mod-wide code
Section titled "Mod-wide code"Define any of the atl.Mod methods on a type and register it with atl.SetMod:
type MyMod struct{}
func (MyMod) ServerOnCreate() { log.Info("loaded") }func (MyMod) ServerOnFixedUpdate(dt float64) { }
func init() { atl.SetMod(MyMod{}) }The available methods are ServerOnCreate, ServerOnFixedUpdate, ServerOnDestroy,
ClientOnCreate, ClientOnFixedUpdate, ClientOnUpdate and ClientOnDestroy. A type only
needs the ones it uses.
Split parts
Section titled "Split parts"Split parts work as in C++. The halves embed sm.ServerShapeClass
and sm.ClientShapeClass, their callbacks drop the Server/Client prefix, and they talk
through plain structs:
type Toggle struct{}type State struct { On bool By string}
type LampServer struct { sm.ServerShapeClass state State}
func (l *LampServer) OnCreate() { l.LoadState(&l.state) l.Network().Sync(l.state, 1)}
func (l *LampServer) OnToggle(_ Toggle, sender sm.Player) { l.state = State{On: !l.state.On, By: sender.Name()} l.Interactable().SetActive(l.state.On) l.Network().Sync(l.state, 1) l.SaveState(l.state)}
type LampClient struct { sm.ClientShapeClass state State}
func (l *LampClient) OnInteract(_ sm.Character, pressed bool) { if pressed { l.Network().Send(Toggle{}) }}
func (l *LampClient) OnState(s State) { l.state = s }
func init() { sm.ServerClass[LampServer]("Lamp").On(sm.HandleFrom((*LampServer).OnToggle)) sm.ClientClass[LampClient]("Lamp").On(sm.Handle((*LampClient).OnState))}sm.Handle builds a handler that takes the message, and sm.HandleFrom one that also takes
the sending player (server half only). Messages use the same wire format as the C++ and Rust
SDKs, with the same size limits.
Building
Section titled "Building"Build with TinyGo's freestanding WebAssembly target, so the module imports nothing but the
atl functions:
tinygo build -target=wasm-unknown -o MyMod/Scripts/lamps.wasm .Then run atl-build on the mod folder.