Skip to content

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, available give false as the second value when the read failed. The first value is then a placeholder such as 0: ignore it. Scripts that keep only the first value still work, but return Call() forwards both.
  • Other calls return nil when 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

Call Returns
GetAssignedPlayer() The local player as a Player, or nil outside a match.
GetAssignedPlayerId() The local player's number, or 0.
GetPlayerById(id) The Player in slot id: 0 Gaia, 1 to 8 players. nil for an empty slot.
GetPlayerCount() count, available. Leaves Gaia out: a 1v1 reads 2, so loop over 1..count.
IsEnemyPlayer(player) true when the assigned player treats player as an enemy.
GetPlayerHome(playerId) x, y of the player's town centre, else its start position; nil, reason if unreadable.
IsPocketPosition(playerId) true when the game placed the player in a pocket at the start. nil, reason if unreadable.
GetGameTime() Game time in whole seconds.
GetGameTimeMs() Game time in milliseconds.
GetTimeInCurrentAge([playerId]) Seconds the player (default: assigned) has been in its age.
GetTreatyTime() Time left on the treaty, 0 without one. Works in any match.
GetVictoryCondition() value, available: a VictoryCondition value.
GetVictoryPlayer() The winner as a Player once the match has ended; nil before.
IsDeathMatchGame() true for Death Match. false if unreadable.
GetWorldRelicTotal() Relics in the match, or nil, reason.
IsGamePaused() true while paused.
IsMenuOpen() true when no match is being played.
IsInMatch() true while a match is running on screen with a local player, the state match scripts wait for. false in menus, while loading and on the end screen.
IsDiagnosticBuild() true in Debug and Dumper builds of WololoLabs.

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

Call Returns
GetObjectById(id) The live Object with that ID, or nil once it is gone.
GetObjectsByType(objectType) Every object of the type.
SortObjectsById(objects) positions, ids: the list's positions ordered by object id and the ids in that order, so objects[positions[1]] has the lowest id. Faster than table.sort with a comparator, which calls back into Lua per comparison. Raises for an item that is not an Object.
GetObjectsByTypes(types) Every object of any of the types: an array, or separate IDs.
GetObjectsByClass(unitClass) Every object of a UnitClass; -1 for all.
GetIndexedObjects(playerId[, unitClass]) One player's objects of a UnitClass (-1 or omitted for all), dead ones included, from an index shared by every script in the update; nil if the player does not exist.
GetTrees() Gaia's trees, or nil, "tree list unavailable" before a match.
GetObjectsInArea(a, b) Every object whose centre is inside the rectangle between corners a and b, edges included.

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

Call Returns
GetMapWidth() width, available in tiles.
GetMapHeight() height, available in tiles.
GetMapTile(x, y) The MapTile there. Also takes { x, y }; fractions round down.
GetAllMapTiles() Every tile, row by row from y = 0. Tens of thousands of values: call it once and keep it.
GetPointZone(x, y) The land zone of a tile. Tiles in one zone are connected by land. nil, reason if unreadable.

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.