Scripting in Go
A map's rules are a Go program. It is ordinary Go, and you write it in the code editor you already use. W4 builds it to WebAssembly and the game runs it beside the simulation, in a sandbox. There is no W4 language to learn. GUI triggers are planned for later, as a layer that writes Go for you.
New here? Do Your first script first, then come back.
The shape of a script
A script is a main function that says what should happen when. The game runs main once, before the map starts, so main only registers things. The work happens in the functions you hand over.
package main
import (
"w4/event"
"w4/game"
)
func main() {
event.OnMapInit(func() {
game.Print("hello from the map")
})
}Four packages make up the API:
w4/eventsays when. Map start, a unit dying, a timer. See Events and timers.w4/gameis the game itself: units, time, the log and slash commands. See Units, Players and owners and Commands and interface.w4/playernames the players:player.Redtoplayer.Brown,player.Neutral,player.NeutralAggressiveandplayer.Everyone. See Players and owners.w4/uiputs text, buttons, panels and quests on the players' screens. Also in Commands and interface.
fmt.Println works too. Whatever the script writes to standard output ends up in the same log window as game.Print.
The full list of functions and types is the Scripting API reference. It is generated from the SDK's own doc comments. The pages here explain how the pieces fit together.
In this section
- Why Go
- Where the API lives: the
w4module,go.mod,go.work, gopls. - Events and timers
- Units: spawn, order, query.
- Players and owners
- Commands and interface: slash commands, text, buttons, panels, quests.
- Multiplayer rules: what a script may and may not do.
- Build, play and errors: where a mistake shows up.
- What is next