Object
An Object is a live unit, building, tree, animal or other thing in the world.
Get one from a query such as GetObjectById or from a Player method; never
create one yourself. Call methods with :.
local tc = GetAssignedPlayer():GetTownCenters()[1]
if tc and tc:IsAlive() then
LogInfo(tc:GetName(), tc:GetHitpoints())
end
Rules for every method. Calling a method on something that is not an
Object raises a Lua error. A nil result means the value could not be read.
Some methods return a default such as 0 instead; the tables say which.
Don't keep Object values between updates. A saved object can outlive the
unit. Save object:GetId() and look it up again with GetObjectById, or check
IsAlive() first.
Comparing and printing. a == b is true when two Object values are
the same game object. Each call makes a new value, though, so two of them are
still different table keys: key tables by GetId(). tostring(object) and
LogInfo(object) show Object#123 HORSWC (ID and internal name), or
Object#? (unreadable) once the object can no longer be read.
Identity
| Method |
Returns |
GetId() |
The object's ID, which commands and Xs* queries take. -2147483648 (the smallest 32-bit integer) if the object can't be read; 0 for a null handle. |
GetName() |
Display name, or the internal name when the type has none. "" if unreadable. |
GetInternalName() |
The type's internal data name, such as HORSWC. "" if unreadable. |
GetMasterName() |
The type's own name, with no fallback. "" if none. |
GetUnitObjectType() |
The type ID, a UnitObjectType value such as 83 (villager). 0 or less if unreadable. |
GetClass() |
The unit class, a UnitClass value (900 and up). -1 if unreadable. |
GetObjectType() |
1 building, 2 combat unit, 0 anything else. Not the type ID. |
GetKind() |
The engine's kind byte: 70 trainable unit, 80 building, lower for scenery, trees, birds, dead units and projectiles. |
GetOwningPlayer() |
The owner as a Player. Gaia objects return player 0. |
Names are for logs and display; compare type IDs in script logic. Class 914
(TERRAIN) is mostly decoration such as dead plants and skeletons, with almost
no collision.
Health and state
| Method |
Returns |
IsAlive() |
true while hit points are positive and the world still holds the object. false otherwise. |
GetHitpoints() |
Current hit points, or nil. |
GetMaxHitpoints() |
Maximum hit points with upgrades, or nil. |
GetBuildStatus() |
0 unfinished foundation, 2 finished. Also 0 if unreadable, so check IsAlive() too. |
GetFaith() |
A monk's faith, 0 to 100; nil, "faith-unavailable" if unreadable. |
GetConversion() |
false when the unit is not converting, else { target, elapsed, duration }: the unit being converted (or nil) and the convert action's two timers; elapsed / duration is its progress. nil, "conversion-unavailable" if unreadable. |
GetCarriedResourceType() |
The ResourceType carried. -1 if unreadable. The type alone doesn't mean it carries anything. |
GetCarriedResourceAmount() |
Amount carried. 0 when empty or unreadable. |
GetAttribute(attribute[, damageClass = 0]) |
An attribute with upgrades, as xsGetUnitAttribute reads it: use an ObjectAttribute value. damageClass picks the class for ARMOR and WEAPON. Attributes 3 and 4 are the collision half-widths. |
GetObjectData(dataId) |
A PER object-data value for the type, from ObjectData. 0 if unreadable. |
GetInstanceData(dataId) |
The same, for this object, or nil and a reason. -2 means the data doesn't apply. IDs 44, 45, 46, 48 and 80 need the AI player and return nil. |
GetControlGroups() |
Bit mask of the local player's control groups holding the object: bit 1 << n for group n. 0 for none. |
GetAnimalMode() |
The type's animal mode byte; predators in modes 1 and 4 count as enemies in town. nil, "animal-mode-unavailable" if unreadable. |
Position and visibility
| Method |
Returns |
GetPosition() |
Vector3: x, y in tiles with fractions (centre of tile 10, 20 is about 10.5, 20.5), z height. If the position can't be read, all three are NaN (position.x ~= position.x), not nil. |
DistanceTo(other) |
Straight-line distance in tiles from this object's position to another Object's position (centre to centre, not the gap between them) or to an { x, y } point, ignoring height. nil when a position is unreadable. For the game's own edge-to-edge distance use GetNativeDistanceTo. |
GetCurrentMapTile() |
The MapTile under the object. |
IsVisible() |
true when the tile under it is visible to the assigned player now. |
IsExplored() |
true when the tile under it is visible or explored. |
IsSighted() |
true when its tile is visible, or explored for an object that stays drawn in the fog (the engine's sighting rule). One call instead of IsVisible, StaysVisibleInFog and IsExplored. |
StaysVisibleInFog() |
true for types that stay visible once seen, such as buildings. |
IsVisible() tests the tile, not line of sight to the object.
Actions and movement
| Method |
Returns |
GetTargetObject() |
The object its current action targets: the tree being chopped, the unit being attacked. |
GetTargetPosition() |
The point it is heading for or working at, as a Vector3. GetActionTargetPosition() is the same. |
GetActionType() |
The engine ID of its current action, or nil when it has none. |
GetSubActionType() |
The sub-action field of its current action. |
IsNativeIdle() |
The game's own idle test, as idle-villager selection uses it. |
IsIdle() |
A stricter test: no unit acts on it, no action, no target position and, for buildings, no production. false if unreadable. |
IsNativeMoving() |
true while its velocity is not zero. |
IsMoving() |
true when it has a target point over half a tile away and no target object. false if unreadable. |
GetMovementGroup() |
id, size, formation of its formation group, or just -1 when it has none. |
Prefer IsNativeIdle() and IsNativeMoving(). IsIdle() reads false for a
worked farm or a unit that still holds a target point, and IsMoving() reads
false for a unit walking to a tree or an enemy. Action type IDs have no enum;
log them in a known situation before you rely on one.
Garrison and production
| Method |
Returns |
IsGarrisoned() |
true inside a unit or building, false outside. |
GetGarrisonObject() |
The unit or building it is garrisoned in, or nil. |
GetNativeContainerId() |
ID of whatever holds it, including a monk carrying a relic. -1 for nothing. |
GetGarrisonCount() |
How many units are inside it. |
GetProductionQueue() |
The building's batches in order, each { kind = "unit" or "research", typeId, quantity, batchId }. {} when empty; nil, reason if unreadable. |
GetProductionQueueCount() |
Units and research left in the queue, including the current one. nil, reason if unreadable. |
GetProductionProgress() |
The queue's current item as { kind, typeId, elapsed, duration }: kind and typeId as in GetProductionQueue, elapsed the production timer, and duration the owner's train or research time in seconds. elapsed can pass duration while the building waits (housed, no room). Works for research too. nil, "not-producing" when idle; nil, "production-action-mismatch" when the action names another item than the queue head; nil, reason if unreadable. |
GetTrainingProgress() |
progress, typeId of the unit being trained: its production timer, counting up to its train time. nil, "not-training" when nothing trains; nil, reason if unreadable. |
GetGatherPointTargetId() |
ID of the object the building's gather point names. |
GetActingUnitCount() |
How many units have an action on it: builders, repairers, gatherers. Useful to see whether a foundation has builders. |
GetGarrisonObject() and IsGarrisoned() see garrisons only; use
GetNativeContainerId() for relics.
Combat and pathing
These call the game's own functions and return nil when a game update has
moved them.
| Method |
Returns |
GetNativeDistanceTo(other) |
Tiles between the two objects' edges, 0 when touching. Compare this with ranges, not centre distance. |
GetNativeRange() |
Weapon range including modifiers on this object alone. |
GetNativeDamageTo(target) |
Damage per attack against target, after armour, as the computer player scores targets. |
GetNativeRateOfFire() |
Seconds between attacks. |
GetNativeTargetFlags() |
Two booleans the computer player's target filter requires on a candidate. |
CanNativePathToObject(target, range) |
true when the pathfinder can get it within range tiles of target; nil, reason if the search can't run. Expensive: cache it. |
GetNativeZone() |
The pathing zone it stands in. Same zone means reachable by its movement type. |
GetNativeZoneAt(x, y) |
The zone of a tile in its zone map. Equal to GetNativeZone() means it can reach the tile. |
PassesNativeRepairCheck() |
true when the computer player's repair search would repair this building. |
GetDirection, IsScouting, GetPath and CalculatePath exist but are
not implemented.