Best practices for OOP architecture for an inventory system?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By systwin

I’m just starting out with Godot, but I have a decent amount of experience programming. I’m currently puzzling over my inventory system, and I’ve hit a bit of a diamond problem impasse.

I expect to have a bunch of types of inventories – the player’s/enemies’ inventories, dropped stacks of items on the ground, shopkeepers inventory with extra buy/sell actions, etc. Currently, I have an ItemCollection class which is the base class of all of these and extends Node, which has the normal stuff you’d expect – add items, remove items, get items. At least to start, I’m thinking in terms of the way Caves of Qud handles items, if that gives you context.

Currently, only DroppedItemStack is implemented. It’s an Area2D node with an ItemCollection node as its child, plus a Sprite/CollisionShape2D, since an item stack on the ground will show the icon of the last item added to indicate that some number of items exists.

The trouble is, since ItemCollection is a child of the DroppedItemStack scene instead of DroppedItemStack inheriting from ItemCollection, any time I want to get an item out of the dropped items scene, I have to duplicate behavior in DroppedItemStack (or otherwise make a secondary getter function inside DroppedItemStack to then get items out of its ItemCollection child).

But, though I do conceptually want to make DroppedItemStack inherit from ItemCollection, I also want DroppedItemStack to extend Area2D, since I want the player to be able to collide with the stack and go through some menus to pick things up from the pile. And I definitely don’t want ItemCollection to extend Area2D, since the player’s/enemy corpse’s/shopkeeper’s inventory isn’t something sitting on the ground that needs collision.

Is there a good solution for this? Is there some version of the OOP interface concept I could use here, or is a bit of duplicate code the best solution?

Maybe this https://www.youtube.com/watch?v=lrAwX2t1mGY helps.

I myself tend to develop using x-base extends Reference for my business objects then attach those to y extends Node has attribitute x-special extends x-base … not sure this helps :-p

clemens.tolboom | 2021-07-26 12:22