variables & values
val vs var
val maxHealth = 20 // immutable (can never be reassigned)
var currentHealth = 20 // mutable (can be reassigned)
trying to reassign a val is a runtime error:
val name = "Steve"
name = "Alex" // error: cannot reassign 'val name': it is immutable
use var for anything that changes like counters, flags, current game state, etc. use val for everything else, or don't, you do you
katton has exactly five kinds of runtime value, plus null:
assignment operators
var score = 0
score = 10 // plain assignment
score += 5 // score = score + 5 -> 15
score -= 3 // score = score - 3 -> 12
score *= 2 // score = score * 2 -> 24
score /= 4 // score = score / 4 -> 6
top level vs local
var globalCounter = 0 // top level: shared across every function in this script
function increment() {
var localTemp = 1 // local: only exists inside this call
globalCounter += localTemp
}
null
val item = event.item // might be null if there's no item involved
if (item != null) {
// safe to use item here
}
always check for null before using a value that might not be present: event.item, Katton.pick() on an empty list, and Katton.vars.*.get(...) on an unset key can all return null