Whitenoise 2014, Ainneve contributors, Griatch 2020

A Trait represents a modifiable property on (usually) a Character. They can be used to represent everything from attributes (str, agi etc) to skills (hunting 10, swords 14 etc) and dynamically changing things like HP, XP etc.

Traits use Evennia Attributes under the hood, making them persistent (they survive a server reload/reboot).

Adding Traits to a typeclass

To access and manipulate traits on an entity, its Typeclass needs to have a TraitHandler assigned it. Usually, the handler is made available as .traits (in the same way as .tags or .attributes). It’s recommended to do this using Evennia’s lazy_property (which basically just means it’s not initialized until it’s actually accessed).

Here’s an example for adding the TraitHandler to the base Object class:

# mygame/typeclasses/objects.py

from evennia import DefaultObject
from evennia.utils import lazy_property
from evennia.contrib.traits import TraitHandler

# ...

class Object(DefaultObject):
    def traits(self):
        # this adds the handler as .traits
        return TraitHandler(self)

Using traits

A trait is added to the traithandler, after which one can access it as a property on the handler (similarly to how you can do .db.attrname for Attributes in Evennia).

# this is an example using the "static" trait, described below

>>> obj.traits.add("hunting", "Hunting Skill", trait_type="static", base=4)
>>> obj.traits.hunting.value
>>> obj.traits.hunting.value += 5
>>> obj.traits.hunting.value
>>> obj.traits.add("hp", "Health", trait_type="gauge", min=0, max=100)
>>> obj.traits.hp.value
>>> obj.traits.hp -= 200
>>> obj.traits.hp.value
>>> obj.traits.hp.reset()
>>> obj.traits.hp.value
# you can also access property with getitem
>>> obj.traits.hp["value"]
# you can store arbitrary data persistently as well
>>> obj.traits.hp.effect = "poisoned!"
>>> obj.traits.hp.effect

When adding the trait, you supply the name of the property (hunting) along with a more human-friendly name (“Hunting Skill”). The latter will show if you print the trait etc. The trait_type is important, this specifies which type of trait this is.

Trait types

All default traits have a read-only .value property that shows the relevant or ‘current’ value of the trait. Exactly what this means depends on the type of trait.

Traits can also be combined to do arithmetic with their .value, if both have a compatible type.

>>> trait1 + trait2
>>> trait1.value
>>> trait1 + 2
>>> trait1.value

Two numerical traits can also be compared (bigger-than etc), which is useful in all sorts of rule-resolution.

if trait1 > trait2:
    # do stuff

Static trait

value = base + mod

The static trait has a base value and an optional mod-ifier. A typical use of a static trait would be a Strength stat or Skill value. That is, something that varies slowly or not at all, and which may be modified in-place.

>>> obj.traits.add("str", "Strength", trait_type="static", base=10, mod=2)
>>> obj.traits.mytrait.value
12   # base + mod
>>> obj.traits.mytrait.base += 2
>>> obj.traits.mytrait.mod += 1
>>> obj.traits.mytrait.value
>>> obj.traits.mytrait.mod = 0
>>> obj.traits.mytrait.value


min/unset     base    base+mod                       max/unset
                            current   value
                                      = current
                                      + mod

A counter describes a value that can move from a base. The .current property is the thing usually modified. It starts at the .base. One can also add a modifier, which will both be added to the base and to current (forming .value). The min/max of the range are optional, a boundary set to None will remove it. A suggested use for a Counter Trait would be to track skill values.

>>> obj.traits.add("hunting", "Hunting Skill", trait_type="counter",
                   base=10, mod=1, min=0, max=100)
>>> obj.traits.hunting.value
11  # current starts at base + mod
>>> obj.traits.hunting.current += 10
>>> obj.traits.hunting.value
# reset back to base+mod by deleting current
>>> del obj.traits.hunting.current
>>> obj.traits.hunting.value
>>> obj.traits.hunting.max = None  # removing upper bound

Counters have some extra properties:


The descs property is a dict {upper_bound:text_description}. This allows for easily storing a more human-friendly description of the current value in the interval. Here is an example for skill values between 0 and 10:

{0: "unskilled", 1: "neophyte", 5: "trained", 7: "expert", 9: "master"}

The keys must be supplied from smallest to largest. Any values below the lowest and above the highest description will be considered to be included in the closest description slot. By calling .desc() on the Counter, will you get the text matching the current value value.

# (could also have passed descs= to traits.add())
>>> obj.traits.hunting.descs = {
    0: "unskilled", 10: "neophyte", 50: "trained", 70: "expert", 90: "master"}
>>> obj.traits.hunting.value
>>> obj.traits.hunting.desc()
>>> obj.traits.hunting.current += 60
>>> obj.traits.hunting.value
>>> obj.traits.hunting.desc()


The rate property defaults to 0. If set to a value different from 0, it allows the trait to change value dynamically. This could be used for example for an attribute that was temporarily lowered but will gradually (or abruptly) recover after a certain time. The rate is given as change of the current .value per-second, and this will still be restrained by min/max boundaries, if those are set.

It is also possible to set a .ratetarget, for the auto-change to stop at (rather than at the min/max boundaries). This allows the value to return to a previous value.

>>> obj.traits.hunting.value
>>> obj.traits.hunting.ratetarget = 71
# debuff hunting for some reason
>>> obj.traits.hunting.current -= 30
>>> obj.traits.hunting.value
>>> obj.traits.hunting.rate = 1  # 1/s increase
# Waiting 5s
>>> obj.traits.hunting.value
# Waiting 8s
>>> obj.traits.hunting.value
# Waiting 100s
>>> obj.traits.hunting.value
71    # we have stopped at the ratetarget
>>> obj.traits.hunting.rate = 0  # disable auto-change

Note that if .rate is a non-integer, the resulting .value (at least until it reaches a boundary or rate-target) will also come out a float (so you can get a very exact value at the current time). If you expect an integer, you must run int() (or something like round()) on the result yourself.


If both min and max are defined, the .percent() method of the trait will return the value as a percentage.

>>> obj.traits.hunting.percent()
>>> obj.traits.hunting.percent(formatting=None)


This emulates a [fuel-] gauge that empties from a base+mod value.

min/0                                            max=base+mod
                      = current

The .current value will start from a full gauge. The .max property is read-only and is set by .base + .mod. So contrary to a Counter, the .mod modifier only applies to the max value of the gauge and not the current value. The minimum bound defaults to 0 if not set explicitly.

This trait is useful for showing commonly depletable resources like health, stamina and the like.

>>> obj.traits.add("hp", "Health", trait_type="gauge", base=100)
>>> obj.traits.hp.value  # (or .current)
>>> obj.traits.hp.mod = 10
>>> obj.traits.hp.value
>>> obj.traits.hp.current -= 30
>>> obj.traits.hp.value

The Gauge trait is subclass of the Counter, so you have access to the same methods and properties where they make sense. So gauges can also have a .descs dict to describe the intervals in text, and can use .percent() to get how filled it is as a percentage etc.

The .rate is particularly relevant for gauges - useful for everything from poison slowly draining your health, to resting gradually increasing it.


A single value of any type.

This is the ‘base’ Trait, meant to inherit from if you want to invent trait-types from scratch (most of the time you’ll probably inherit from some of the more advanced trait-type classes though). A Trait**s **.value can be anything (that can be stored in an Attribute) and if it’s a integer/float you can do arithmetic with it, but otherwise it acts just like a glorified Attribute.

>>> obj.traits.add("mytrait", "My Trait", trait_type="trait", value=30)
>>> obj.traits.mytrait.value
>>> obj.traits.mytrait.value = "stringvalue"
>>> obj.traits.mytrait.value

Expanding with your own Traits

A Trait is a class inhering from evennia.contrib.traits.Trait (or from one of the existing Trait classes).

# in a file, say, 'mygame/world/traits.py'

from evennia.contrib.traits import StaticTrait

class RageTrait(StaticTrait):

    trait_type = "rage"
    default_keys = {
        "rage": 0

    def berserk(self):
        self.mod = 100

    def sedate(self):
        self.mod = 0

Above is an example custom-trait-class “rage” that stores a property “rage” on itself, with a default value of 0. This has all the functionality of a Trait - for example, if you do del on the rage property, it will be set back to its default (0). Above we also added some helper methods.

To add your custom RageTrait to Evennia, add the following to your settings file (assuming your class is in mygame/world/traits.py):

TRAIT_CLASS_PATHS = ["world.traits.RageTrait"]

Reload the server and you should now be able to use your trait:

>>> obj.traits.add("mood", "A dark mood", rage=30, trait_type='rage')
>>> obj.traits.mood.rage

exception evennia.contrib.traits.TraitException(msg)[source]

Bases: RuntimeError

Base exception class raised by Trait objects.


msg (str) – informative error message


Initialize self. See help(type(self)) for accurate signature.

class evennia.contrib.traits.MandatoryTraitKey[source]

Bases: object

This represents a required key that must be supplied when a Trait is initialized. It’s used by Trait classes when defining their required keys.

class evennia.contrib.traits.TraitHandler(obj, db_attribute_key='traits', db_attribute_category='traits')[source]

Bases: object

Factory class that instantiates Trait objects.

__init__(obj, db_attribute_key='traits', db_attribute_category='traits')[source]

Initialize the handler and set up its internal Attribute-based storage.

  • obj (Object) – Parent Object typeclass for this TraitHandler

  • db_attribute_key (str) – Name of the DB attribute for trait data storage

property all

Get all trait keys in this handler.


list – All Trait keys.


trait_key (str) – key from the traits dict containing config data.


(Trait or None) – named Trait class or None if trait key is not found in traits collection.

add(trait_key, name=None, trait_type='static', force=True, **trait_properties)[source]

Create a new Trait and add it to the handler.

  • trait_key (str) – This is the name of the property that will be made available on this handler (example ‘hp’).

  • name (str, optional) – Name of the Trait, like “Health”. If not given, will use trait_key starting with a capital letter.

  • trait_type (str, optional) – One of ‘static’, ‘counter’ or ‘gauge’.

  • force_add (bool) – If set, create a new Trait even if a Trait with the same trait_key already exists.

  • trait_properties (dict) – These will all be use to initialize the new trait. See the properties class variable on each Trait class to see which are required.


TraitException – If specifying invalid values for the given Trait, the trait_type is not recognized, or an existing trait already exists (and force is unset).


Remove a Trait from the handler’s parent object.


trait_key (str) – The name of the trait to remove.


Remove all Traits from the handler’s parent object.

class evennia.contrib.traits.Trait(trait_data)[source]

Bases: object

Represents an object or Character trait. This simple base is just storing anything in it’s ‘value’ property, so it’s pretty much just a different wrapper to an Attribute. It does no type-checking of what is stored.


See module docstring for configuration details.


trait_type = 'trait'
default_keys = {'value': None}
allow_extra_properties = True

This both initializes and validates the Trait on creation. It must raise exception if validation fails. The TraitHandler will call this when the trait is furst added, to make sure it validates before storing.


trait_data (any) – Any pickle-able values to store with this trait. This must contain any cls.default_keys that do not have a default value in cls.data_default_values. Any extra kwargs will be made available as extra properties on the Trait, assuming the class variable allow_extra_properties is set.


TraitException – If input-validation failed.

static validate_input(cls, trait_data)[source]

Validate input


trait_data (dict or _SaverDict) – Data to be used for initialization of this trait.



Validated data, possibly complemented with default

values from default_keys.


TraitException – If finding unset keys without a default.

property name

Display name for the trait.

property key

Display name for the trait.

property value

Store a value

class evennia.contrib.traits.StaticTrait(trait_data)[source]

Bases: evennia.contrib.traits.Trait

Static Trait. This is a single value with a modifier, with no concept of a ‘current’ value.

value = base + mod

trait_type = 'static'
default_keys = {'base': 0, 'mod': 0}
property mod

The trait’s modifier.

property value

The value of the Trait

class evennia.contrib.traits.CounterTrait(trait_data)[source]

Bases: evennia.contrib.traits.Trait

Counter Trait.

This includes modifications and min/max limits as well as the notion of a current value. The value can also be reset to the base value.

min/unset base base+mod max/unset
current value

= current + mod

  • value = current + mod, starts at base + mod

  • if min or max is None, there is no upper/lower bound (default)

  • if max is set to “base”, max will be equal ot base+mod

  • descs are used to optionally describe each value interval. The desc of the current value value can then be retrieved with .desc(). The property is set as {lower_bound_inclusive:desc} and should be given smallest-to-biggest. For example, for a skill rating between 0 and 10:

    {0: “unskilled”,

    1: “neophyte”, 5: “traited”, 7: “expert”, 9: “master”}

  • rate/ratetarget are optional settings to include a rate-of-change of the current value. This is calculated on-demand and allows for describing a value that is gradually growing smaller/bigger. The increase will stop when either reaching a boundary (if set) or ratetarget. Setting the rate to 0 (default) stops any change.

trait_type = 'counter'
default_keys = {'base': 0, 'descs': None, 'max': None, 'min': None, 'mod': 0, 'rate': 0, 'ratetarget': None}
static validate_input(cls, trait_data)[source]

Add extra validation for descs

property base
property mod
property min
property max
property current

The current value of the Trait. This does not have .mod added.

property value

The value of the Trait (current + mod)

property ratetarget

Return the current value as a percentage.


formatting (str, optional) – Should contain a format-tag which will receive the value. If this is set to None, the raw float will be returned.


float or str

Depending of if a formatting string

is supplied or not.


Resets current property equal to base value.


Retrieve descriptions of the current value, if available.

This must be a mapping {upper_bound_inclusive: text}, ordered from small to big. Any value above the highest upper bound will be included as being in the highest bound. rely on Python3.7+ dicts retaining ordering to let this describe the interval.



The description describing the value value.

If not found, returns the empty string.

class evennia.contrib.traits.GaugeTrait(trait_data)[source]

Bases: evennia.contrib.traits.CounterTrait

Gauge Trait.

This emulates a gauge-meter that empties from a base+mod value.

min/0 max=base+mod


= current

  • min defaults to 0

  • max value is always base + mad

  • .max is an alias of .base

  • value = current and varies from min to max.

  • descs is a mapping {upper_bound_inclusive: desc}. These

    are checked with .desc() and can be retrieve a text description for a given current value.

    For example, this could be used to describe health values between 0 and 100:

    {0: “Dead”

    10: “Badly hurt”, 30: “Bleeding”, 50: “Hurting”, 90: “Healthy”}

trait_type = 'gauge'
default_keys = {'base': 0, 'descs': None, 'min': 0, 'mod': 0, 'rate': 0, 'ratetarget': None}
property base
property mod
property min
property max

The max is always base + mod.

property current

The current value of the gauge.

property value

The value of the trait


Return the current value as a percentage.


formatting (str, optional) – Should contain a format-tag which will receive the value. If this is set to None, the raw float will be returned.


float or str

Depending of if a formatting string

is supplied or not.


Fills the gauge to its maximum allowed by base + mod