Skip to content

Packages and imports

A file’s package is the folder it sits in. scripts/lumbridge/shop.rift is in package lumbridge, and a file directly in scripts/ has none.

scripts/
game-loop.rift no package
lumbridge/
shop.rift package lumbridge
guards.rift package lumbridge

A file never says its package. A package line naming another folder is refused.

scripts/lumbridge/shop.rift
package varrock
// error: a file's package is its folder's (lumbridge): remove this line

A folder that holds scripts has a name that starts with a letter or _ and holds only letters, digits and _, and is not a keyword. The build refuses my-folder/ and suggests my_folder.

The most commonly used things for building Rift games are automatically available inside a Riftlang script, without an import.

Logger is an example of this.

on_login { player ->
Logger.info("A player logged in.")
}

import at the top of a file brings in any other name. * brings in everything in a package.

You can import Rift packages, but also Java and Kotlin libraries.

import java.time.LocalTime
import rift.ts2.*
val opensAt = LocalTime.of(9, 0)

Use the as keyword to alias imports

import java.time.LocalTime as Clock
val now = Clock.now()

In Riftlang all declarations are implicitly private, this means other files cannot import them. In Riftlang a file where all of its contents are private is called a script.

By adding the public keyword before a declaration you make that code available for other files to import. A file with a public declaration is a library, even if it otherwise contains script code.

Riftlang scripts can be hot-reloaded while a game continues to run without interruption, but Riftlang libraries cannot. During development if you change a library file, the game will restart and load the scripts against from scratch.

To get the most out of Riftlang, we recommend that you are mindful to keep shared code and pure script code separate.

scripts/goblins/loot.rift
public const val RARE_WEIGHT = 1
public class LootRow(val item: ItemDefinition, val amount: Int = 1, val weight: Int = 10)
public class LootTable(vararg rows: LootRow) {
private val rows = rows.toList()
private val totalWeight = this.rows.sumOf { it.weight }
fun roll(): LootRow {
var pick = (0 until totalWeight).random()
for (row in rows) {
pick -= row.weight
if (pick < 0) return row
}
return rows.last()
}
}
public val GOBLIN_LOOT = LootTable(
LootRow(Assets.Item.bones),
LootRow(Assets.Item.shark, weight = RARE_WEIGHT),
)
public fun dropLoot(table: LootTable, at: Entity): LootRow {
val row = table.roll()
World.spawn(row.item, at.tile4, row.amount)
return row
}

Files in the same folder import each other’s library code by name too, the same way as from another folder.

scripts/goblins/cave_goblin.rift
import goblins.GOBLIN_LOOT
import goblins.dropLoot
node<Npc>()
on_self_option("Pickpocket") { player ->
dropLoot(GOBLIN_LOOT, target)
player.message("Something falls out of the goblin's pocket.")
}

To use library code from another folder, you must explicitly import that code.

scripts/lumbridge/bones_vendor.rift
import goblins.LootRow
import goblins.LootTable
import goblins.dropLoot
val HANS_LOOT = LootTable(
LootRow(Assets.Item.bones)
)
on_npc_option(Assets.Npc.hans, "Talk-to") { player ->
dropLoot(HANS_LOOT, target)
player.message("Oh no, what have you done.")
}

Riftlang scripts can hold temporary state in their variables, but script state is global and not per entity.

scripts/goblins/counters.rift
// all goblins referencing this script, will share this
public var dropsToday = 0

To understand per-entity state, and persistence see: Save files and persistence