Unity unique items in list management

So ive been searching for a few weeks on and off to find the best way to store multiple unique classes inside a list, and then still being able to acces the class specific properties with as much flexibility as possibe, i think this picture explains my problem well enough

2855286--208743--Untitled.png
(ofcourse you get an error in this example saying that “damage doesn’t excist in the IItem interface”, but my point is that i want to get the value damage somehow when only having acces to the IItem interface, and not the actual sword class.)

Ive come up with several ways of doing this, but i don’t know which i should pick, or if there are others that are better (more flexible, faster, simpler, etc)

Using interfaces:


Pros:
Easy to keep track of, i guess, and the only way i got it to work using interfaces, since i dont know how to get the class that is using the interface i have a reference to

Cons:
Could be expensive (atleast more expensive than using direct references)
No keywords (aka when you type some parts of a variable, and the rest auto completes)

Using inheritence:

I tried this one, and while i got it to work the same way as interfaces, it was not as flexible, and more messy as many has already explained on this forum before

Pros:
Less text to write, since you can inherit variables such as damage, armor, etc

Cons:
Less flexible in the long run (for example if you want a item to be both a weapon and armor)

Having a items class with every possible variable declared in it, but with different constructors for every possible item:

Pros:
really easy to set up and to create new item types
you get autocomplete

Cons:
Probably takes a lot of memory over time (with hundereds of items and unique variables), although im not too sure if declared but unassigned variables takes a lot of space
Items will become a huge class, requiring a whole lot of regions to sort everything

So, which one should i pick, or is there a better way of doing this?
Please let me know if you have any idea, ive been trying to do this for weeks now

String based lookups are going to be a nightmare at some point – either through maintenance when you add another field to a type or when you mistype a name and the compiler can’t warn you. Inheritance doesn’t make sense sense because a Shield isn’t a Weapon nor is it a FoodItem, but you could have an edible shield or something as you pointed out.

Combine the two methods. Create an Item base class with values that all items have in common, then create a bunch of interfaces to further define them.

class Item { int value = 0f; }
interface IWeapon { float Attack(); }
interface IShield { float Defend(); }
interface IFood { float Eat(); }

class Sword : Item, IWeapon { Attack() { return 10f; } } // deal 10 damage
class Bucker : Item, IShield { Shield() { return 5f); } } // prevent 5 damage
class Food : Item, IFood { Eat() { return 25f; } } // regenerate 25 damage

class EdibleGunShield : Item, IWeapon, IShield, IFood { // etc. }

Then use the is comparison to check for types.

List<IFood> FindAllFoodInInventory()
{
   List<IFood> results = new List<IFood>();

   for(int i = 0; i < inventory.Count; ++i)
   {
       if(inventory[i] is IFood)
           results.Add(inventory[i] as IFood);
   }

   return results;
}
1 Like

Thanks! This is just what i was looking for, but didn’t find after googling several times.
This is going to save me a lot of trouble, since i was just about to start using the string based lookups

Also, it should be noted that

class Item { int value = 0f; }

should be

class Item { protected int value = 0f; }

if you want to acces them in the script that inherits it (if you don’t have a constructor in the Item script)