Skip to main content

Functions and expressions

Functions​

function isLate(moment: time): bool
return not moment.between(opensAt, closesAt)
end
  • Parameter and return types are written. A function with a return type must return on every path; the compiler checks.
  • A function may read and write fields and memory, goto, start and stop timers, log and use random; it cannot wait.
  • Recursion is allowed; its depth is limited by the budget.
  • Functions are declared at the script's level.

Statements​

let total = business.visitors + 1 // type inferred, immutable
var count = 0 // mutable
if total > 100 and not hasPower
goto Closed
elseif total == 0
log("empty")
else
business.visitors = total
end
while count < 3
count += 1
end
for i in 1 to 10 // 1..10 inclusive
ring()
end
if count == 0 then return
let shade = if lit then onColor else offColor
  • if … then … is the one-line form; if a then b else c is also an expression.
  • for i in a to b evaluates its bounds once, does not run when a > b, and i cannot be changed in the body.
  • break, continue and return work as usual.
  • Assignment is =, +=, -=, *=, /=. The target is a local var, memory or a writes field.

Operators​

From the highest precedence to the lowest:

Operators
call, .clock().between(a, b)
asspeed as int
unary --x
* / %
+ -x-1 is always a subtraction
== != < <= > >=comparisons do not chain
notnot a == b means not (a == b)
andshort-circuits
orshort-circuits

a.f(b) is the same as f(a, b): clock().between(opensAt, closesAt), neonColor.scaled(0.5).

Operands and arguments are evaluated left to right; in a compound assignment the target first, then the right side.

log​

log(...) (server) writes to Play's log with the world's time. {expression} inserts a value:

log("closing, {business.visitors} visitors today")

A transaction logs at most 20 lines of 400 characters; more is cut and marked. Logging does not change behaviour, and lines of a transaction that fails are kept, marked as failed.