SCRMaps
/
Sign in

Getting a handle on one unit

How to learn the structure offset of a unit you just created, and how to keep trusting it.

12 min read

A pre-placed unit's index you can read in the editor. Every unit matching a condition you can get with a unit loop. But one unit created by a trigger during the game - a respawned hero, a chosen character - fits neither.

0x628438 holds the structure offset of the NEXT unit to be created. Read it immediately before creating, and the unit you then create is the one at that address.

// 만들기 직전에 읽고, 만든다. 순서가 전부입니다.
hero = dwread(0x628438);
CreateUnit(1, "Protoss Scout", loc, P1);
// 이제 hero 는 방금 만든 스카웃의 구조 오프셋입니다.
Nothing else may create a unit between the read and the create.

If the unit table is full the creation fails and the address you saved points at something else. 0x6283F0 is the current unit count, so only create while it is at most 1698. StarCraft's limit is 1,700 and past it you get "cannot".

Which is why it is worth having one wrapper that checks whether the creation actually happened.

function spawnUnit(unit: TrgUnit, loc: TrgLocation, player: TrgPlayer) {
    if (Memory(0x628438, Exactly, 0)) return 0;      // 테유닛 테이블이 가득 차음 / table full
    const ptr, epd = cunitepdread_epd(EPD(0x628438));
    CreateUnit(1, unit, loc, player);
    if (Memory(0x628438, Exactly, ptr)) return 0;    // 아무것도 안 생김 / nothing was made
    return epd;
}

The comparison must be against the pointer. 0x628438 holds a pointer and an EPD can never equal it, so code comparing against the EPD is false forever - and wrapped in a retry loop it is an infinite loop. This is a reported mistake, not a hypothetical.

And retrying a failed create in the same pass is pointless: a “cannot” or an unplaceable location keeps failing for the rest of the frame, so a while loop that waits for success never ends. A create that must succeed has to wait across frames. And on an unlimiter map or near the sprite limit a unit can exist with no sprite, so check epd + 0xC/4 is at least 1 before touching anything graphical.

Holding it is the harder half. When the unit dies its slot is reused by the next unit, and the address you stored now points at a different unit entirely. The address did not change; its occupant did.

The uniquenessIdentifier at +0xA5 exists for this. It increments each time the slot is reused, so storing it alongside the address and comparing on use is what tells apart "the same slot, a different unit".

Never dereference the structure offset of a unit that has died - crash list #9. A stored handle has to be checked before use: if the CSprite pointer at +0x0C is 0, there is no unit in that slot.

Which way you make a CUnit depends on what you already have, and the choice is about cost rather than taste: some forms emit no triggers at all, others cost a read or a division.

What you have / what to use
EPD → CUnit(epd)Costs nothing - it is only retyping.
Both EPD and ptr → CUnit(epd, ptr=ptr)Also free, and later code that wants the pointer does not recompute it.
A function argument or return value → CUnit.cast(x)Free.
An address that holds a structure pointer → CUnit.from_read(its EPD)One read; the result is 0 when the slot is empty.
ptr → CUnit.from_ptr(ptr)The pointer-to-EPD division, cached at that call site.
const unit = CUnit.from_read(EPD(0x628438));   // 다음에 생성될 유닛 / the unit about to be created
const sel  = CUnit.from_read(EPD(0x6284B8));   // 이 기계의 선택 / this machine’s selection
const shr  = CUnit.from_read(EPD(0x6284E8));   // 공유 선택 / the shared selection
from_read exists for these three slots. It is exactly the two lines of reading with cunitepdread_epd and then making a CUnit.

For a function that takes a unit, declare the parameter as CUnit and write f(CUnit.from_ptr(p)) at the call site: the division then only happens when p has actually changed. A function that takes a raw pointer divides on every call.

And every member of CUnit that points at another unit is itself a CUnit - prev, next, moveTarget, orderTarget, subUnit, connectedUnit, addon, parent, rallyUnit, harvestTarget and the rest. That is why pointer-chasing code contains no EPD arithmetic at all.

worker.connectedUnit.resourceAmount -= bonus;   // 한 줄 / one line
unit.subUnit.order = py_str("Die");
// 대입할 때도 CUnit을 그대로 넣으면 됩니다. .ptr 필요 없음
// assigning a CUnit stores the pointer; .ptr is never needed on either side
unit.rallyUnit = target;
Three ways to read the struct wrongStructure offsets