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.

7 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.

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.

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.
Sign in to track your progress