SCRMaps
/
Sign in

What you can do at compile time

Most people know py_eval and stop there. Printing to the build log, reading an external file and generating your own reader all happen at compile time.

12 min read

In epScript almost every Python 3 builtin is available with a py_ prefix - py_len, py_list, py_set, py_map, py_enumerate, py_range, py_str - and two of them are worth singling out.

The two
py_print(...)Prints to the build log. It says nothing about the map and everything about what the compiler decided - compile-time debugging is essentially this one function.
py_open(path, mode)Opens a file at compile time and hands back a Python file object.
function onPluginStart() {
    MPQAddWave("1.wav", py_open("C:/1.wav", "rb").read());
}
This is how an external file gets into the map - and any build-time data, such as a table another tool produced, can be read in and baked without a copy-paste step.
The rest of the toolbox
GetPlayerInfo(player)The map's own settings for that player - type, race, force. Map data, not runtime state.
UnitProperty(...) · GetPropertyIndex(p)Builds a Create Unit with Properties record from script and gives you its number.
SCMD2Text(text)Turns ScmDraft's <03> escapes into the real control bytes, so editor text pastes in unchanged.
GetEUDNamespace()The dictionary of everything registered with EUDRegisterObjectToNamespace - how a plugin and your script find each other.

And of the name-to-index encoders $L, $S, $U and $T, only $T has a side effect.

$T is get-or-create: it returns the id when the text is already in the map string table and inserts a new entry when it is not. $T("Force 1") returns an existing id; a new phrase creates one. Repeating the same literal is free, but every distinct literal costs an STR entry - which is where “the map stopped compiling after I added more text” comes from.

Finally, when a dword you read often holds several packed values, you can generate your own reader. posread_epd is not a special function - it is made this way.

const posread_epd = readgen_epd(
    0x1FFF1FFF,                                                    // 달라질 수 있는 비트 / the bits that can differ
    list(0, py_eval('lambda x: x if x < 65536 else 0')),
    list(0, py_eval('lambda y: y // 65536 if y >= 65536 else 0')),
);
const x, y = posread_epd(epd);
One read, several values out. The saving comes from the mask, because a binary-search read costs a step per bit that could differ. Any two- or three-field dword you read in a hot loop is a candidate.

For moving a block there is repmovsd_epd(dst, src, dwordCount), which beats a loop of reads and writes.

There is a dedicated macro for setting several variables at once, too. SetVariables takes three lists - variables, values, operators - and does the lot in a single trigger.

var a, b, c = 10, 10, 10;
SetVariables(
    list(  a,    b,      c   ),
    list(  3,    2,      4   ),
    list(SetTo, Add, Subtract),
);   // a=3, b=12, c=6
The values may be variables, but they are snapshotted as the action starts and do not change while it runs - which means a swap of two variables written this way works without a temporary.
The compile-time tools people miss
EUDCreateVariables(n)Makes n variables and returns them as a compile-time list. The list does not exist at run time, so its index has to be a constant.
unProxy(x)Takes the raw pointer out of a reference type: EUDArray.cast(unProxy(a) + 4) is a reference to a[1].
MPQAddFile · MPQAddWavePut bytes read with py_open into the output map as a file; isWave runs it through Wave lossy compression first.
SCMD2Text("<03>…<02>")Turns SCMD's <XX> notation into real characters, so a string copied out of the editor can be used as it is.
b2i1·b2i2·b2i4 / i2b1·i2b2·i2b4Convert between byte strings and integers, little-endian. u2b and b2u go between string and byte literal, b2utf8 and u2utf8 handle UTF-8.
GetEUDNamespace().get(name)Fetches an object another module registered with EUDRegisterObjectToNamespace - the official route for passing things between modules.

list() flattens automatically, so it cannot make a nested list; for an empty one use py_list(). And py_list has append, which is what lets you collect actions in a loop and hand the whole lot to DoActions at once.

One data block instead of six arraysFinding what is making the map lag