A unit loop is expensive: every cycle it walks 1,700 slots to pick out the ones that match, even though the set it finds barely changes. A UnitGroup is a structure that keeps that set and iterates only it.
The implementation is the interesting part. Triggers are a linked list that points at itself, and EUDBag uses exactly that: it keeps triggers holding between one and 64 actions, in sorted order. The data structure is not an array in memory, it is the trigger chain.
Units in your list die. That is why UnitGroup carries the uniquenessIdentifier, and if you roll your own list you have to make the same check. Without it you command whichever unit moved into the dead one's slot - the symptom is "sometimes the wrong unit moves", and that is very hard to reproduce.
Iterating a UnitGroup includes a block that reaps the dead members, and it is not a formality - it is the last place a dead unit can be read. The member leaves the group when that block ends, so if you want to know where it died, you read it there.
foreach(unit : zerglings.cploop) {
foreach(dead : unit.dying) {
dead.move_cp(0x4C / 4); // 소유 플레이어 / owner
const owner = bread_cp(0, 0);
dead.move_cp(0x28 / 4); // 위치 / position
const x, y = posread_cp(0);
setloc("loc", x, y);
CreateUnit(1, "Infested Terran", "loc", owner);
}
}Before building a data structure out of death counters, look at what is already there: a light array (EUDArray), arrays whose value lives in an action so reads cost the same every time (EUDVArray, PVariable), a fixed-capacity queue and deque (EUDQueue, EUDDeque), a stack (EUDStack) and an object pool.
Making a unit loop cheaper is usually not about the inside of the loop - it is about looping less. The choices come in a ladder.
- 1Put the gate in front of the loop. A Bring tested per index adds a Bring to every index; tested once in front, it usually means no loop at all.
- 2Hand the units to an unused player and iterate only them. If losing the selection is acceptable, GiveUnits to P9-P11 and use EUDLoopPlayerUnit, which walks that player's own chain instead of 1,700 slots.
- 3For a fixed-size location, four DeathsX beat a Bring. If the location moves, refresh those four conditions.
- 4And then stop. A loop that almost never runs is not where the frames are going - look at what runs every cycle.
For a set whose size you do not know, a linked list is the answer. Give each unit a record of its own and use one cell of it as "the next unit".
var head = 0;
function join(epd) {
SetMemoryEPD(epd + dst, SetTo, head); // 내 next = 직전 머리 / my next = the old head
head = epd; // 머리 = 나 / head = me
}| A 1,700-slot array | With an unknown count you need all 1,700 slots, every pass skips the empty ones, and inserting means hunting for a free index. |
|---|---|
| A linked list | A pass costs the number of members. Joining is one action. In exchange there is no random access, no backwards traversal, and deleting needs the previous node. |
| UnitGroup | It already exists and carries the dead-member check for you. Build the list yourself only when you also want per-unit fields living beside the membership. |
EUDLoopUnit2 does not stop early. It skips dead and empty slots but visits every index, in the order units fill: 0, then 1699, 1698, down to 1. A return inside it does leave the rest unvisited, but that is not an optimisation - it means "find exactly one".
All of that was about your own list. StarCraft keeps one too, and it is an ordinary doubly linked list - which means it can be edited. The boldest example is color_detach, which builds arbitrary-coloured units and then UNLINKS them from the game's own used-unit list, after which nothing iterates them and nothing collides with them.
| 0x628430 | The first used unit - the head of the used-unit list. |
|---|---|
| 0x628438 | The free-list head - the slot the next unit you create will occupy. |
Do not simply kill a detached unit. Killing or removing one at either END of the detached list corrupts that list and gives a memory bug. Unlink it from the detached list first, link it back in front of the first used unit, and only then kill it.
While temporarily relinked, MoveUnit works - but currentDirection2 and halt are not handled, so a moved unit can end up facing wrong. And the colours this produces degrade past 4: 5 and 6 come out black, 7 is orange-ish, and beyond that it is unstable.
This is not a recommendation to use it. It is the most invasive technique here, and it is worth reading for two things: as proof that the unit list is an ordinary data structure you may edit, and as a warning about what editing it costs.
Getting a handle on one unit