Skip to content

Preprocessing

Zezombye edited this page Nov 21, 2020 · 7 revisions

Preprocessing is an important part of OverPy and allows extending its power way beyond the limitations of the workshop.

Normal macros

Normal macros are declared with the #!define directive:

#!define TEAM_HUMANS 1
#!define TEAM_ZOMBIES 2

This is primarly useful for declaring constants.

The #!defineMember directive behaves exactly the same as #!define, except the VS Code extension will put the autocompletion in the dot trigger. This is primarly useful for vectors: if you have a vector that stores 3 distinct numbers, you can do #!defineMember someVar x to do vector.someVar instead of vector.x.

Function macros

Function macros are declared with the #!define directive, but have parameters for the macro:

#!define setUsefulVars(x)     hasFirstInfectionPassed = x\
    currentSection = x\
    firstInfectionLoopIndex = x\
    countdownProgress = x\
    roundWinners = x

Note the usage of the backslashed lines to make a multi-line macro.

You must be careful to use parentheses where you must, as macros are a literal replacement!

For example, given this function:

#!define sum(a,b) a+b

then sum(3,4)*3 will be resolved as 3+4*3 and sum(3,4*3) will be resolved as 3+4*3.

This macro should be declared instead as #!define sum(a,b) ((a)+(b)): parentheses around the whole macro definition, and around each argument.

Javascript macros

You can do script macros with the special __script__ function. For example:

#!define addFive(x) __script__("addfive.js")

The content of addfive.js is simply x+5 (no return!)

For the technical details:

  • Arguments are automatically inserted into the script (in this case, var x = 123; would be inserted at the top of the script)
  • A vect() function is automatically inserted, so that vect(1,2,3) returns an object with the correct x, y, and z properties and toString() function
  • The script is then evaluated using eval()