SCRMaps
/
Sign in

Detecting a button press with the build queue

The modern standard for a skill button: press a production button, read the queue, clear it.

18 min read

StarCraft has no "a button was pressed" condition. So you make the button change something that is guaranteed to change, and read that instead. The thing you read is the unit production queue.

  1. 1Put a unit-production button in a free slot of the unit's button set. Copying the Barracks' Marine button and changing only the unit is the easy way.
  2. 2Scope the requirement to that unit - "Current unit is... Jim Raynor". Do not set it to always allowed.
  3. 3Every cycle, read the selected unit's queue, and run the skill if something is in it.
  4. 4Put the queue back, whether or not you read anything.

You do not need a unit loop. A production button can only be pressed on the unit you clicked, and selecting several units hides the button entirely - so the only thing to check is the one unit each player has selected.

foreach(player : EUDLoopPlayer("Human")) {
    // 0x6284E8 + 48 * p 에 선택한 유닛의 구조오프셋이 들어 있습니다.
    const ptr, epd = dwepdread_epd(EPD(0x6284E8) + 12 * player);
    if (ptr > 0) {
        if (MemoryEPD(epd + 0x64 / 4, Exactly, $U("Jim Raynor (Marine)"))) {
            if (MemoryEPD(epd + 0x98 / 4, Exactly, $U("Terran Civilian") * 65536)) {
                // 버튼이 눌렸습니다. 스킬은 여기에.
            }
            // 인식 여부와 상관없이 매번 되돌립니다.
            dwwrite_epd(epd + 0x98 / 4, 0 + 228 * 65536);
            dwwrite_epd(epd + 0x9C / 4, 228 + 228 * 65536);
        }
    }
}
One selected unit per player. A 48 byte stride is 12 in EPD terms.
The queue lives in the structure offset
+0x64The unit's own id - check you are holding the right unit first.
+0x98Queue slots 0 and 1, two bytes each.
+0x9CSlots 2 and 3.
+0xA0Slot 4 - and the other two bytes are the unit's mana. Do not overwrite this one wholesale.
228An empty slot. Not 0 - 228.

Never empty the queue completely. Setting every slot to 228 is the same as cancelling production: the unit goes invisible and immobile, or the game crashes. So slot 0 always holds a Marine (0) and the press is detected in slot 1 - which is why the code above writes 0 + 228*65536 to 0x98.

Though "never empty it" is not the whole story. The BuildResetXEPD that ships with eudplib does set all five slots to 228 - and resets the secondary order at 0xA6 to 2, Idle, instead. That is where the invisible unit actually comes from: the queue is empty but the unit is still in the "producing something" state, drawing a sprite for making a unit that is not there. Reset the secondary order as well and emptying the queue is fine.

Choosing between them is simple. Keeping slot 0 filled works in classic triggers too and there is less to read. Emptying everything disposes of the rotating five slots and buildQueueSlot at 0xA4 in one move - and for that one, use BuildCheckXEPD and BuildResetXEPD rather than writing it yourself.

The reset sitting outside the detection branch is deliberate too: the detection only matches when slot 0 already holds a Marine, so the reset has to have run at least once for anything to be detectable.

Do not do this on a building that can really produce. Real production happens when the advanced info has Building checked and Zerg unchecked (bar the Infested Command Center), or on a Reaver or Carrier and their heroes. Detect a queue on a Barracks and an invisible unit really is created; it eats a unit index, clearing the queue does not remove it, and eventually you get "cannot" errors. This is item #28 on the crash list.

  • If the unit can really produce, use an upgrade button and detect the upgrade instead.
  • If you must use the queue anyway, the reset has to kill that unit too - with KillUnit, not KillUnitAt.

If you have to detect a queue on a building that really does produce - a Nexus you let players build something from, say - there is one more model to know: the queue and the unit currently in production are independent of each other.

All four combinations happen
A queue with nothing in productionHappens without EUD at all - supply-blocked, or in the “cannot” state.
An empty queue with something in productionOnly through EUD: a queue reset run on something already in production.
Both, or neitherBoth, or neither - obviously.

So clear them separately and do not infer one from the other. The way to remove the unit in production is to set that unit's own order to Die - not to zero the building's currentBuildUnit field. Confusing the two is a real, reported bug whose symptom is units copying themselves forever.

const nexus = CUnit.cast(NEXUS[$P1]);
if (nexus.currentBuildUnit != 0) nexus.currentBuildUnit.order = "Die";
if (!nexus.are_buildq_empty()) {
    if      (nexus.check_buildq("Protoss Scout"))   { /* ... */ }
    else if (nexus.check_buildq("Protoss Corsair")) { /* ... */ }
    nexus.reset_buildq();
}

And the sentence that explains all of it: a unit appears when production starts, not when it finishes. Clear only the queue and leave currentBuildUnit alone and one invisible unit accumulates per detection - a symptom with a name. A “ghost unit”: it looks ordinary, cannot be clicked, does not attack, ignores every trigger and will not die.

How far a production has got is not where people look either: the remaining build time is on the unit being produced, not on the building - read 0xAC off a finished building and it is 0, because that field is a building under construction reporting its own progress. Upgrades are the other way round and are on the building, at 0xC6 and 0xC9, which is what makes the analogy mislead.

const inProgress = epdcunitread_epd(epd + 0xEC/4);
const remaining  = wread_epd(inProgress + 0xAC/4, 0);
Check 0xEC is non-zero first even when the queue is not empty: supply or a “cannot” can leave a queue with nothing actually started.
Three reasons the infinite copy appears
The queue has five slotsThere are five slots. Clearing 0x98 alone clears slots 1 and 2 and leaves three behind.
Slot 1 is not the frontSlot 1 is not the front: the five rotate, and buildQueueSlot at 0xA4 holds which one is.
Packets are batched into one turnCommands are batched into one turn, so even a per-frame reset can find more than one new entry. Button mashing or a production completing is enough.

And if what you are building is a button that upgrades one unit, order detection is the better choice over queue detection. There are two reasons and both are dead ends.

  • The button requirement "is not training or morphing" only applies to buildings. It does not bind on a unit, so you cannot hide the button while the upgrade runs.
  • Cancel Last does not work on a unit either, so there is no stock cancel button.

Order detection fixes both: several units can be dragged and upgraded at once, and a cancel button can be shown by the Cancel Last button condition while its action runs a different order detection.

There is one case where rewriting the queue is the only route at all: making a Carrier produce something that is not an Interceptor. Adding a button does not help, because the obstacle is the format of the order rather than the data.

The ordinary train order takes a UnitType argument. The Interceptor/Scarab train order does NOT. So a button for a different unit on a Carrier can only ever say “train interceptor” when pressed, and an Interceptor is what you get.

  1. 1Read buildQueueSlot at 0xA4 - it says which of the five slots is the front, and the five rotate.
  2. 2Walk the five from there: slot 3 means 3, 4, 0, 1, 2.
  3. 3Where the entry is a normal Interceptor, write 228 (0xE4) over it - that cancels and refunds it.
  4. 4Raise buildQueueSlot by one and put your unit in the first EMPTY slot after it.
  5. 5Pay for your unit yourself - the refund is the budget.

A “hero interceptor” made this way is not a real Interceptor, and the union at 0xC0 is where that shows. Some of what came out of that is useful elsewhere.

  • Real Interceptors form a chain: the Carrier points at the most recently produced or launched one, and they link to each other. BWAPI has the Interceptor's prev and next the wrong way round - prev holds the NEXT one's address - and inHangar is 1 when the Interceptor is OUTSIDE.
  • On a fake Interceptor every one of those fields reads 0, and writing parent does not hold - it goes back to 0. Raszagal, Bunker, Barracks and Academy were all tried as the carrier type and none worked.
  • Which means the Interceptor button does not hide. It hides when linked plus queued reaches 8, and with fakes the linked count is 0, so it only hides once 8 completed ones exist - at 7 a player can still queue five more. You cannot block the button, so you delete from the queue instead.
  • The unit currently in production is at CUnit 0xEC, buildUnit. If that is a stock Interceptor, kill that one.
  • With more than one hero Carrier you must store which Carrier each fake belongs to yourself, because the union will not hold it. Write it at the moment the Carrier's 0xEC is your unit.

The count that matters is inside + outside + queued <= 8. Inside is 0xC8, outside is 0xC9, and the queue has to be walked. Count only one of the three and the player exceeds the limit through the others.

Crash list item #28