Attributes library
Part of Character Stats
Registry of the attribute definitions, together with the player methods that read and
change the attributes of a player's character.
Definitions are AttributeBase objects registered with Attributes.register, usually
by Attributes.include_attributes loading the files of a plugin's attributes folder.
This file adds attributes to the extra folders of every plugin and handles that folder
through the PluginIncludeFolder hook. It is part of the lib folder of the Stats plugin,
which Flux includes before the other folders of the plugin and before its sh_plugin.lua, so
the attributes folder of the Stats plugin is loaded this way too, as are those of the
plugins and the schema that load after it.
What a character has of an attribute is kept in an Attribute record: a level and any
number of boosts. A level changes only when it is set, by staff or by the code of a
plugin, such as the Levels plugin spending a point on it. A boost may carry an identifier,
which lets the code that gave it replace or remove it later, and may have no expiry, in
which case it stays until it is removed. The server networks the attributes of a player to
that player alone, in the private 'attributes' networked variable, which the getters read
on the client.
Summary
Functions
Functions
Attributes.add_modifier(target, attribute_id, list_key, model, value, duration, identifier)
server plugins/stats/plugin/lib/sh_attributes.lua:472Gives a player's character a boost and networks the attributes. A boost that carries an
identifier replaces those of the same attribute with that identifier. This is what
Player:boost_attribute is built on; it does not check whether the attribute is
boostable.
Parameters
- target Player
- attribute_id String
- list_key String field of the Attribute record that holds the records, 'attribute_boosts'
- model ActiveRecord::Base class of the records, AttributeBoost
- value Number
- duration Number optional, defaults to
nilseconds until it expires; it never expires if this is nil or not above 0 - identifier Any optional, defaults to
nilidentifier, converted to a string
Returns
- AttributeBoost the new record, or nil if the value is not a number or the character has no record for the attribute
Attributes.clear_modifiers(target, list_key, attribute_id, identifier)
server plugins/stats/plugin/lib/sh_attributes.lua:510Removes boosts from a player's character and networks the attributes if any were
removed. This is what Player:remove_attribute_boosts is built on.
Parameters
- target Player
- list_key String field of the Attribute record that holds the records, 'attribute_boosts'
- attribute_id String optional, defaults to
nilonly remove from this attribute - identifier Any optional, defaults to
nilonly remove the ones with this identifier
Returns
- Number amount removed
Gives a character a record for every registered attribute it has none for. A new record starts at the given level, rounded down and kept between the attribute's min and max, or at the attribute's default level. The records are saved with the character.
Parameters
- character Character
- levels Map optional, defaults to
nilstarting levels keyed by attribute ID
Returns
- Number amount of records added
Returns the definition of a registered attribute.
Parameters
- id String attribute ID
Returns
- AttributeBase the attribute definition, or nil if it is not registered
Returns the level and the boosts of one attribute of a player's character, in the form that is networked.
Parameters
- target Player
- attribute_id String
Returns
- Map attribute data, or nil if the attribute is not registered or the player's character has no record for it
Returns the level of one attribute of a player's character and the sum of its boosts that have not expired, read from the record without building the networked form.
Parameters
- target Player
- attribute_id String
Returns
- Number level and Number sum of the boosts, cut down for an attribute that has boost_limited set; nil if the attribute is not registered or the player's character has no record for it
Attributes.get_record(character, attribute_id)
server plugins/stats/plugin/lib/sh_attributes.lua:266Finds the record that a character keeps for an attribute.
Parameters
- character Character character record; anything else finds nothing
- attribute_id String
Returns
- Attribute the record, or nil if the character has none
Returns every registered attribute definition.
Returns
- Map attribute definitions keyed by attribute ID
Includes every file of a folder as an attribute definition. Each file gets a fresh ATTRIBUTE global, an AttributeBase named after the file, that is registered once the file has run.
-- attributes/sh_strength.lua, registered as 'strength'
ATTRIBUTE.name = 'attribute.strength.name'
ATTRIBUTE.max = 20
Parameters
- directory String folder to include files from
Checks whether an attribute is shown to a player on the character sheet. An attribute with its hidden field set is never shown; any other can be hidden with the IsAttributeVisible hook.
Parameters
- attribute AttributeBase attribute definition
- target Player player whose attributes are being shown
Returns
- Boolean
Opens a window that lists the given attributes and skills of a player's character, hidden ones included. The list is a snapshot: it does not follow later changes. A window that is still open from an earlier call is replaced.
Parameters
- target Player player the attributes and skills belong to
- attributes Map attribute data keyed by attribute ID, as
Player:get_attributesreturns it - skills Map optional, defaults to
nilNumber ranks keyed by skill ID, asPlayer:get_skillsreturns it
Returns
- Panel the
fl_framethat was opened
Stores an attribute definition and runs the AttributeRegistered hook. Missing fields get defaults: min 0, max 10 and default equal to min.
local attribute = AttributeBase.new('sanity')
attribute.name = 'attribute.sanity.name'
attribute.boostable = false
Attributes.register(attribute.attribute_id, attribute)
Parameters
- id String unique attribute ID; raises an error when it is not a string
- data AttributeBase attribute definition; nothing happens when it is nil
See also
Removes the boosts of a player's character that have expired, deleting their records.
Does not network the change, see Attributes.sync.
Parameters
- target Player
Returns
- Number amount of boosts removed
Attributes.sum_boosts(attribute_table, attribute)
shared plugins/stats/plugin/lib/sh_attributes.lua:101Adds up the boosts in the data of an attribute. If the attribute has boost_limited set, the sum is cut down so that the level together with the boosts stays between the attribute's min and max.
Parameters
- attribute_table AttributeBase attribute definition
- attribute Map data of the attribute as
Player:get_attribute_datareturns it; its level and boosts are used
Returns
- Number
Networks the attributes of a player's character to that player again and notes when the next of its boosts expires. Called after every change the plugin makes; call it yourself after changing the records directly.
Parameters
- target Player
Builds the table that is networked for a player: the level and the boosts of every registered attribute their character has a record for.
Parameters
- target Player
Returns
- Map attribute data keyed by attribute ID
- Number earliest unix time at which a boost of the character expires or has expired; nil if none has an expiry
Attributes.update(target, attribute_id, level)
server plugins/stats/plugin/lib/sh_attributes.lua:424Sets the level of an attribute of a player's character, networks the attributes and runs the PlayerAttributeChanged hook. Nothing happens if the level does not change or the character has no record for the attribute.
Parameters
- target Player
- attribute_id String
- level Number new level, rounded and kept between the attribute's min and max
Returns
- Boolean true if the level has changed