Basic syntax
Scripts
Section titled “Scripts”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
Section titled “Comments”// comments out the rest of a line. /* */ comments out a block.
// A single line.
/* Several lines. */val price = 25 /* inside a line */Writing to the log
Section titled “Writing to the log”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.")Values and variables
Section titled “Values and variables”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 = 100var stock = 40
stock += 10Functions
Section titled “Functions”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 / 10String templates
Section titled “String templates”$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."Conditions
Section titled “Conditions”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 = 10while (health > 0) { health -= 3}Collections
Section titled “Collections”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()Null values
Section titled “Null values”A type ending in ? can hold null. ?. and ?: handle it.
val title: String? = null
val shown = title?.uppercase() ?: "No title"Classes
Section titled “Classes”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()Reacting to the game
Section titled “Reacting to the game”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.
Scripts on a node
Section titled “Scripts on a node”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.
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.