Skip to content

Player

A Player is one player slot: 0 is Gaia, 1 to 8 are players. Get one with GetAssignedPlayer(), GetPlayerById(id) or object:GetOwningPlayer().

local me = GetAssignedPlayer()
local villagers = me:GetObjectsByClass(UnitClass.VILLAGER)
LogInfo(me:GetPlayerName(), #villagers)

Rules for every method. Calling a method on something that is not a Player, or with an argument of the wrong type, raises a Lua error. A nil result means the value could not be read. Some methods return a default such as 0 or false instead; the tables say which.

a == b is true when two Player values are the same slot, but they are different table keys: key tables by GetId(). tostring(player) and LogInfo(player) show Player#2 Name, or Player#? (unreadable).

GetPlayerCount() leaves Gaia out, so a 1v1 reads 2. Loop over 1..count.

About the player

Method Returns
GetId() The player number. -1 if unreadable.
GetPlayerName() The name. "" is a real, empty name.
GetPlayerType() A PlayerType value: HUMAN, BOT, GAIA or NON_PLAYER.
GetCivilizationId() Civilization ID. 0 if unreadable, which is also Gaia's.
GetColor() The player colour as { r, g, b, a }.
IsResigned() true after resigning. Use GetPlayerInGame to test whether a player is still playing.
HasWon() true once the match has ended with this player named the winner. The game names one player; check their allies in team games.
GetPercentMapExplored() Percent of the map explored, 0 to 100.
IsAlliedWith(other) true when this player's stance toward other is ally. Self and Gaia read false.
IsEnemyTo(other) true when this player's stance toward other is enemy.

Stances are one-way: A can be allied to B while B is enemy to A.

Resources and technology

Method Returns
GetAttribute(attribute) A stockpile, population or other per-player value; use PlayerAttribute or ResourceType, such as PlayerAttribute.FOOD.
GetFact(factId[, parameter = 0]) A PER fact for this player; use a Fact value.
CanAfford(objectType) true when the player has the resources for one of that type. A second argument is accepted and ignored.
GetObjectCost(objectType[, multiplier = 1]) costs, available; see costs.
GetTechCost(technology) costs, available; see costs.
CanAffordResearch(technology) true when the player has the resources for the technology.
CanResearch(technology) true when the technology is researchable, affordable, and a building that researches it exists.
GetResearchState(technology) A ResearchState value. 0 (LOCKED) if unreadable.
IsTechnologyResearched(technology) true once researched. false if unreadable.
IsObjectTypeAvailable(objectType) true when civilization and age allow the type. false if unreadable.

Costs

GetObjectCost and GetTechCost return two values. The first is an array of { resourceId, amount } entries for each resource with a cost, in food, wood, stone, gold order; each entry also has resourceId and amount fields. The second is false when the cost could not be read: ignore the array then. An empty array with true means free.

local costs, available = me:GetObjectCost(UnitObjectType.ARCHER)
if available then
    for _, cost in ipairs(costs) do LogInfo(cost.resourceId, cost.amount) end
end

Counting and finding objects

Method Returns
GetUnitTypeCount(unitType) Finished units of the type. 0 if unreadable.
GetUnitTypeCountTotal(unitType) The same, including units in training.
GetBuildingTypeCount(buildingType) Finished buildings of the type. 0 if unreadable.
GetBuildingTypeCountTotal(buildingType) The same, including foundations.
GetPlayerObjects() Everything the player owns.
GetObjectsByClass(unitClass) Objects of a UnitClass; -1 for all. GetObjectsByClassDeadInclusive is the same call and adds no dead objects.
GetObjectsByTypes(types) Objects of any of the types. Pass an array or separate IDs: GetObjectsByTypes(83, 293).
GetObjectsByMostCommonType(types) Objects of whichever listed type the player has most of; first listed wins a tie.
GetTownCenters() Objects of type 109. Scenario town-centre variants need GetObjectsByTypes.
GetSleepingObjectsByClass(unitClass) Objects on the engine's separate sleeping list; -1 for all. nil if unreadable.

Lists are empty when nothing matches or the list cannot be read. The global GetUnitTypeCount and friends do the same for the assigned player but also return an availability flag; prefer them when a wrong 0 would matter.

Local screen

These read the screen of the person at this computer, not the player you call them on.

Method Returns
GetCameraPosition() The camera's map position as a Vector2. Same for every player.
GetSelectedObject() The selected object, if this player owns it.
GetSelectedObjects() Every selected object, in order, any owner. Only for the local player: others return nil, "not-local-player".
GetSelectedObjectCount() How many are selected. Only for the local player.

GetCivilizationName and GetMouseHoveredObject exist but are not implemented.