Commands¶
Commands ask the game to do something: move units, train, build, trade. They are global functions.
local player = GetAssignedPlayer()
local selected = player and player:GetSelectedObject()
if selected then
local accepted = UnitsMove(selected, Vector2(42, 42))
if not accepted then LogWarn("Move request was not queued") end
end
How every command works¶
- Units. Where a command takes
units,buildingsor similar, pass oneObjector a dense array of them. They must be yours and alive. - Positions. A position is
Vector2(x, y)or{ x = ..., y = ... }in tile coordinates. - Result. A command returns
truewhen it entered the command queue, andfalsewhen it was refused at once.truedoes not mean the game did it: the command can still be refused before it is sent, and the game applies its own rules after that. Read game state in a later update to see the effect. - Tracked form. Most commands have a
...Trackedtwin that takes the same arguments and returnsaccepted, receipt. The receipt tells you whether the command was sent, and why not when it was refused. See Command receipts; release receipts you no longer need. - Errors. Arguments of the wrong Lua type raise a Lua error. Everything
else (dead units, a lost target, a changed match) is a refusal:
false, or a rejected receipt with a reason. - Re-checks. The queue re-checks units, targets and the match just before sending, so a unit that died or that a human player re-ordered in the meantime is left out. Leaving the match drops every queued command.
Don't send the same command every update. Send it once when your script decides to, then check the result later. See Write reliable scripts.
Movement and orders¶
Only UnitsMove and UnitsTargetObject can append. Routes, follow and guard
always replace the units' current orders.
Follow, guard, patrol, attack-move and auto-scout are orders, not stances: use the
Units* names. The SetUnitStance* names run the same commands, with the same
tracked forms, and keep working.
Production and building¶
To keep a building busy, ask for the units you need in one TrainUnit call
instead of waiting for an empty queue and training one at a time; in
multiplayer every wait leaves the building idle. Reserve the resources and
population yourself. The worked example is
Keep a production buffer.
CancelProduction sends a queue position, not the batch. If the queue changes
before the game applies it, a different item can be cancelled. Don't send two
cancellations for the same building at once.
Garrisons, town centres and packing¶
Gates¶
| Command | Does |
|---|---|
LockGates(gates) |
Lock the gates. At most 255. Safe to repeat. |
ToggleGate(gate) |
Switch one gate between locked and unlocked. |
There is no unlock command. Never retry a ToggleGate automatically: a second
toggle undoes the first. Read the gate's state instead.
Market, tribute and match¶
If you can't afford a tribute plus its fee, the game can send less. Don't retry a tribute automatically; check your resources.
Refusal reasons worth knowing¶
A tracked receipt names why a command was refused. The ones scripts most often need to handle:
| Reason | Meaning |
|---|---|
invalid-target |
The target died or disappeared before the command was sent. |
invalid-position |
A position was missing or not a finite number. |
invalid-waypoint-count |
A route had no points or more than 10. |
invalid-actors, actor-group-too-large |
A unit was not yours or not alive, or the group was too big. |
partial-submission |
Only some batches of a large group were sent (the receipt is indeterminate). Check the units before retrying any. |
rally-target-changed |
A gather-point target died or changed. |
original-market-unavailable |
The market used for a trade is gone. |
tribute-fee-unavailable |
The tribute fee could not be read. |
teams-locked |
Diplomacy is locked in this match. |
not-a-gate-or-class-unavailable |
ToggleGate was given something that is not a gate. |
packet-layout-unavailable |
This game version is not supported for the command. |
GetApiCapability(command) tells you whether a command works in this game
version at all, without sending anything. See Runtime.
The removed api table's commands and their replacements are listed in Compatibility.