Skip to content

Basic syntax

A script is a .rift file. Its lines run from top to bottom when the script loads. There is no main and nothing to wrap it in.

val realm = "Lumbridge"
Logger.info("$realm is loading.")

// comments out the rest of a line. /* */ comments out a block.

// A single line.
/* Several
lines. */
val price = 25 /* inside a line */

Logger writes a line to the server’s log. info, warn and error set its level.

Logger.info("The shop is open.")
Logger.warn("The shop is low on stock.")

val names a value that never changes. var names one you can change. Riftlang works out the type, or you write it after :.

val shopName = "General store"
val maxStock: Int = 100
var stock = 40
stock += 10

fun declares a function. Its parameters and return type have types. A function that is one expression can use =.

fun price(base: Int, quantity: Int): Int {
return base * quantity
}
fun discounted(price: Int) = price * 9 / 10

$name puts a value in a string. ${} puts in any expression.

val item = "bronze sword"
val price = 32
val line = "A $item costs $price coins, or ${price * 2} for two."

if chooses between branches, and gives back a value.

val coins = 150
val verdict = if (coins >= 100) "You can afford it." else "You need more coins."

when picks the first branch that matches.

val level = 42
val title = when {
level >= 90 -> "Master"
level >= 40 -> "Adept"
else -> "Novice"
}

for goes over anything you can count through. while repeats while a condition holds.

for (n in 1..3) {
Logger.info("Wave $n")
}
var health = 10
while (health > 0) {
health -= 3
}

listOf, setOf and mapOf make collections. Their mutable forms can change.

val loot = listOf("bones", "coins", "bronze dagger")
val prices = mapOf("bones" to 1, "coins" to 1, "bronze dagger" to 10)
val bag = mutableListOf<String>()
bag += loot.first()

A type ending in ? can hold null. ?. and ?: handle it.

val title: String? = null
val shown = title?.uppercase() ?: "No title"

class declares a type with properties and functions.

class Shop(val name: String, var stock: Int) {
fun sell() {
stock -= 1
}
}
val store = Shop("General store", 40)
store.sell()

A hook runs a block when something happens in the game. The block’s parameter is who it happened to.

on_login { player ->
player.message("Welcome to the realm.")
}

The hooks are in Gameplay hooks and Lifecycle hooks.

node<Npc>() on the first line makes the script belong to a node you place in Forge. self is that node, and the on_self_ hooks are about it.

scripts/lumbridge/shopkeeper.rift
node<Npc>()
on_self_option("Talk-to") { _ ->
self.addComponent(
Speaking("Take a look at my wares.")
)
}

The kinds are Npc, WorldObject, WorldItem, WorldProjection, Actor, WorldArea, Projectile and Director. A script declares its kind once.