Stats 01: Base Classes

In this section of the RPGSystems tutorial series, we’ll be covering the creation of Stat System. The Stat System will allow for the creation of a collection of stats that can be easily modified and manipulated from external actions such as items, buffs, and spells.

This post is an old implementation and should be only used as a reference. This post maybe updated or replaced in the future.

Base Stat

All Stats within our Stat System will be inheriting from the same base stat class. This will allow us to create a Stat Collection class that can contain and easily manage all the stats within the collection. The following starting code for the RPGStat and RPGStatCollection classes will show the basic setup and concept behind how we will handle the stats. We’ll update the classes with additional features later on.
Create a new C# script called RPGStat

using UnityEngine;
using System.Collections;

public class RPGStat {
    private string _statName;
    private int _statValue;

    public string StatName {
        get { return _statName; }
        set { _statName = value; }
    }

    public int StatValue {
        get { return _statValue; }
        set { _statValue = value; }
    }

    public RPGStat() {
        this.StatName = string.Empty;
        this.StatValue = 0;
    }

    public RPGStat(string name, int value) {
        this.StatName = name;
        this.StatValue = value;
    }
}

In this class we have the bare minimum information that describes a stat. We have the two private variables with their two corresponding properties that will allow us to easily update the properties later on in the series without having to rewrite could that is using the properties. Lastly we have the Constructors at the bottom of the class to set the values.

Stat Types

To tell which RPGStat is connected to which stat in game and in our collection we will use a enum.

Create a new C# script called RPGStatType

public enum RPGStatType {
    None = 0,
    Health = 1,
    Mana = 2
}

When extending the stat collection for your own game or project all you need to do is add your stats to the RPGStatType enum and add the corisponding code to your stat collection configure stat method. (Example shown later on)

Stat Collection

The RPGStatCollection class will hold all stats within a Dictionary that will be using the RPGStatType enum as the key to accessing the corresponding stat. This allows for quick access to the stat along with easily checking if a stat collection has a given stat.

Create a new C# script called RPGStatCollection

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RPGStatCollection {
    private Dictionary<RPGStatType, RPGStat> _statDict;

    public RPGStatCollection() {
        _statDict = new Dictionary<RPGStatType, RPGStat>();
        ConfigureStats();
    }

    protected virtual void ConfigureStats() {

    }

    public bool Contains(RPGStatType statType) {
        return _statDict.ContainsKey(statType);
    }

    public RPGStat GetStat(RPGStatType statType) {
        if (Contains(statType)) {
            return _statDict[statType];
        }
        return null;
    }

    protected RPGStat CreateStat(RPGStatType statType) {
        RPGStat stat = new RPGStat();
        _statDict.Add(statType, stat);
        return stat;
    }

    protected RPGStat CreateOrGetStat(RPGStatType statType) {
        RPGStat stat = GetStat(statType);
        if (stat == null) {
            stat = CreateStat(statType);
        }
        return stat;
    }
}

There are a few thing to take note of within the collection class. The first is that the ConfigureStat method will be used to setup which stats will be created in the collection and how those stats will interact with other stats in the collection (Later on we’ll make the stat’s values interact with each other) Also the ConfigureStat method is only called once within the collection since in this implementation the stats will only be configured when the collection is created and no additional stats will be added later.

The second is that the CreateStat and CreateOrGetStat both have the protected access modifier. This is because we do not want any outside script to create additional stats and we want the full control over what is created to be determined by the classes that inherit from the collection class.

Configuring the Collection

The stat collection class will handle all stats contained within it and will not allow for external script to add stats or remove stats from it. In order to create a new stat collection you will have to inherited from the RPGStatCollection class and override the ConfigureStat method. Below we’ll create a simple default stat collection class to demonstrate how to configure the stat collection.

Create a new C# script called RPGDefaultStats.

using UnityEngine;
using System.Collections;
public class RPGDefaultStats : RPGStatCollection {
    protected override void ConfigureStats() {
        RPGStat health = CreateOrGetStat(RPGStatType.Health);
        health.StatName = "Health";
        health.StatValue = 100;

        RPGStat mana = CreateOrGetStat(RPGStatType.Mana);
        mana.StatName = "Mana";
        mana.StatValue = 2000;
    }
}

Testing the Collection

To test that our stat collection is correctly creating the stats will create a simple test script that will loop through all the values of our RPGStatType enum and display the stat corresponding to the enum value if it exists within our default stat collection.

Create a new C# script called RPGDefaultStats

using UnityEngine;
using System;
using System.Collections;
public class RPGStatTest : MonoBehavior {
    private RPGStatCollection stats;

    void Start() {
        stats = new RPGDefaultStats();

        var statTypes = Enum.GetValues(typeof(RPGStatType));
        foreach(var statType in statTypes) {
            RPGStat stat = stats.GetStat((RPGStatType)statType) ;
            if(stat != null) {
                Debug.Log(string.Format("Stat {0}’s value is {1}",
                stat.StatName, stat.StatValue));
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *