Packages and imports
The folder is the package
Section titled “The folder is the package”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 lumbridgeNo package line
Section titled “No package line”A file never says its package. A package line naming another folder is refused.
package varrock// error: a file's package is its folder's (lumbridge): remove this lineFolder names
Section titled “Folder names”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.
Built-ins
Section titled “Built-ins”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
Section titled “import”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.LocalTimeimport rift.ts2.*
val opensAt = LocalTime.of(9, 0)Import aliases
Section titled “Import aliases”Use the as keyword to alias imports
import java.time.LocalTime as Clock
val now = Clock.now()Sharing code between files
Section titled “Sharing code between files”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.
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}In the same folder
Section titled “In the same folder”Files in the same folder import each other’s library code by name too, the same way as from another folder.
import goblins.GOBLIN_LOOTimport goblins.dropLoot
node<Npc>()
on_self_option("Pickpocket") { player -> dropLoot(GOBLIN_LOOT, target) player.message("Something falls out of the goblin's pocket.")}From another folder
Section titled “From another folder”To use library code from another folder, you must explicitly import that code.
import goblins.LootRowimport goblins.LootTableimport 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.")}Shared state
Section titled “Shared state”Riftlang scripts can hold temporary state in their variables, but script state is global and not per entity.
// all goblins referencing this script, will share thispublic var dropsToday = 0To understand per-entity state, and persistence see: Save files and persistence