0 votes

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?

Godot version 3.3.2
in Engine by (12 points)

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

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.