reference to different classes : item held by different type of owner

Hello everyone,

It’s probably answered somewhere else but i couldn’t formulate my question so that a search could find the answer.

Here’s the thing : i’m ocding a simple 2D game seen from above. I’m imlplementing different types of items such as materials, tools, and the objects the character can use, called “Inventory” in my code.
Such inventory can be either held by a character or lying on the ground, so both the classes “Tile” and “Character” have a “Inventory” reference.
But the inventory needs to know who owns it. And i don’t know how to do it elegantly.

what i did before was to have simply 2 references, one of type “Character” and one of type “Tile”, but since other type of owner will add in the future this add more and more lines of code, and the checks will be quite painful to implement…

DO you know a way of solve this riddle?
Thanks in advance, Gabriel

You should look up script-able objects. I think that may help solve your issue. If your destroying the object a script-able object being destroyed wont remove any of the properties or inventory items.

I’ll look at it. And could an interface do the work?

I used intefaces very little, but i could implement a “CanStockInventory” interface and have “Character” and “Tile” have it? And the inventory reference would be in the interface?

I’ll look at it. And could an interface do the work?

I used intefaces very little, but i could implement a “CanStockInventory” interface and have “Character” and “Tile” have it? And the inventory reference would be in the interface?