Skip to content

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, buildings or similar, pass one Object or 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 true when it entered the command queue, and false when it was refused at once. true does 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 ...Tracked twin that takes the same arguments and returns accepted, 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

Command Does
UnitsMove(units, position[, append]) Move to a point. append = true adds the move after the units' current orders. Also accepts UnitsMove(units, x, y[, append]).
UnitsStop(units) Stop.
UnitsTargetObject(units, target[, append]) The game's default action on target: attack an enemy, gather a resource, and so on. append as for UnitsMove.
DropResources(units) Drop off carried resources.
UnitsGarrison(units, container) Garrison inside container, even where the default action would differ (a loaded villager on its own town centre garrisons rather than drops off).
UnitsFollow(units, target) Follow target. SetUnitStanceFollow is the older name of this order.
SetUnitStanceFollow(units, target) Follow target.
UnitsGuard(units, target) Guard target. SetUnitStanceGuard is the older name of this order.
SetUnitStanceGuard(units, target) Guard target.
UnitsPatrol(units, position) Patrol to one point. SetUnitStancePatrol is the older name of this order.
SetUnitStancePatrol(units, position) Patrol to one point.
UnitsPatrolRoute(units, positions) Patrol along 1 to 10 points, in order.
UnitsAttackMove(units, position) Attack-move to one point. SetUnitStanceAttackMove is the older name of this order.
SetUnitStanceAttackMove(units, position) Attack-move to one point.
UnitsAttackMoveRoute(units, positions) Attack-move along 1 to 10 points, in order.
UnitsAttackGround(units, position) Attack the ground at a point. At most 255 units.
UnitsAutoScout(units) Scout automatically. SetUnitStanceAutoScout is the older name of this order; EnableScouting() picks a scout for you.
SetUnitStanceAutoScout(units) Scout automatically.
EnableScouting([units]) Scout automatically; without units, the API picks one of your scouts. No tracked form.
SetUnitStanceSeekShelter(units) Find shelter; the game picks the building. SetUnitStanceGarrison is the same command with no tracked form; it is deprecated, and UnitsGarrison garrisons in a building you choose.
SetUnitStanceUngarrison(buildings) Unload every unit in the buildings.
GatherInside(actors[, state = -1]) Set the gather-inside state: -1, 0 or 1.
SetUnitCombatStance(units, stance) Set the stance: a UnitCombatStance value, 0 to 3.
SetUnitFormation(units, formationId) Set the formation. The game may not support every ID.

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

Command Does
TrainUnit(buildings, buildingType, unitType[, count = 1]) Train count units (1 to 32767). Type IDs are UnitObjectType values.
ResearchTechnology(buildings, technology) Research a Technology.
UnitsBuildStructure(builders, buildingType, position[, options]) Place a building and send the builders. Leave out options: its fields depend on the game version. The game can still refuse the spot.
SetGatherPoint(buildings, target) Set the gather point to an Object or a position.
CancelProduction(building, batchId) Cancel one item of a batch from building:GetProductionQueue().

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

Command Does
UngarrisonUnit(container, occupant) Unload one unit from the building or transport it is in.
UngarrisonTo(containers, position) Unload toward a point. At most 255 containers.
RingTownBell(townCenter[, active = true]) Ring the town bell, or with false, stop it.
SendBackToWork(townCenter) Send sheltered villagers back to work.
SendAllBackToWork(townCenter) Send every sheltered villager back to work.
UnitsPack(units) Pack, for units that can (trebuchets). Not a toggle.
UnitsUnpack(units) Unpack.
DeleteUnit(objects) Delete the objects. Cannot be undone. DestroyBuilding is the same command, does not check for buildings, has no tracked form, and is deprecated.

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

Command Does
BuyCommodity(resource[, amount = 100]) Buy at the market. amount is a multiple of 100, up to 25500.
SellCommodity(resource[, amount = 100]) Sell at the market. Same amounts.
TributeToPlayer(playerId, resource, amount) Send amount of food (0), wood (1), stone (2) or gold (3) to another player. The game charges its tribute fee on top.
SetDiplomacy(playerId, stance) Set your stance toward another player: "ally", "neutral" or "enemy". Locked teams refuse it.
SendFlare(position) Send a flare to your allies.
Resign() Resign. Cannot be taken back once sent. DispatchResignGame() is the same command with no tracked form, and is deprecated.

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.