- Home /
'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
Answer by kannan21 · Jan 17, 2014 at 10:01 AM
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.
I like the look of this, but will this not require a setup for each type of upgrade?
This works well in the way that every item, on purchase, can call upgrade(), and if its something to be stored it does nothing (blank upgrade()) and upgrades can apply their respective upgrade() then destroy themselves (removeat[i]).
Iay be answering my own question here, but could an overridden upgrade() receive an int for magnitude? Have the item class include this and feed it in to the upgrade function like upgrade(item.magnitude)? That would then allow for various strengths of upgrade?
Thanks guys!
operator overloading still works. So do like this
public class Item
{
public virtual void upgrade()
{
}
public virtual void upgrade(int value)
{
}
}
and call the int overloaded function if needed.
Answer by Fabkins · Jan 17, 2014 at 01:16 AM
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.
This sounds like a good approach, and has made me reconsider how I would do this, however doesnt fit with this particular project as the only equippable I have is Weapon. (Will definitely keep this in $$anonymous$$d when creating my "traditional" RPG though)
$$anonymous$$y two new ideas for approach are: have Items and Upgrades separate. Separate classes, separate tab in the shop etcand thus Upgrades can be treated completely different to Items. Or, have Upgrade as an Item you purchase and equip. Rather than each upgrade adding to armour, you can only equip one (or maybe a handful) at a time - obviously higher benefits at higher cost further in the game.
If you only have one upgrade then just create one generic class and subclass it.
If you can have more than one upgrade , this is where the above could work for you. Yes, the upgrade could be an item. Whether you attach a component directory to the object or you attach an object with upgrade has similar effect.
In retrospect, I would probably favour having the upgrades as separate objects , each have a standard class (which you could subclass, see virtual example below), and attached the object to your weapon.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130639im_/https://answers.unity.com/themes/thub/images/avi.jpg)