[SOLVED] Best way to go about scripting items that give stats boosts?

So my characters have various stats (damage, health, etc.) which are store in a Stats class.
I also want to make items that can increase those stats.

I created an Enum so I may choose which stat I want the item to increase.

I was thinking of storing everything in a dictionary with a string “Damage”, “Health”, etc. for the key and then the value, but since other scripts always needing those stats, I feel it’s a bit inefficient.
I also don’t want to code 10 similar methods for every stat.

This is a design issue rather than a scripting issue if you ask me.

Here’s how I would go about this.

I’d create an item class. The item class would have each stat with the modifier on it.

For the character class (programming class, not to be confused with RPG class :slight_smile: I would create another list which holds what equipment the character has equipped.

The specification for the item class would be:

using UnityEngine;
using System.Collections;

// need this to use generic collections like List
using System.Collections.Generic;

public class Item
{
     // declare a list to store the stat mods for the item
     // each stat is a number 0 to n  ... so it might look like...
     // 0 strenth
     // 1 agility
     // 2 intelligence
     // 3 wisdom
     // 4 charisma
  public List<int> mods;
     private int NumOfStats;
    
     public Item()
     {
       NumOfStats = 5;
       // init the list
       mods = new List<int>();
       for(int i = 0; i < NumOfStats; i++)
       {
         mods[i] = 0;
       }      
     }
    
     // this function stores the mod into list of stats
     // if you want to put 10 strength on the item you would
     // call item.AddStat(0, 10);
     public void AddStat(int stat, int mod)
     {
       mods[stat] = mod;
     }
}

Then you can do something like:

(actually I’d read this stuff from a text file with my equipment database … using ID numbers for each item)

public Item sword = new Item();
sword.AddStat(0, 10);
sword.AddStat(1, 5);

character.AddEquipment(sword);

when you calculate your modified stats, you go through the list in your
character class…

for(int i = 0; i < EquipList.Count(); i++)
{
    strMod += EquipList[i].mods[0];
    //etc... for all other stats
}
1 Like

I forgot to mention–those are consumable items. They permanently increase your stats.

haha… that is so easy…

if you consume the item, add some number to your stat… done.

Yea I think I’ll just add stats to an array and remember what each array index stands for. Right now I have a huge switch statement. I thought perhaps there’d be a way to increase the stat by reference or something.

Why not use a dictionary / hash table instead of remembering names? That way you can read and set values based on key not a index. You could even make a struct to use as your own data type for these stats.

Yea that’s what I suggested in the OP but since other classes will be depending on those stats a lot I’m not sure how efficient it’d be.

Well by other classes if you mean like other enemy and character classes just have it as its own component or as a inherited value. Same thing for the methods that seal with if.

I’ve made a script which has all my items inside of it, then another script which sends a message to a 3rd script whenever an item is giving to my player, once he has the item, attach a script to that item that sends a message to health saying if used then restore 30 health then destroy object :slight_smile: hope this helps.