Reference
Formula operators and functions
Everything the formula editor offers is listed here. In Text mode you type the symbols shown in the Typed as column. In Blocks mode you pick the same things from the palette. Try them out in Create a formula.
Arithmetic
| Operation | Typed as | Meaning |
|---|---|---|
| Add | + | Add two values |
| Subtract | - | Subtract the second from the first |
| Multiply | * | Multiply two values |
| Divide | / | Divide the first by the second |
| Remainder | % | Remainder after dividing |
| Power | ^ | Raise to a power. 2 ^ 3 ^ 2 is read as 2 ^ (3 ^ 2) |
Comparison
Comparisons give true or false. They don't chain, so write a < b && b < c rather than a < b < c.
| Operation | Typed as | True when |
|---|---|---|
| Equal | == | Both are equal |
| Not equal | != | They differ |
| Less than | < | The first is smaller |
| At most | <= | The first is smaller or equal |
| Greater than | > | The first is larger |
| At least | >= | The first is larger or equal |
Logic
| Operation | Typed as | Meaning |
|---|---|---|
| Both true | && | True when both are true |
| Either | || | True when either is true |
| Negate | ! | Flips true and false |
Functions
| Function | Meaning |
|---|---|
round(value) | Round to the nearest whole number |
floor(value) | Round down |
ceil(value) | Round up |
abs(value) | Remove the minus sign |
min(a, b) | The smallest of several values |
max(a, b) | The largest of several values |
clamp(value, min, max) | Keep a value between a minimum and maximum |
lerp(a, b, t) | Blend two values by an amount from 0 to 1 |
remap(value, in_min, in_max, out_min, out_max) | Map a value from one range to another |
sqrt(value) | Square root |
log(value, base) | Logarithm, natural or with a base |
exp(value) | e raised to a power |
sign(value) | Minus one, zero or one, by sign |
Random functions
These roll fresh every time they are worked out. See Random values for how to use them.
| Function | Meaning |
|---|---|
random(min, max) | A whole number from min to max, both included. Reversed bounds are swapped |
sum_of(count, value) | Work out value count times and add the results |
sum_highest(count, keep, value) | Work out value count times and add the highest keep results |
sum_lowest(count, keep, value) | Work out value count times and add the lowest keep results |
count_at_least(count, value, target) | Work out value count times and count the results at or above target |
Count, keep, min and max must be whole numbers. The value can't call another formula. A count above 1,000 is treated as 1,000.
Choosing between values
if condition then a else b picks one of two values. For example, if two_handed then 1.35 else 1 gives 1.35 for two handed weapons and 1 for the rest.
What you can refer to
- A field of the object type, by its key, such as
damage. - A parameter, with a
$in front, such as$armour. - Another formula on the same object type, with brackets, such as
base_damage().
For the type each of these gives, see Formula types.