Programming Basics
If you already know how to program, most of what you would write in code can be written with actions, filters, and variables. This page shows how common code is written in PGM.
| Concept | PGM equivalent |
|---|---|
| Variable | <variable> |
| Condition | Filter |
| Block of code | <action> |
| Function | <function> |
| Event listener | <trigger> |
All values are numbers. Like in C, there is no boolean type.
In the examples below, condition is any filter, expr is any expression, and ... is more actions.
How It Runs
Actions are usually started by a <trigger>, which runs an action when its filter starts allowing.
That action can then run other actions and functions.
Every action runs for one match, team, or player, set by its scope.
This works like this: in a player action, player_x is that player's value.
Values
Variables
Variables are global, like global state in other languages.
They are defined in their own <variables> tag, outside of any action, and keep their value for the whole match.
The scope can be match, team, or player, to store one value for the match, each team, or each player.
var x = 0;
var y = 5;
<variable id="x" scope="match"/>
<variable id="y" scope="match" default="5"/>
Actions can use variables from their own scope or any scope above it.
Arrays hold a fixed number of values, set by a number from 1 to 1024.
var arr = new Array(10);
<array id="arr" scope="match" size="10"/>
Local variables are declared with locals on an <action>, <repeat>, or <function>, and start at 0 each time it runs.
let x;
<action locals="x">...</action>
Assignment
Variables are changed with the <set> action.
x = expr;
x += expr;
<set var="x" value="expr"/>
<set var="x" value="x + expr"/>
Arrays also take an index.
arr[i] = expr;
<set var="arr" index="i" value="expr"/>
Assignments can also be written inline with :=, anywhere an action is referenced.
x = expr;
<trigger filter="condition" scope="match"
action="x := expr"/>
Control Flow
Conditions
Conditions are filters.
A filter defined in <filters> is used by its id.
if (holdingGap) { ... }
<action filter="holding-gap">...</action>
Filters can also be written inline, to combine other filters or compare numbers.
Combining Conditions
if (a && b) { ... }
if (a || b) { ... }
<action filter="all(a, b)">...</action>
<action filter="any(a, b)">...</action>
if (!a) { ... }
<action filter="not(a)">...</action>
Comparing Values
if (x == 5) { ... }
if (x != 5) { ... }
<action filter="x=5">...</action>
<action filter="not(x=5)">...</action>
Greater and less than are written as ranges, with .. on the open side.
The values in a range must be numbers, not variables or expressions.
if (x >= 5) { ... }
if (x <= 5) { ... }
<action filter="x=5..">...</action>
<action filter="x=..5">...</action>
Inline filters only use inclusive ranges, so x > 5 is written as x=6.. for whole numbers.
For decimals, use not on the opposite range.
if (x > 5) { ... }
if (x < 5) { ... }
<action filter="not(x=..5)">...</action>
<action filter="not(x=5..)">...</action>
Ranges & Expressions
if (x >= 1 && x <= 10) { ... }
<action filter="x=1..10">...</action>
The left side of a filter can be an expression, as long as it has no parentheses.
Inside all, any, or not, it also can't have spaces.
Filters with an expression only work in actions, not in triggers.
if (x == y) { ... }
if (expr >= 5) { ... }
<action filter="x-y=0">...</action>
<action filter="expr=5..">...</action>
If Statements
Actions only run when their filter allows, so a filter works like an if statement.
if (condition) { ... }
<action filter="condition">...</action>
Some single actions can use a filter directly.
if (condition) return;
if (condition) return expr;
<return filter="condition"/>
<return filter="condition" value="expr"/>
if (condition) break;
if (condition) continue;
<break filter="condition"/>
<continue filter="condition"/>
if (condition) f();
<call filter="condition" function="f"/>
If-Else
An else is a second action with the opposite filter.
if (condition) {
...
} else {
...
}
<action filter="condition">...</action>
<action filter="not(condition)">...</action>
Each filter is checked when its action runs, so if the first action changes a variable the condition uses, both can run.
Loops
<repeat> runs its actions a number of times.
for (let i = 0; i < n; i++) { ... }
<repeat times="n" index="i">...</repeat>
<switch-scope> runs its actions once for each team or player.
for (const player of team.players) {
...
}
for (const player of match.players) {
...
}
<switch-scope outer="team" inner="player">
...
</switch-scope>
<switch-scope outer="match" inner="player">
...
</switch-scope>
for (const team of match.teams) {
...
}
<switch-scope outer="match" inner="team">
...
</switch-scope>
Loops can be stopped or skipped ahead.
break;
continue;
<break/>
<continue/>
<break/> only stops the innermost loop.
While
There is no while loop, but a <repeat> with a <break/> can be used, up to a maximum number of iterations.
while (condition) {
...
}
<repeat times="100">
<break filter="not(condition)"/>
...
</repeat>
Functions & Events
Functions
Functions are reusable actions with parameters and an optional return value, defined in the <actions> tag.
function f(a, b) {
...
}
<function id="f" scope="player" params="a,b">
...
</function>
Functions are run with <call/>, which can store the returned value.
f(1, 2);
x = f(1, 2);
<call function="f" a="1" b="2"/>
<call function="f" a="1" b="2" result="x"/>
return expr;
<return value="expr"/>
A function's scope must match the caller's.
Functions can not see the caller's local variables.
Events
A trigger runs an action when its filter starts allowing, like an event listener.
condition.on("allow", () => { ... });
<trigger filter="condition" scope="match" action="..."/>
Triggers run separately for each player, team, or match, each time their filter changes from deny to allow.
A schedule runs its actions later, up to 1 minute.
setTimeout(() => { ... }, 5000);
<schedule after="5s">...</schedule>
Variables are shown in messages using a <decimal> replacement.
console.log(`${x}`);
<message text="{x_text}">
<replacements>
<decimal id="x_text" value="x"/>
</replacements>
</message>
Full Example
This function checks if a number is prime, returning 1 if it is and 0 if it is not.
Every value in PGM is a double, so the code is written with decimals to match.
function isPrime(number) {
let divisor;
if (number == 2.0) return 1.0;
if (number < 2.0 || number % 2.0 == 0.0) return 0.0;
for (let i = 0.0; i < number; i++) {
divisor = (i * 2.0) + 3.0;
if (divisor * divisor > number) break;
if (number % divisor == 0.0) return 0.0;
}
return 1.0;
}
<function id="is-prime" scope="match" params="number" locals="divisor">
<return filter="number=2" value="1"/>
<return filter="any(number=..2, number%2=0)" value="0"/>
<repeat times="number" index="i">
<set var="divisor" value="(i * 2) + 3"/>
<break filter="divisor * divisor - number = 1.."/>
<return filter="number % divisor = 0" value="0"/>
</repeat>
<return value="1"/>
</function>
x = isPrime(7.0);
<call function="is-prime" number="7" result="x"/>
Limitations
- Every variable is a double or an array of doubles.
- Expressions can't use comparisons or logic like
==or&&. Use a filter instead. - Every loop has a maximum number of iterations.
- A schedule can wait up to 1 minute.
- Another player's variables can only be used by looping over players with
<switch-scope>.