Skip to content

Attributes

Tomm edited this page Apr 7, 2019 · 5 revisions

What are attributes?

Attributes are a way to store individual values for players and npcs.

Why use attributes?

If you need to save a value for a player or npc for later use, you would want to use an attribute.

How do attributes work?

Attributes are made up of two parts:

  1. AttributeKey
  2. AttributeMap

AttributeKeys are defined in your plugin file and the AttributeMap is stored for each player and can be accessed via player.attr.

Example

package gg.rsmod.plugins.content.minigames.pestcontrol

// If we want the attribute to save on log-out, we specify a name 
// inside the AttributeKey. In this case, it'll be saved as 
// "pc_win_count"
val GAME_WIN_COUNT = AttributeKey<Int>("pc_win_count")

fun inform_of_win_count(player: Player) {
    val wins = player.attr.getOrDefault(GAME_WIN_COUNT, 0)
    player.message("You have won $wins pest control games.")
}

fun set_win_count(player: Player, count: Int) {
    player.attr[GAME_WIN_COUNT] = count
    player.message("You now have $count pest control wins.")
}

Table of Contents

Clone this wiki locally