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.
- Starting mid-match:
MatchStartedonly fires on the transition, never for a match that is already running. Doif Game.IsRunning() then setup() endbefore connecting, as in the quickstart. - Between matches no team is in play.
Teams.GetTeams()is empty, team handles from the last match haveExists = false, and calls that take a team error with "team is not in the current match".Map.AddBlockandZones.Creatework only during a match, and blocks and zones are removed when the match ends. - GameConfig is reset during
Loading. The gamemode's settings are written back, so aGame.SetConfigmade between matches is lost for the keys a gamemode sets. Set rules inMatchStarted, or register a gamemode with the overrides (Game.RegisterGamemode) and pick it withGame.SetNextMatch.
Errors
- An uncaught error ends only the thread it happened on. It is logged as
script:line: message, and the mod's error count in the running list goes up. The mod keeps running.- An error in
mainafter you connected handlers leaves those handlers connected. - Each time a signal fires, every handler runs on a new thread. A handler that errors stays connected, and its next call runs normally.
- An error in
- API calls validate their arguments and throw
Member: message, for exampleUnits.Spawn: position must be a Vector3orUnit:SetTarget: cannot target an ally. These errors can be caught withpcall. - Running out of budget (an endless loop with no
task.wait, or a hook that runs too long) aborts the thread.pcallcannot catch an abort. - A hook that errors, yields or runs out of budget counts as "no opinion": the damage stays the same and the build or recruit is allowed.
Handles to things that are gone
| Handle | Once the object is gone |
|---|---|
Unit / Building | Alive 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. |
Team | Exists and Alive are false. Passing it to the API errors ("team is not in the current match"). |
Block / Zone | Alive is false. Check it before you use one again. |
Widget | Alive 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 stops | Left as it is |
|---|---|
| Hooks, signal connections, threads and coroutines | Units and buildings it spawned or placed (they fight on) |
| UI panels | Money, health and research it changed or granted |
| Zones it created; blocks it built | Alliances it formed or broke |
| Gamemodes it registered | SetConfig changes (until the next match load resets the gamemode's keys) |
SetAutoEnd(false), SetNextMatch | Teams players were moved to with SetTeam |
Team IncomeMultiplier | Quest freeze (stays for the server's life) |
Storage is saved one last time |