Creating an Inventory System in UE4 (Choice of storing data) Need help

Hi UE4 community,

I am fairly new to UE4, but I’ve been learning from various tutorials and starter packs off the market place. I’m at a point where I am developing an inventory, and I would like your opinion on how to store the data. Details of my current state below.

What I have

  • Base_InventoryActor BP → Has 1 variable which is a data struct, which holds basic item data, such as name of the item, description, weight etc.

  • Base Weapon BP → Inherits from Base_InventoryActor and has additional data struct which holds weapon specific data, such as Damage, Level requirements etc.

  • Base Armor BP → Inherits from Base_InventoryActor and has additional data struct which holds armor specific data, such as Armor Rating, Durability etc.

  • etc… (4-5 more item classes that inherit from Base, each with their own data struct)

Inventory Question

I’ve been looking into different ways of building the inventory. Basically my issue is I cannot think of one way that is performance efficient AND satisfies my requirements. My initial plan was to give the inventory an array of each of those structs (i.e. Array of Base_Struct, array of weapon_struct, Array of Armor_Struct etc.) and then in the logic just build each item by mapping the data, but that seemed very cumbersome, as each item would have data in two arrays, its basic data in the base_struct, and its item-specific data in its own struct.

My other choice is adding an “ID” for every item in my data table, and then the inventory would just have an array of type ID, so each time the player opens their inventory, it would load the inventory by looking up the stored IDs from the data table.

What is the best solution in your opinion? I feel like the second choice (ID option) might present lag of some sorts because the game will have to look up data from the datatable literally everytime the player opens the inventory, but it seems like the neatest option. The first option seems very cumbersome to implement, and it will be tricky getting the details of the items from both their structs, but it seems like it would be less performance intensive.

Would like to hear your thoughts. Thanks in advance.

Best,
Nader

Hey,
I would recommend that you have a single array in your inventory that holds all items as a structure. I would not store blueprint references in the array and here is why, blueprint references are more difficult to save and reload. You can simply save a struct array in a save game and load it again later, references are more difficult.

You cannot use a array of IDs if you need to modify the items, e.g. if you want to change the current stack size etc.

Also you don’t need to make different structures for different item types. I attached the structure that I use, with this I can create various weapons, armor, usable´s etc.
If you want to know what the individual variables are for, you can find them here

Regards,
SYN

Hi Synia!

First off, thanks a lot for replying to my first post here ^.^ Just now saw the notification for your reply, apologies.

With regards to my approach to creating an inventory, this is the approach I am implementing now. Would love to hear your thoughts on how feasible it is or if it can be improved.

Approach:

Assuming I have 4 item types, Weapons, Armor, Consumables, Junk -> I have 5 data tables as follows:

  • Base_Item Information Datatable

  • Contains all variables that are common to all items (i.e. Name, Description, Weight, Sell value etc.)

  • Weapon_Item Information Datatable (contains weapon specific data such as Damage, Clip size etc.)

  • Armor_Item Information Datatable (Armor specific-data) etc. and same for the remaining ones below.

  • Consumable_Item Information Datatable

  • Junk_Item Information Datatable

Note that each item has an “ID” in the FName column which is consistent in all tables. i.e. every item will have an ID in its class-table and the same ID in the base_item table. For this, I have a convention that if the ID starts with 1 -> Weapon, 2-> Armor, 3-> Consumable etc.

Inventory:
I created a component which I can attach to anyone/anything. This component has the following variables:

  • Inventory Name

  • Inventory Current Size (integer of how many item slots it holds)

  • Content, which is an array of type struct which goes as follows:

  • ID

  • Quantity

So basically at any point, the player’s inventory will be a collection of IDs and Quantities (to determine stack size or something). When the player opens his inventory, I do a lookup in the datatable for each item to get its basic data from the basic table and its class-specific data from the class-specific table, and i populate every slot in his inventory from the lookup (with the item name, icon, weight, cost etc.).

Thanks again,
Nader