I'm about to start programmin an inventory, harvest and loot system. I will need enemies that drop items, harvestable environmentals that yield resources and an inventory system that takes items and resources.

Should I solve this with interfaces, class inheritance or both? Why? Or is it just up to my programming style? I'm more familiar with inheritance, but if interfaces give an advantage I'd use them and get used to it.


In my humble opinion you should use neither. I would go with the Entity Component System and use composition instead of inheritance in this case. If you do choose to use one of the two, I reccommend using an Interface.

You should avoid inheritance and use interfaces when there is no substantial inherit connection between the various classes that share the same functionality. If you do use inheritance then the base class might be pretty abstract. This is pretty much the same thing as an interface. The main advantage of interfaces over inheritance (in the languages that have interfaces) is that you could easily have a class that implements multiple interfaces. Multiple inheritance is not available in some languages and even in languages where it's available (e.g. C++), it is known to cause trouble (e.g. Diamond of Doom).

  • \$\begingroup\$ Thanks! Ill look into that. You are refering to use Unitys component based architecture right? It makes sense since that is how it is built. Its just that i have not come across anything similar before unity. So am i right in thinking that i would use components kind of like Interfaces. Like if a game object should be able to take fire from the player i would givet it a Shootable component and then in the code i can look if the game objects hit by fire has a Shootable component. I like it! \$\endgroup\$ – Daarwin Feb 21 '14 at 12:00
  • 2
    \$\begingroup\$ I did not specifically refer to Unity's architecture although I'm aware of it. The idea is that every entity has an ID number. The relevant attributes or behaviors are stored in detached "components". When there is interaction between entities, you can let the specific relevant components handle the event. \$\endgroup\$ – AturSams Feb 21 '14 at 12:15
  • \$\begingroup\$ The more i think about it the more i realize this is what Unity is built around. I just have not been adapting well enough. Ill look into this definitly. THANKS! \$\endgroup\$ – Daarwin Feb 21 '14 at 13:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.