Riftlang for Kotlin programmers
Riftlang is a flavour of Kotlin, so if you know Kotlin you’ll be right at home. This page is what a script does differently.
The language
Section titled “The language”Values, functions, classes, data classes, null safety, lambdas, collections, string templates and when are the same as in Kotlin.
data class Drop(val item: String, val amount: Int)
val drops = listOf(Drop("coins", 50), Drop("bones", 1))val coins = drops.filter { it.item == "coins" }.sumOf { it.amount }val line = when { coins > 0 -> "You have $coins coins." else -> "No coins."}Statements at the top level
Section titled “Statements at the top level”A Kotlin program has a fun main, under a package line with its own imports. A script has no main and no package line, and common names are imported for you. Its statements run top to bottom when the script loads, and hooks bind what runs later.
package lumbridge
import java.time.LocalTime
fun main() { val town = "Lumbridge" println("The $town welcome is ready at ${LocalTime.now()}.")}import java.time.LocalTime
val town = "Lumbridge"
fun greeting() = if (LocalTime.now().hour < 12) "Good morning" else "Good evening"
Logger.info("The $town welcome is ready.")
on_login { player -> player.message("${greeting()}, welcome to $town.") }Errors and stack traces point at the script’s own lines, such as welcome.rift:7.
The folder is the package
Section titled “The folder is the package”scripts/lumbridge/welcome.rift is in package lumbridge. Kotlin names the package in a package line, and Riftlang takes it from the folder. You don’t write one, and one that names another folder is refused.
package varrock// error: a file's package is its folder's (lumbridge): remove this lineImports you don’t write
Section titled “Imports you don’t write”The engine’s common packages and Logger are imported in every script, along with your project’s Scenes, Points, Area and Assets. Anything else is a normal import, as in Kotlin, and your own import of a name wins over one of these.
Node scripts
Section titled “Node scripts”node<Npc>() on its own line at the top makes the script belong to a node you place in Forge. Inside it, self and the on_self_ hooks are that node. See Scripts on a node.
Destructuring and return
Section titled “Destructuring and return”A top-level destructuring declaration is refused, so write one val per value. Inside a function or a lambda, destructuring works as usual.
on_login { player -> val (name, level) = "Hans" to 99 player.message("$name is level $level.")}A bare return at the top level is refused too. Inside a hook, leave with return@on_login and the like.
Source folders
Section titled “Source folders”A Kotlin project names its source folders in its build. A Riftlang project’s scripts live in scripts/, with nothing to set up. To add another folder, name it in project.forge.
[run]sources = ["shared"]Java libraries and the JDK
Section titled “Java libraries and the JDK”A script imports and calls Java classes the same as in Kotlin.
import java.util.concurrent.ConcurrentHashMap
val lastSeen = ConcurrentHashMap<String, Long>()
on_login { player -> lastSeen["$player"] = System.currentTimeMillis()}Sharing code between files
Section titled “Sharing code between files”A top-level declaration is its file’s own unless it’s marked public; a public one can be imported by any file. A file with a public declaration is a library file: helpers, components, systems, saved classes and events. It shares them through its package, which is its folder, and a file in another folder imports them, as in Kotlin. Unlike Kotlin, where top-level declarations are public by default, a Riftlang declaration is its file’s own until you write public.
public const val REALM_NAME = "Lumbridge"
public fun greet(who: Entity, place: String) { who.message("Welcome to $place!")}import shared.greetimport shared.REALM_NAME
node<WorldArea>()
on_self_entered { who -> greet(who, "the courtyard of $REALM_NAME") }greet takes the visitor as a parameter. A public function that used a node’s self instead is refused:
`greet` is public, so other files can use it, but it uses `self`, which only this file has: pass it in as a parameter, or drop `public`A file with no public declarations reloads in place when you save it; a library file restarts the realm. Write library files for commonly shared code, and keep hook bindings in their own scripts, so you get the most out of the language. See Packages and imports.