[SOLVED]About RPG Type Inventory Planning

Hello all.
I am working on a RPG type inventory for my project. But I am quite a novice and I wanted to consult to more experienced members about planing for functionality of inventory from C++ side.
I have checked and used all the tutorials I could find and up until now they have worked pretty well. I have a basic inventory system (a TArray of custom FInventoryItem class which in turn reads from a data table including details such as cost, stack numbers etc etc ) and it is connected to an UI.

Here is the tutorial I have used for base of inventory system:

Now I am trying to do things such as remove items, or changing slots where the item is located in UI. Which raised some questions for me. After looking for tutorials for a possible way I decided to come here. For example:

  • An inventory is an array in C++ and an array can not have “empty” elements in it, that is an array is “dynamic” must have a series of elements that is continoeus. So if I use Remove(), RemoveSingle() etc that element will be removed and the next element will take it’s place; changing “indices” and order of the array. Of course that is not acceptable for an RPG inventory. So after some searching and help I made an “empty item” and replaced the removed or slot-changed items with that one (since my inventory stores actual objects and not pointers using a nullptr for same purpose simply crashed the whole thing.)

https://i.ibb.co/x8pBVFT/Empty-Item.png

But the thing is an “empty value” or an “empty item” is still an “element” that is even though it looks like and functions like empty slot it still occupies an element and an indice in the array. That is for example if I have two items in my inventory and add another item to my inventory after dropping out the first item in my inventory first slot is still considered occupied and new item is added to 3rd slot, as you can see in the screenshot.

https://i.ibb.co/Z1ZWNy5/Example.png

So a solution I came up is querying the array, look for elements with the ID tag of the “empty item” and then emplacing the picked up items directly to the first found empty item’s indice (Inventory[0] = *NewAddedItem for example, if there is an empty value at 0 in array)

But I am not exprienced and not sure if this would work “nicely” so to speak or is there an another method that is well known and used for inventories.
Basically I want to achieve what is demonstrated in this video below but with a C++ base, not Blueprint.

Thank you for patience and helps beforehand.
Have a nice day.

if you want to be able to rearrange items or have them keep position when earlier items are removed then rhe way you are doing it is correct.

The array would most likely always be the max inventory size and items would only occupy slots that are needed.

Adding a new item should loop through the array to find the first available slot.

I would personally use pointers as its cleaner considering an empty slot is completely empty.

You just need to make sure you check if the element exists before attempting to access its struct members, which is most likely what caused the crash.

Ex:
if(items*){
// there is an item in this slot. Its safe to access
item*->cost;
}

if something is not nullptr, it is considered true for if statements

edit: saw your other post. Youd need to make the array an array of pointers to use them.

Thanks man. I’ll try doing it this way first thing tomorrow morning . I’ll report about how it functions and performs here.
Have a good day :wink:

Sorry for the late reply.
Thanks for your comment. I tried your solution and things really seem to be doing fine. I am marking this as solved.
Thank you again very much.