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.)
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.
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 = *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.