IHaveStats

v4.0

Any object which has Stats should implement IHaveStats. Doing so will also implement IUseGameModules and IHaveGameId.

Actors which inherit GameModulesActor or GameModulesInventoryActor have these methods already built in!

Below is some sample code for the required methods, which you may choose to use or modify for your purposes. Unless you are adding Stats that are not actors, you would not need to do any of the following, so long as you inherit GameModulesActor :)

// TryGetGameStat()
public override bool TryGetGameStat(Stat stat, out GameStat gameStat) 
    => TryGetGameStat(stat.Uid(), out gameStat);
    
public override bool TryGetGameStat(string uid, out GameStat gameStat)
{
    gameStat = stats.Get(uid, false);
    return gameStat != null;
}

// GetOtherLevels()
public virtual List<ModificationLevel> GetOtherLevels(bool cache = false)
{
    if (_modificationLevels != null && !cache)
        return _modificationLevels;

    var otherModificationLevels = new List<ModificationLevel>();
    
    // You would add the "Modification Levels" from other Game Module
    // objects used on this object or actor.
    otherModificationLevels.AddRange(conditions.ModificationLevels);
    otherModificationLevels.AddRange(quests.ModificationLevels);
    otherModificationLevels.AddRange(equippedItems.ModificationLevels);
    
    // This example is from DemoActor.cs in the Party Based RPG demo game
    if (!string.IsNullOrWhiteSpace(ActorRace.objectName)) 
        otherModificationLevels.Add(ActorRace.ModificationLevel);
    if (!string.IsNullOrWhiteSpace(ActorClass.objectName)) 
        otherModificationLevels.Add(ActorClass.ModificationLevel);

    _modificationLevels = otherModificationLevels;
    return otherModificationLevels;
}

// SetStatsDirty()
public virtual void SetStatsDirty(List<Stat> statList)
{
    ResetOtherLevels(false); // Set this to cache next time it is required
    foreach (var stat in statList)
    {
        if (!TryGetGameStat(stat, out var gameStat)) continue;
        gameStat.SetDirty();
    }
}

// GetStat()
public virtual GameStat GetStat(Stat stat, bool addIfNull = true) 
    => GetStat(stat.Uid(), addIfNull);
public virtual GameStat GetStat(string uid, bool addIfNull = true) 
    => stats.Get(uid, addIfNull);

Last updated