Best way to store items in an inventory

Hi,

I am working on a inventory system and was hoping for a bit of advice about how to store the actual items. Basically, I am trying to figure out if it would be better to store items as an array of a custom struct that has details about the item or as the actual actor that the item represents.

Essentially I have a pickupactor class (extends actor) that describes what the item is that should be picked up. I then have various actual item classes such as a weapon which also extend actor and have various methods to do stuff like shoot etc.

I also have a actorcomponent class called inventory (original I know) that houses an array of the items along with various functions to add/remove and equip or mount onto the pawn etc. I was planning on doing this as an interface but I haven’t got that far yet.

I then obviously have my player class which has an instance of the inventory actorcomponent.

So basically, when the player picks up an item, the pickup actor uses the instagator pawn to call a function in the inventory component to get itself added to the array.

However, what I can’t decide is how to best store the items within the inventory. Initially I was going to use an array of the item actors (i.e. weapon) as it makes it easier to mount and also to use the array as TArray can compare the actors automatically.
But I thought this might be a very costly way of doing it as i’d have to create a load of actors for everything in the inventory even when they weren’t getting used. So I then thought it would be better to create a struct and add it as a property on the pickupactor and then just pass that property to the inventory to store in an array of that struct type, then I can use the info stored in the struct instance to spawn the actor (pickup or item depending on function).

But then this obviously makes adding/removing harder and I’m not sure what to do when it comes to mounting some items on the player, but Im sure they can be fixed.

A final option I considered was to just store the pickupactors, but again that seemed like id be keeping hold of a whole actor object when all I needed was a bit of info (class, image icon, name, size and a few others), just like with using the actual item actor

I have essentially got myself in a bit of a muddle and am hoping for an outside opinion/advice on how best to store items

Hey John (Again :D)

I recommend you to use your attempt to only store struct with specific Informations about the Item you want to pickup and spawn Actors depending on these Informations.

If you find a better way, please let me know :slight_smile:

Good Luck

Greetings

yeah I was leaning towards structs but kept second guessing myself, was looking some reassurance :wink: so thanks

I ask this question to myself quite often…

I used the structure to store all the items, but when it came to different item types it became a problem, as I need to store every item type variable in one structure. As a result, I got a weapon with consumables stats and vice versa.

Second version of my inventory was based on references.

  1. I created class based on “Object” (The most basic class in UE which only stores data) and put all item’s data in it. Then by creating child classes I created items to pick up.
    Also I created Actor classes to put them in the world. Each actor class consist class of the item. When actor is spawned, it builds an object based on item class.
    When the actor is traced, item reference is put to an array.
    It makes managing individual items ridiculously easy.

However it becomes a huge pain in the back when comes to making it work in multiplayer and saving inventory…

Just came up with an idea of storing inventory in a structure consisting the arrays of other structures, which consist item data. It’s more optimized than one structure, but still pain in the neck to manage

Okay, came up with another idea.

Everything is almost the same as before, but only item IDs and item count is stored in the inventory. All the items data is stored in the databases.
It makes managing inventory much easier.
Also, you can put any variables that can be changes to the structures along with IDs and counts. (For example if you need to implement dynamic stats, changing variables over time, etc.)