Pages

COB unit-type query

A unit script can ask what type another unit is:

if (get UNIT_TYPE(unit) == UNITTYPE("ARMCOM"))
{
    // unit is an Arm commander
}

This is an AEX extension. Stock TA has no UNIT_TYPE: a script that uses it runs only on AEX.

get UNIT_TYPE(unit)

UNIT_TYPE is get id 0xAE01. Like UNIT_HEIGHT, its argument is a unit id (as MIN_ID..MAX_ID, MY_ID or TARGET_ID give). It answers the type number of that unit's name, the name of its FBI file (ARMCOM for units/armcom.fbi, the same as its UnitName in TA's own units), while the unit is alive, and 0 when the slot holds no unit or a dead one. No type number is 0.

Unlike a model height, every unit type has its own number, so two types that share a model cannot be confused.

UNITTYPE("NAME")

The BOS compiler turns UNITTYPE("NAME") into the type number of NAME as a constant, so a comparison costs nothing at run time. The name is the unit's name, in any case; it must be one quoted word with no spaces. The compiler does not check that the type exists: a name no loaded unit has never matches.

A long list of get UNIT_TYPE(v) == UNITTYPE(...) || ... comparisons on one unit asks the engine once and checks the list in one step, and a MIN_ID..MAX_ID loop over it skips empty slots quickly, as with the other unit-slot gets.

The type number

32-bit FNV-1a (offset basis 2166136261, prime 16777619) over the bytes of the unit name with a-z changed to A-Z, then the low 31 bits. A result of 0 becomes 1. The number is part of the script format and never changes, so a compiled COB keeps working across AEX versions. A tool outside AEX that needs the number computes it the same way.

Publishing from AEX Studio

A TA mod that tells types apart by model height (get UNIT_HEIGHT(v) == <height>) can be moved to UNIT_TYPE: the publish step (aex-mod pack, and AEX Studio's export) rewrites those comparisons. What it rewrites and when is in The AEX mod package.