I’m setting up an inventory system with different objects such as potions, armor, random items, ect. I have created a different class for each type, since they each do something different or effect certain parts of the game differently when used. My question is how would I go about storing all these different classes into a single list for my inventory ? Right now I’m storing each class in a separate list.
Make them all inherit from another class!
EDIT: I think you can do that anyway…
Ah I wasn’t aware that you could do that. Would it work something like this?
//main class
public class item {
}
//potion subclass
public class potion : item {
}
//weapon subclass
public class weapon : item {
}
//Main
public item[] allItems;
public List<item> itemList = new List<item>();
itemList.Add (new item(allItems[0]));
public weapon[] allWeapons;
itemList.Add (new weapon(allWeapons[0]));
I think so, pretty sure you can make mark it as an abstract class aswell to make sure an ‘item’ is never instantiated.
As an alternative you can have them all implement a common interface.