Skip to content

Lua values and game objects

The API returns Lua tables for vectors and colors. It returns Object, Player, and MapTile values for live game data.

Call methods with :. The colon passes the value to its method.

local player = GetAssignedPlayer()
if player ~= nil and player:GetId() > 0 then
    LogInfo(player:GetPlayerName())
end

Vectors

Constructor Result
Vector2(x, y) { x, y } with distance methods.
Vector3(x, y[, z = 0]) { x, y, z } with distance methods.
Vector4(value) { x, y, z, w } with every component set to value.
Vector4([x[, y[, z[, w]]]]) Four-component vector. Missing components are 0.

Each vector has these methods:

Method Result
left:Distance(right) Three-dimensional Euclidean distance. Missing z is 0.
left:Distance2D(right) Two-dimensional Euclidean distance.

The global Distance2D(a, b) gives the same two-dimensional distance for any two positions: vectors or plain { x, y } tables. A missing coordinate raises an error.

You can pass a plain { x = ..., y = ... } table to a position command. Use a vector constructor when you need its distance methods.

Colors

Color returns { r, g, b, a } tables.

Call Result
Color() Opaque white.
Color(r[, g[, b[, a]]]) Numeric color channels.
Color("#RRGGBB") Opaque RGB color.
Color("#RRGGBBAA") RGBA color.
Color("0xAARRGGBB") ARGB color.
Color.Parse(text) Parse a hex color.
Color.HSV(h, s, v[, a = 255]) Convert an HSV color.
Color.FromARGB(n) The color of an ARGB integer such as 0xFF0000FF (opaque blue).
Color.ToARGB(c) or c:ToARGB() The ARGB integer of a color table, read as drawing functions read it; an integer comes back as given.
c:WithAlpha(a) or Color.WithAlpha(c, a) A new color with alpha a (clamped to 0..255); c is unchanged. An ARGB integer gives an ARGB integer.

Color.HSV accepts hue in 0..1 or degrees. Saturation and value use 0..1. Colors made by Color, Color.*, Player:GetColor() and Settings.GetColor have ToARGB and WithAlpha as methods; a plain { r, g, b, a } table does not, so call Color.ToARGB(t) on one. Color.ToARGB truncates fractional channels as drawing does. When every color channel is in 0..1, drawing functions scale them to 0..255. Use one channel convention in a script.

Game values

Object, Player and MapTile values come from the game. Each has its own page:

  • Object: units, buildings, trees, animals.
  • Player: player slots, resources, technology, counts.
  • MapTile: terrain, elevation, visibility.

Lifetime rule

Object and Player values describe live game data. An entity can disappear or change after a callback. Save its ID, then query it again in a later callback. See Write reliable scripts.

How calls report failure

A call that can't read or do what you asked tells you in one of these ways. Each reference row says which one applies; check for it before you use the value.

Convention Example What failure looks like
value, available GetFact(Fact.FOOD_AMOUNT), every Xs* query 0, false (GetFact) or nil, false (Xs*). 0, true is a real zero.
nil, reason unit:GetFaith() nil, "faith-unavailable". Log the reason; it names the cause.
Bare nil unit:GetHitpoints(), unit:GetOwningPlayer() nil with no reason.
NaN vector unit:GetPosition() x, y and z are NaN; test position.x ~= position.x.
A default value unit:GetBuildStatus(), player:GetPlayerObjects() 0, -1, "" or {}, which can't be told from a real value. Check IsAlive() or use a call that reports availability when it matters.
Raised error Calling a method on something that is not an Object, or passing a string where a number belongs A Lua error that ends the callback. Fix the script; don't pcall around it.
Command result UnitsMove(units, position) false when refused at once. true means queued, not done.
Command receipt UnitsMoveTracked(units, position) accepted, receipt; the receipt's state and reason say whether it was sent and why not.
local position = unit:GetPosition()
if position.x ~= position.x then
    return -- position unreadable this update
end
local faith, reason = unit:GetFaith()
if faith == nil then
    LogDebug("faith unavailable:", reason)
end