1 Arithmetic
Victor Westerlund edited this page 2026-08-22 10:25:40 +02:00

Arithmetic

Arithmetic functions mutate the target variable k. The operand v can be either an integer literal or the name of another mcfstd variable.

function var:set { k: "score", v: 10 }
function var:math/add { k: "score", v: 5 }

After this example, score is 15.


Functions

Function Operation Example
var:math/add k = k + v function var:math/add { k: "score", v: 5 }
var:math/sub k = k - v function var:math/sub { k: "score", v: 2 }
var:math/mul k = k * v function var:math/mul { k: "score", v: 3 }
var:math/div k = k / v function var:math/div { k: "score", v: 2 }
var:math/mod k = k % v function var:math/mod { k: "score", v: 4 }

Integer operands

When v is not an existing variable, mcfstd treats it as an integer literal.

function var:set { k: "distance", v: 12 }
function var:math/sub { k: "distance", v: 5 }

distance becomes 7.


Variable operands

When v is an existing variable, mcfstd reads its value from _var.

function var:set { k: "base", v: 10 }
function var:set { k: "bonus", v: 3 }

function var:math/add { k: "base", v: "bonus" }

base becomes 13.


Operand resolution

Arithmetic wrappers call var:isset on v before dispatching to the internal operation.

That means:

  • If a variable named by v exists, v is treated as a variable.
  • If no such variable exists, v is treated as an integer literal.
  • If v is neither an existing variable nor a valid integer literal, the generated scoreboard command will fail.

This also means an existing variable name takes precedence over a same-looking literal.


Division and modulo

var:math/div and var:math/mod use vanilla scoreboard integer operations. Avoid zero divisors.

function var:set { k: "ticks", v: 121 }
function var:math/div { k: "ticks", v: 20 }
function var:math/mod { k: "ticks", v: 6 }

For predictable results, validate divisors before using them:

function var:comp/eq { k: "divisor", v: 0 }
execute if function stdout:false run function var:math/div { k: "total", v: "divisor" }