SCRMaps
/
Sign in

Loops and branches: for, foreach, once, epdswitch

A loop that unrolls at compile time and a loop that runs in game are different things, and confusing them misuses both.

11 min read

Elsewhere this course says loops are barely used. That is not true of modern scripts: epScript has for, while, foreach, switch, epdswitch and once, and each has a clear job.

The important distinction is foreach. Over a compile-time iterator - py_range, a Python list - foreach is unrolled at compile time: the code is copied out, which is why break and continue do not work there.

foreach (i : py_range(5)) {
    simpleprint(i + 1);
}
// 컴파일 결과는 이것과 같습니다
simpleprint(0 + 1);
simpleprint(1 + 1);
simpleprint(2 + 1);
simpleprint(3 + 1);
simpleprint(4 + 1);
It unrolls. A hundred iterations is a hundred copies of the code.

So the iteration count of a compile-time foreach is map size: py_range(1000) makes a thousand copies of the triggers. When you need the loop to run in game, use a runtime iterator such as EUDLoopUnit, or for and while.

A foreach over a runtime iterator is a different thing entirely, and the official explanation settles it: the compiler steps the iterator exactly twice, laying an EUDWhile and an EUDEndWhile around the body.

foreach (cu : EUDLoopNewCUnit()) { py_print(cu); println("{}", cu); continue; }

// 대략 이것과 같습니다 / roughly equivalent to
const it = EUDLoopNewCUnit();
const cu = py_next(it);   // 첫 번째 스텝: EUDWhile을 여는다 / opens an EUDWhile
    py_print(cu);          // 컴파일 타임 — 빌드 때 한 번 / compile time, once at build
    println("{}", cu);     // 런타임 — 반복마다 / run time, per iteration
    EUDContinue();
py_next(it, 0);           // 두 번째 스텝: EUDEndWhile / closes it
The body is emitted once at compile time. It is not copied per unit.

Two things follow. break and continue work here because they compile into EUDBreak() and EUDContinue(), which is also why they do not work over a compile-time iterator. And a py_print in the body firing once while your println fires 300 times is not a bug - it ran once, at build.

once runs its body one time, the moment its condition is met, and never again. It is how you say once, eventually inside something like beforeTriggerExec that runs every frame.

once (ElapsedTime(AtLeast, 60)) {
    printAll("1분이 지났습니다");
}

once {
    // 조건 없이, 딱 한 번만
}
With a condition or without one.

Do not put once inside a player loop. once works by detaching the trigger after it fires - which is why it costs nothing afterwards - so it cannot mean "once per player". The first player to satisfy the condition consumes it for everybody.

"Once per player" needs per-player state - and CurrentPlayer already points at a different address for each player, so one death counter lets one trigger do six players' work.

const done = $U("Cave");   // 맵에 절대 나오지 않는 유닛 / a unit the map never spawns

foreach(p : EUDLoopPlayer()) {
    setcurpl(p);
    if (Deaths(CurrentPlayer, Exactly, 0, done)
        && Accumulate(CurrentPlayer, AtLeast, 10000, Ore)) {
        SetDeaths(CurrentPlayer, SetTo, 1, done);
        printAll("1만 미네랄 달성");
    }
}
Change Exactly 0 to AtMost 254 and the same shape becomes "at most 255 times per player".

It can seem faster to copy the triggers once per player. For six players that is making six copies of the whole body to save executing a handful of loop triggers six times - a bad trade. Inlining pays where the repeat count is large: in a 1700-iteration unit loop, every trigger removed from the body saves 1700 executions.

epdswitch branches on the value at one memory address. When behaviour depends on which unit it is, this is what you write instead of a stack of ifs.

const unitId = epd + 0x64 / 4;
epdswitch (unitId, 255) {
    case $U("Terran Marine"):
        break;
    case $U("Terran Ghost"):
        break;
}
The second argument is a mask. A unit id is one byte, so 255.

An ordinary switch takes a mask in the same place: switch(x, 0xff) compares x & 0xff. A single case can also list several values separated by commas, and a case without a break falls through into the next one.

switch (day) {
    case 4:
    case 5:          // 4와 5가 같은 동작 / 4 and 5 share this
        printAll("주말이 다가옵니다");
        break;
    case 0, 6:       // 한 줄에 두 값 / two values on one label
        printAll("주말!");
        break;
    default:
}
An empty switch body is not an error either.

A case label has to be a constant. "I want the case to come from an array" is the question that follows immediately, and the answer is not a variable - it is to stamp the cases out at compile time.

const UNIT_ID = list(1, 10, 15);
const UNIT_HP = list(100, 1000, 150);

EPDSwitch(cunit + 25, 0xFF);                 // 25 == 0x64/4, 유닛 종류
foreach(id, hp : py_zip(UNIT_ID, UNIT_HP)) {
    EUDSwitchCase()(id);
    cunit.hp = 256 * hp;
    break;
}
EUDEndSwitch();
The foreach unrolls at compile time and stamps out the three cases. Change the lists and the cases follow.

And inside a unit loop, if (cunit == 1) is not comparing the unit type. Used as a value, cunit is its EPD address - so this does not error, it is just silently wrong. For the type, use cunit.unitType.