How to optimally implement item OnPickUp functions, without hard ref to the inventory class?

Hi. New dev here. I’m designing my inventory system at an early stage. What I have now:

  • Interact detection logic on player BP. Calls items’ OnPickUp through an interactable interface.
  • Inventory BP component attached to player BP, with functions like AddToInventory, etc.
  • In my item BP, a ref to the inventory component, acquired via GetComponentByClass on player. Calls inventory functions like AddToInventory through the OnPickUp event.

Recently I learnt that I should avoid hard refs unless it’s necessary. So I wonder if/how I should replace the inventory class ref with something else. But I can’t figure out how I can access the player’s inventory component without a hard ref. Is there any tip about how it’s usually implemented?

Thanks in advance!

Why does Item call the AddToInventory function? Or did I misunderstand?
Different Items can do this differently? :thinking:

If not (most likely not) - then it is worth doing in the character (inventory component).

Hard references don’t always need to be (and can’t always be) avoided.
In this case, you probably don’t even need to come up with anything, since a reference is not needed in principle.

Basically, your item should only provide information about itself via the interface (it’s better if it’s just an ItemID, and detailed information about the items is stored separately, for example in the Data Table).
And the rest of the work - what and when to do with this information - is already known to the inventory component.

1 Like

Thanks for the reply. Maybe I’m dumb but I have multiple kinds of interactable objects in the game (like doors, levers, etc) as well as items that can be picked up. So I have a common interactable interface for them to handle the player interact input, and implement different behaviors in respective object BPs. And only when the object is an obtainable item, it should trigger the pick up function.

So if I understand it right, I need the item BP to somehow notify the inventory BP that a pickup function should be called, without calling it directly in the item BP. How can I achieve that?

edit: Right now a solution I can come up with is to add another interface to the player character BP, with a PickUpItem function. So that in the item BP I can just GetPlayerCharacter and then PickUpItem. Would that be a good approach?

Now I understand…

In this case, you can make a system of callback interfaces.
In your general OnPickUp interface, you pass a reference to the object that called it, and then you can call the AddToInventory interface in it (with the ItemID parameter).

But as I said above - it is not always necessary/possible to avoid hard references.
It makes sense to create an additional layer of abstraction (interface) between the item and the inventory only in the case when during the game a situation may arise when the inventory reference is invalid, or if your item should be added differently to different inventories (or something like that).
In this case, even if the inventory is not on the level - its class and all dependencies will be loaded into memory anyway (due to the hard reference), which should be avoided.

P.S. Soft references (and even an empty reference) will also load the class asset into memory (and all dependencies)…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.