SCRMaps
/
Sign in

Desync, and the local player

Drive a change everyone shares from a condition only one machine can see, and everyone else drops.

21 min read

Multiplayer StarCraft works by every machine computing the same game separately. Only input is sent; everything else is assumed to come out identical everywhere. The moment one machine computes something different the games have diverged, and StarCraft drops whoever diverged.

In an EUD map the reason this bites is simple: some values are the same for everybody, and some exist only on this machine.

Shared and not shared
SharedUnits, resources, death counts, everything on the map. Every machine holds the same value.
LocalMouse position, screen position, key presses, the selected unit, and who you are. Different on every machine.

Never fire a shared action from a local condition. This is item #13 on the crash list and it is the most common reason an EUD map drops. A condition only your machine can see - a key is down, the mouse is here - driving a unit creation or a resource grant means it happens on your machine alone, and everyone else drops.

IsUserCP() sits exactly on that line. It asks whether the player currently being processed is the one sitting at this machine, so it is true for exactly one slot per machine.

// 안전합니다: 로컬 조건, 로컬 효과
if (IsUserCP()) {
    setcurpl(getuserplayerid());
    DisplayText("당신에게만 보이는 글");
}

// 게임을 드랍시킵니다: 로컬 조건, 공유 효과
if (IsUserCP()) {
    CreateUnit(1, "Terran Marine", loc, CurrentPlayer);  // 절대 금지
}
Putting text on screen is local. Creating a unit is shared.

A real case: in Super Turret Defense a block that updated shared game state sat inside if (IsUserCP()), so it ran for a different single slot on each machine. Every computer computed something different and the whole game dropped. The code looked fine and worked perfectly in a solo test - because when you are alone, every slot is your slot.

You cannot find a desync alone. With one player there is nobody to diverge from. Any map that touches a local value has to be tested with at least two people.

Which action belongs to which side has to be learned, and the official reference splits them in two. The first group only takes effect on the machine whose owner is the current player, so running it on one machine is safe. The second group has to execute identically everywhere; run it on one machine only and the rest drop.

The synchronisation rule
Safe under a local conditionSafe from a local condition: DisplayText, CenterView, PlayWAV, MinimapPing, TalkingPortrait, Transmission, SetMissionObjectives
Must run on every machineMust run on every machine: SetAllianceStatus, RunAIScript, RunAIScriptAt, Draw, Defeat, Victory

Note what is in the second group: Defeat and Victory. Driving "defeat this one player" from a local condition drops the room rather than the player. To lose one player, the condition itself has to be the same on every machine - decide it from shared state such as a death count.

When a room splits, there are only three places to look. Take the shared action and ask which of three ways it could differ between machines.

The three ways
The value handed over differsThe number handed to the action differs. If a variable or array cell holds 11 in one player's StarCraft and 22 in another's, the same line adds a different amount.
The player it applies to differsThe player it applies to differs - CurrentPlayer was set from an unshared value, or the code that sets it sits under an unshared condition.
Whether it runs at all differsWhether it runs at all differs, because an unshared condition somewhere above gates it.

"Differs between machines" does not mean array[0] and array[1] hold different numbers. It means the same slot holds a different value in each player's StarCraft. And it applies to the index as well as the value: even an identical array read as array[my own player id] reads a different slot on each machine.

Creating a unit under an unshared condition is the textbook case of the first and third together: one machine has a unit the others do not, so the game state itself differs. Keep only the genuinely local work inside, and move the shared work out - setting a switch inside and acting on it outside is the standard shape.

Finally, the Bring and Command cache can split a room by itself. Run a Command under an unshared condition and the cache refreshes on that machine only, so an identical Bring immediately after answers differently for one player. The code is character-for-character the same everywhere and the results diverge - unfindable until you remember that the cache is state.

So what do you do when a value only one machine knows - a mouse position, a key - genuinely has to affect shared state? You do not infer it. You put it into StarCraft's own outgoing packet queue and send it.

StarCraft broadcasts that queue periodically, so what one machine knew arrives at all of them by the same path a mouse click takes. This is not a loophole: the value becomes an input, and every machine still computes the same game from the same inputs. It is exactly how MSQC gets a key press to everybody.

That packet list is public, with every command's opcode and argument layout. Anything outside the named helpers goes straight into QueueGameCommand.

Two things are easy to get wrong about QueueGameCommand_Select. First, its argument is an array of CUnit pointers, not EPDs - the official reference spells that out, which tells you how often it is got wrong. Second, it does not select anything on your own screen: all it does is tell the other machines that this player has selected those units, and what makes them act is the packet you send next - a right click, a train order, whatever.

const uar = EUDArray(12);
if (playerexist(P1) && GetPlayerInfo(P1).type == 0x06) {   // 그 자리가 사람 자리로 설정되어 있고, 지금 사람이 있는가
    uar[0] = dwread_epd(EPD(0x628438));                 // 포인터를 먼저 잡고 / grab the pointer first
    CreateUnitWithProperties(1, "Terran Command Center", "Location 1", P1, UnitProperty(invincible = true));
}
if (getuserplayerid() == $P1) {                          // 내가 보내는 기계인가 / am I the machine sending?
    QueueGameCommand_Select(1, uar);
    QueueGameCommand_QueuedRightClick(1234 + 2345 * 65536);
}
Three conditions asking three different things at different times. GetPlayerInfo is evaluated at compile time against the map's own player settings, so it asks whether that slot is configured as a human; playerexist asks at run time whether anyone is actually in it; and getuserplayerid asks, on that machine only, whether this is the one that should send. Leave the last one out and every machine sends the same packet, once per player.
Commands worth knowing
0x14 right clicks16 x, y; u16 targetId; u16 unitType; u8 queued.
0x15 actions16 x, y; u16 unitId; u16 unitType; u8 order; u8 queued - most of the named helpers are special cases of this one.
0x0C buildu8 orderType; s16 x, y; u16 unitType - and the coordinates are in tiles, not pixels.
0x13 control groupu8 type (0 save, 1 recall, 2 add); u8 slot.
0x0D vision · 0x0E allianceBitmasks: vision is (v >> player) & 1, alliance (a >> player*2) & 3.

The order byte in 0x15 uses the same numbering as the struct's order field. So “make the player issue this order” and “write the order into the struct” are two routes to the same place, and only the first is shared by construction.

And what the game compares to decide a room has split is known too: the 0x37 sync packet rotates through a few hashes.

What the sync packet hashes
UnitEvery unit's type, shields, energy, sprite x and y, and the upper bits of HP.
Resources and the random numberEvery player's ore and gas, the last random number, and elapsed time.
AlwaysFog, player index and vision go into every one of them.

Three things follow. HP is stored times 256 and only the upper bits are hashed, so a divergence smaller than one displayed hit point can sit there unnoticed - “nobody has dropped yet” is not “nothing has diverged yet”. The random number generator is state, so calling rand() a different number of times on different machines desyncs on its own, even if you throw the result away. And fog and vision are shared state, so revealing something for one player under a local condition splits the room although nothing but a screen changed.

Here is how the random-number rule actually gets broken: by calling rand() inside an effect that only one player is meant to see. Screen shake is the classic. The shake moves only the viewer's camera, so putting the whole thing inside the local branch looks natural - and the one rand() in there advances the shared generator on a single machine. The fix is to hoist the roll out of the branch.

// 굴리기는 모두가, 쓰는 것은 한 사람만
// everyone rolls; only one machine uses it
const randX, randY = rand(), rand();
if (IsUserCP()) {
    const rX = (randX % x) - x/2;
    const rY = (randY % y) - y/2;
    ...   // 화면만 흔든다 / move only this screen
}
The point is that rand() sits outside the if. Move that one line inside and the map splits in multiplayer.

The sync packet is filtered out of replays, so a replay never shows you the desync itself - only the commands that led to it.

Finally, a desync that comes from assuming chat is shared. It is not.

The message pointer and pattern value the chatEvent plugin hands you are valid only on the machine of the player who typed. And chat itself is not shared: a whisper, an ally message or a DisplayText already on screen means the message is missing on one client, or lands on a different line and therefore a different address on each. So a number parsed out of chat is unshared data, and writing it straight into a shared array is the textbook desync.

Let MSQC do the carrying - and mind the plugin order, because MSQC can only move a value that already exists this frame.

[chatEvent]        ← 패턴과 주소를 씁니다 / writes the pattern and the pointer
<내 main>         ← 메시지를 num으로 파싱합니다 / parses it into num
[MSQC]             ← num을 공유된 val_arr로 옮깁니다 / promotes num into the shared array
In euddraft that is the order of blocks in the .edd; in EUD Editor 3 it is the drag order in the plugin menu. Get it wrong and it silently does nothing useful.
Taking keyboard input (MSQC)Counting units lags a cycleCrash list item #13