Game data and world¶
Global queries read the match: players, time, resources, objects and the map. Then use the methods of the Object, Player and MapTile values they return.
local me = GetAssignedPlayer()
local food = GetAttribute(PlayerAttribute.FOOD)
local villagers, ok = GetUnitTypeCount(UnitObjectType.VILLAGER_MALE)
if ok then LogInfo("food", food, "villagers", villagers) end
The assigned player. Most queries answer for the assigned player: the
local player, the one GetAssignedPlayer() returns. Use the matching Player
method to ask about anyone else.
Reading results. Wrong argument types raise a Lua error. Otherwise:
- Calls that return
value, availablegivefalseas the second value when the read failed. The first value is then a placeholder such as0: ignore it. Scripts that keep only the first value still work, butreturn Call()forwards both. - Other calls return
nilwhen the value can't be read, unless the table names a default. - Every result is a snapshot. Two calls in a row are not read at the same instant.
Match and players¶
Game time follows the simulation: it runs faster at higher game speeds and
stops while paused. A 0 time is real; failures return nil. Use
GetHighResolutionTimeMs() for real time.
Economy and technology¶
These answer for the assigned player. Player methods with the same names
answer for any player.
| Call | Returns |
|---|---|
GetAttribute(attribute) |
A stockpile, population or other value; use PlayerAttribute or ResourceType. |
CanAfford(objectType[, isBuilding]) |
true when the player can pay for one of the type. |
GetObjectCost(objectType[, multiplier = 1]) |
costs, available; see costs. |
GetTechCost(technology) |
costs, available. |
GetObjectPopulationCost(objectType) |
Population the unit takes. |
CanBuyCommodity(resource) |
true when the player can buy the resource at a market. nil, reason if unreadable. |
CanResearch(technology) |
true when researchable, affordable, and a building that researches it exists. |
IsTechnologyResearched(technology) |
true once researched. false if unreadable. |
GetResearchProducerTypes(technology) |
Building type IDs that research it. Empty for an unknown technology. |
IsObjectTypeAvailable(objectType) |
true when civilization and age allow it. false if unreadable. |
GetObjectTrainLocation(objectType) |
The building type that trains the unit. |
GetUnitTypeCount(unitType) |
count, available: finished units of the type. |
GetUnitTypeCountTotal(unitType) |
count, available, including units in training. |
GetBuildingTypeCount(buildingType) |
count, available: finished buildings of the type. |
GetBuildingTypeCountTotal(buildingType) |
count, available, including foundations. |
GetTotalUnitCount() |
count, available: finished objects not of class 903. |
GetTotalUnitCountTotal() |
The same, finished or not. |
GetTotalBuildingCount() |
count, available: finished objects of class 903 (BUILDING). |
GetTotalBuildingCountTotal() |
The same, including foundations. |
The GetTotal* counts treat only class 903 as a building: towers, walls,
gates and farms count as units. Use Player:GetObjectsByClass with the classes
you mean when that matters.
Objects¶
These scan the whole world: every player and Gaia, with no fog-of-war
filter, in ID order. Check object:IsVisible() when your script must only
use what a player can see. They are slow on large maps; prefer a Player
method for one player's objects and don't call them every frame.
GetObjectById is how to use an object you saved by ID in an earlier update.
Trees¶
Gaia keeps trees in their own list, so GetIndexedObjects and Gaia's
GetPlayerObjects() leave them out; GetTrees() reads that list. A map can
have thousands of trees and each call builds a new array, so read them every
few seconds rather than every update.
Shared per-update index¶
The first GetIndexedObjects call for a player in an update walks that
player's objects once and sorts them by class. Later calls in the same update,
from any script, return the same arrays without walking again, so several
scripts that run every update cost one walk per player. Prefer it to
player:GetPlayerObjects() or player:GetObjectsByClass() in such scripts.
The arrays are shared: don't add to, remove from or sort them (copy one
first), and don't keep them past the current Update. An unreadable entity
list gives empty arrays for that player.
function Update()
local player = GetAssignedPlayer()
if player == nil then
return
end
for _, villager in ipairs(GetIndexedObjects(player:GetId(), UnitClass.CIVILIAN)) do
-- ...
end
end
Map¶
MapTile methods hide unexplored tiles by default; see
the visibility mask.
More specialised queries (fact shortcuts, Xs* calls, pathing and placement
checks, object type data) are in Low-level queries.