Math
Eventra does its sums inside math(…). Anything with +, -, * or / goes in there,
so you can always spot where a calculation happens.
Syntax
Section titled “Syntax”get answer = math(_2 + _3) // your calculation goes inside the bracketsExample
Section titled “Example”run: get apples = _5 get pears = _3 system.log:message("Fruit:", math($apples + $pears)) system.log:message("Half:", math($apples / _2))close:The order things are worked out
Section titled “The order things are worked out”Inside math(), Eventra works things out in the order you learned at school: brackets
first, then * and /, then + and -. Brackets change the order:
run: system.log:message(math(_2 + _3 * _4)) system.log:message(math((_2 + _3) * _4))close:Powers with **
Section titled “Powers with **”** raises a number to a power, so _2 ** _3 is 2 × 2 × 2. It’s worked out before *
and /. When there are two, the right one goes first:
run: system.log:message(math(_2 ** _3)) system.log:message(math(_2 ** _3 ** _2))close:Negative numbers
Section titled “Negative numbers”Put - in front of a number or a variable to make it negative:
run: get temperature = _3 system.log:message(math(-$temperature - _5))close:Changing a number: +=, -=, *=, /=
Section titled “Changing a number: +=, -=, *=, /=”To change a variable by an amount, use += and its friends. coins += _5 is short for
coins = math($coins + _5):
run: get coins = _10 coins += _5 system.log:message("After +=", $coins) coins -= _3 system.log:message("After -=", $coins) coins *= _2 system.log:message("After *=", $coins) coins /= _4 system.log:message("After /=", $coins)close:Rounding
Section titled “Rounding”math.round gives the nearest whole number. Give it a second number, the step, and
it rounds to the nearest multiple of that instead: the nearest 25, or the nearest 0.01.
run: system.log:message(math.round(_7.6)) system.log:message(math.round(_237, _25)) system.log:message(math.round(_3.14159, _0.01))close:Three more work the same way, each going a different direction:
math.flooralways goes down,math.ceilalways goes up,math.truncgoes towards zero, so it just drops what’s after the point.
math.trunc works like math.floor on positive numbers, and like math.ceil on
negative ones. Negative numbers are where they’re easiest to mix up:
run: system.log:message(math.floor(-_7.5), math.ceil(-_7.5), math.trunc(-_7.5), math.round(-_7.5))close:Random numbers
Section titled “Random numbers”math.random(low, high) picks a whole number from low to high, both included, so
math.random(_1, _6) is a dice roll: 1, 2, 3, 4, 5 or 6. It’s random, so each run is
different. The panel here shows one roll. Yours will be whatever comes up.
run: get roll = math.random(_1, _6) system.log:message("You rolled", $roll)close:How it works
Section titled “How it works”Keeping sums inside math() means a stray + can’t sneak into a line by accident: a
calculation always looks like one. If you do write one outside, Eventra doesn’t guess
what you meant. It stops before running anything and tells you where to put the
brackets.
Whole numbers stay whole with +, - and *, and / always gives a decimal.
Rounding is how you get back to a whole number when you need one, like a score or a
number of lives.
Common mistakes
Section titled “Common mistakes”Arithmetic outside math(). It’s the most common one.
run: get apples = _5 system.log:message($apples + _1)close:Eventra syntax error (line 3): arithmetic has to go inside math(...) — write math(... + ...) insteadFix: system.log:message(math($apples + _1)).
A typo in a variable’s name.
run: get apples = _5 system.log:message(math($aples + _1))close:Eventra runtime error (line 3): variable 'aples' has not been declared — write 'get aples = ...' before using $aplesFix: spell it the way it was declared, $apples.
Dividing by zero.
run: get people = _0 system.log:message(math(_10 / $people))close:Eventra runtime error (line 3): division by zeroFix: check first, with CheckIf($people != _0) then do (see Conditions).
Writing **=. There’s a +=, but no **=.
run: get size = _2 size **= _3close:Eventra syntax error (line 3): Eventra has no '**=' — write it out: size = math($size ** _3)Fix: the message shows you what to write instead.
A number that’s too big. Eventra’s whole numbers go up to 9223372036854775807.
run: get stars = _9223372036854775808close:Eventra syntax error (line 2): 9223372036854775808 is too big for a whole number — the biggest is 9223372036854775807. For a bigger number, write it as a decimal: 9223372036854775808.0Fix: if you really need a number that big, the message shows you how to write it.
Next: Conditions →, doing something only when it’s true.
