Skip to content

Character Variables

Greg edited this page Jul 15, 2023 · 14 revisions

Both Players and NPCs have a Variables container for storing variables and information.

By default variables are only stored temporarily and will be lost/reset when the npc dies, player logs out or the game restarts.

Variable names should use lower camel case. e.g. "in_combat" not "inCombat"

Getting

Characters have extension functions to simplify accessing Variables

// normal way ❌  
val burrowing = giantMole.variables.get<Boolean>("burrow")
// simplified to: ✅ 
val burrow: Boolean = giantMole["burrow"]

Note that this is the unsafe version and will throw an exception is the value hasn't previously been set.

The following safe version will return false if the value is unset.

// normal ❌ 
val burrowing = giantMole.variables.getOrDefault("burrow", false)
// simplified ✅ 
val burrow = giantMole["burrow", false]

This is the recommended way of accessing values.

Setting

Setting a value is straight-forward, it's important to use descriptive names to make sure values don't conflict with other content.

// normal ❌ 
giantMole.variables.set("burrow", true)
// simplified ✅ 
giantMole["burrow"] = true

Player saving

For players an optional persist parameter can be passed to save the value in the player's json file when they next logout.

// normal
giantMole.variables.set("burrow", persist = true, value = true)
// simplified
giantMole["burrow", true] = true

Clone this wiki locally