Tiny Strats Modding

Lifecycle

Starting, matches, errors, stale handles and what stopping cleans up.

Starting

Pressing Run first syntax-checks every script in the mod. If one fails, nothing runs and the error is shown. Then main (the entry script) runs on its own thread. When the top-level code finishes, the mod keeps running: its connections, hooks, task loops and panels stay active until the mod is stopped. Being idle does not stop a mod.

Matches

A mod lives across matches; it is not restarted when a match ends. One normal cycle is:

Intermission -> VotePrepare -> Voting -> Loading -> Active -> JustEnded -> Ended -> Intermission

(Map editor servers sit in Editor.) The match counts as running (Game.IsRunning()) from Active until it ends.

Errors

Handles to things that are gone

HandleOnce the object is gone
Unit / BuildingAlive is false. Getters return the last values (Type, Position, TeamName, ...); Health is 0 or less. Methods do nothing: Sell returns 0, Upgrade returns false, GetTarget returns nil. Bad arguments still error.
TeamExists and Alive are false. Passing it to the API errors ("team is not in the current match").
Block / ZoneAlive is false. Check it before you use one again.
WidgetAlive is false. Setting properties does nothing.
Imported module (interop)Every use errors: "mod '<name>' is no longer running".

Handles are unique per object, so a == b compares objects, and you can use handles as table keys.

Stopping

A mod stops when someone presses Stop in the console, when the mod calls Mod.Stop(), or when the server's memory guard stops it. Stopping closes every thread and connection and then cleans up:

Undone when the mod stopsLeft as it is
Hooks, signal connections, threads and coroutinesUnits and buildings it spawned or placed (they fight on)
UI panelsMoney, health and research it changed or granted
Zones it created; blocks it builtAlliances it formed or broke
Gamemodes it registeredSetConfig changes (until the next match load resets the gamemode's keys)
SetAutoEnd(false), SetNextMatchTeams players were moved to with SetTeam
Team IncomeMultiplierQuest freeze (stays for the server's life)
Storage is saved one last time