'Item' class with various effects

Hi all,

I have followed various tutorials showing me how to create such things as an Inventory and ‘loot’ system for an RPG style game when I first started with Unity. I am now creating my first ‘original’ game and am struggling with ‘Items’.

Very simply put I have a Shop and and Inventory. The shop will sell things like Weapons, Consumables, Upgrades and so on, and when purchased they move into the Inventory.

The store is an array of ‘Items’ which when bought get removed from the Shop array and added to the characters Inventory array.

I know to have things like Weapons and Consumables as sub-classes of Item with variables specific to their type (Weapon: power/type/fire rate, Consumable: Stat/Buff Value), but with Upgrades I want them to affect a different variable (such as armour, or weapon power).

I am struggling with my approach to this problem, so any suggestions are welcome.

One idea I toyed with was having the upgrades added to the inventory array too, just hidden (excluded when the inventory is populated) and the variable is += upgrade*100 (for example) but I don’t know how (or even if) I can extract the item.type of each item in the array, and have it show - almost like [SELECT * FROM ITEMS WHERE ITEMS.TYPE = UPGRADE]?

Thanks

Maybe one idea is you could add scripts to the objects. eg something like:

obj.AddComponent("frostDamage");

So if you had a sword and has a normal doDamage script. You could check whether there are other scripts that could also do damage , they just need to have a common method. Get the damage produced from each script, sum them up, and thats the damage you will inflict.

Same could be for armour, etc. That way you can stack all sorts of things. Like a weapon that also has armour capabilities.

Here is an example of how upgrade will affect different items in a different way.

public class Item 
{
public virtual void upgrade()
{
}
}

Public class Weapon : Item
{
damage = 10;
public Override void Upgrade
{
damage = damage +10;
print("damage is now "+damage);
}
}

Public class HealingPotion: Item
{
healValue = 10;
public Override void Upgrade
{
healValue = healValue +10;
print("healValue is now "+healValue);
}
}

public class Inventory
{
public list<items> itemsInInventory;
void Start()
{
item weapon = new weapon();
item heal = new HealingPotion();
itemsInInventory.Add(weapon);
itemsInInventory.Add(heal);
}
public void UpgradeAll()
{
foreach(item i in itemsInInventory)
{
i.Upgrade();
}
}
}

result will be like:
damage is now 20
healValue is now 20

The upgrade functions works differently for each item since we used virtual - override method.