Classes and objects
An NPC, a player, a shop or a boss is not a class. Each one is an entity, and what it is comes from the components it carries. Classes describe those components. See The Rift ECS.
Components
Section titled “Components”A component is a class that extends Component. It holds one piece of an entity’s state.
import rift.ecs.Component
class DamageImmune : Component()
on_npc_option(Assets.Npc.hans, "Talk-to") { subject -> if (!target.hasComponent<DamageImmune>()) target.addComponent(DamageImmune())}A component with no fields is a marker: carrying it is the whole message.
Data class components
Section titled “Data class components”Use a data class when a component is just its values. It compares and prints by those values, which makes it easy to read in logs and checks.
import rift.ecs.Component
data class CombatSettings( var autoRetaliate: Boolean = true, var retaliateDamage: Int = 1,) : Component()Saved components
Section titled “Saved components”@Persistent on a component saves it with the character, and @Persist marks each field to save.
import rift.ecs.Componentimport rift.ecs.Persistimport rift.ecs.Persistent
@Persistentclass Pet( @Persist var name: String = "", @Persist var tricksLearned: Int = 0,) : Component()Renaming and removing saved fields is covered in Save files and persistence.
Enum classes
Section titled “Enum classes”An enum class is a fixed set of values. Each value can carry properties.
enum class DamageType(val hitsplat: String) { Block("hitsplat_12"), Hit("hitsplat_16"), Poison("hitsplat_65"),}
val names = DamageType.entries.map { it.name }Sealed classes
Section titled “Sealed classes”A sealed class is a closed set of cases, each its own class. A when over it needs no else.
sealed class Reward { data class Coins(val amount: Int) : Reward() data class Item(val item: ItemDefinition) : Reward() object None : Reward()}
fun describe(reward: Reward) = when (reward) { is Reward.Coins -> "${reward.amount} coins" is Reward.Item -> "an item" Reward.None -> "nothing"}Objects
Section titled “Objects”object declares one value with a name, which suits a table of fixed settings.
object Timings { const val CORPSE_TICKS = 5 const val RESPAWN_TICKS = 50}
val total = Timings.CORPSE_TICKS + Timings.RESPAWN_TICKSData classes for values
Section titled “Data classes for values”A data class also suits a plain value passed between functions, such as one row of a loot table.
data class LootRow(val item: ItemDefinition, val amount: Int = 1, val weight: Int = 10)
val bones = LootRow(Assets.Item.bones)val moreBones = bones.copy(amount = 3)val (a, b) = … takes a data class apart inside a block or a function.
data class LootRow(val item: ItemDefinition, val amount: Int = 1, val weight: Int = 10)
fun amountOf(row: LootRow): Int { val (_, amount) = row return amount}Classes for everything else
Section titled “Classes for everything else”Outside entities, model things however suits the job. A class fits a tool with behaviour of its own, such as a loot table that picks a row by weight.
data class LootRow(val item: ItemDefinition, val amount: Int = 1, val weight: Int = 10)
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() }}A class like this never stands in for an entity. To give a goblin loot, give it a component, and let a system or a hook roll the table.
Extension functions
Section titled “Extension functions”fun Type.name() adds a function to an existing type. On Entity, it gives a component check a readable name.
import rift.ecs.Component
class DamageImmune : Component()
fun Entity.isImmune() = hasComponent<DamageImmune>()