about 3 minutes unity

Item Attribute System

Some games have a lot of items with unique stats. This is the besy way I found to handle it.

In Rifle Storm and I'm sure many other games have the issue of assigning special properties to many items. Since this property could be used on only one item, it doesn't make sense to add a new column for all the other items.

The solution is to create a generic class with just an enum and a value. And hold all of them inside an array.

This allows any type of stat to be assigned to an item.

And since these are just enums in an array. It's possible to combine stat lists from other items to an entity.

The Stat Enum

First, I created a generic StatVal class, which simply holds a Stat enum and an integer.

// StatVal.cs

public enum Stat {
    None,
    ChargeSpeed,
    SonicBoom,
    Cover,
    BlastDef,
    ...
}

public class StatVal {
  public Stat stat;

  public int value;
}

Define all your custom enums here. It's simple to add more stats when needed.

Adding Stats to Items

Stats can be added to any items from Excel.

All stats are assigned in Excel and imported automatically. More info here.

Using Them For Descriptions

Here is an example of how these values are used for tooltips and item descriptions.

// TextUtil.cs

public static string FormatStat(Stat stat, int val) {
    var statStr = (stat == Stat.MaxHealth) ? "Health" : stat.ToString();

    var baseString = "+" + val + " " + statStr;

    baseString = TextUtil.HighlightNumbers(baseString);

    return baseString;
}

Note

I could have created a mapping for each enum, but this was used so rarly, it did it's job well enough.

Assigning Stats To Entities

Stats are easily assigned to entities. Any data from it's class or items will automatically be applied to the entities single statList.

// Equip.cs

// init the entities base stats
statList[Stat.Jump] = GameConfig.BASE_JUMP;
statList[Stat.Move] = GameConfig.BASE_MOVE;

statList[Stat.Evasion] = GameConfig.BASE_EVASION;
statList[Stat.Cover] = GameConfig.BASE_COVER;

statList[Stat.Speed] = GameConfig.BASE_SPEED;
statList[Stat.MaxHealth] = klass.Health;

// load in stats from it's class
statList.Add(klass.Stats);

// add stat bonuses
statList.Add(entityData.bonusStats);

Note

There is more here. Like adding gear, weapon and gadget bonuses but I left it out.

Using Their Values

Their values can easily be used like for modifying the damage of an entity.

// Equip.cs
public float DamageMod {
    get { return 1.0f + statList.GetMod(Stat.DamageMod); }
}

Some times a stat just provides a bonus if it's there.

if (statList.HasVal(Stat.Snapshot)) {
  // get snapshot bonus
}

Note

GetMod is a method to convert an integer to a float by dividing it by 100.

Back to tutorials