control flow
if / else
if (player.health < 5) {
player.sendMessage("&clow health!")
} else if (player.health < 10) {
player.sendMessage("&egetting low...")
} else {
player.sendMessage("&ayoure ok :D")
}
braces are always required, even for a single statement:
// this will not compile: no braces
if (x > 0)
doSomething()
// always do this
if (x > 0) {
doSomething()
}
when
when compares one value against several possibilities, top to bottom, and runs the first match (like a switch statement):
when (color) {
"red" -> {
player.sendMessage("&cred team!")
}
"blue" -> {
player.sendMessage("&9blue team!")
}
else -> { // this else statement is optional
player.sendMessage("&7unknown team")
}
}
return
function clamp(value, min, max) {
if (value < min) {
return min
}
if (value > max) {
return max
}
return value
}
a function with no return returns null. return with no value is also valid:
function startGame() {
if (gameRunning) {
return // already running, do nothing
}
gameRunning = true
...
}
this is especially important inside Katton.after/Katton.every callbacks, see Katton.after and Katton.every for why you almost always want a return