Dynamic inventory system

Hi. I’m trying to create a dynamic inventory system but I’m not doing something right.
I’ve created an int array and I have for example (Gun - id 1 and Key - 2). After I collect the Key for example I add the id 2 in the first index of the array. I think I’ve added this right to the array ( I’m new to BPs )
The problem is when I’m opening the inventory. I don’t know how to stop adding the same item into my inventory. I’m looping through the array and if I got the Key, it’s added twice into my inv.

Collecting the item

Geting the items into my inventory

This is how it looks like.

I know where the error is ( I loop each time the same array and when true is adding again the key ) but I don’t know how to pass the index which has already been added.
Thank you!

Hi,

Have you looked at any tutorials on YouTube? There are quite a lot on how to make inventories and I’ve never seen one made like how you are doing it, it seems overly complicated. I can give you steps here on how to make one if you need me to but it might involve starting over.

Hi. Thank you for your reply. I’ve watched a tutorial and it uses slot1,slot2 and so on, but if I have 30 objects to collect it’s a lot of work to add/remove/use so I’m trying with arrays. I’ve succedded something and I hope I can remove the item from the array after I use it. (I didn’t got so far)

As you can see here I succeededto loop through the array and check each index to see if it has some objects in it. The problem was that if index 0 has item 2, I couldn’t set the brush to texture with the Key texture which is = id 2 so I had to create an event to check each texture according to the id.
Thank you!

I’m not sure if you’re saying you got it working how you wanted or not but either way you shouldn’t really be casting to your character every tick in the widget(bad for performance), there are a few ways to go about doing it, for instance you could on construct of the widget cast it once and promote it to a variable and just call the variable each time instead.

Do you actually need to update the inventory each tick though? surely when it is open new items wont add to it.

Thank you! It works with ev construct. I’m new to unreal and I don’t know very well the bps.

I’m glad, so everything is working with the inventory as you wanted then?

Watch this. Dynamic Inventory System In Unreal Engine With Only C++ - YouTube

The system you are using to create the inventory is not very practic, in case you want to add more objetcs to your adventure, increase the code you need to add, and thats not the way.

Things i recomend to you:

1- Create a struct with the objects you want to collect in the inventory. This struct can have a id for each object, the name of the objetc (if you create an ENUM to store the objects name, better!), the reference to his mesh, the reference to his icon(texture2d) etc, or any other things you need for the objects.

2- Create an array of this objects struct, and define all the objects you need. Later you can add more objets if you want to the array whiout problems. This array can be in the gameinstance blueprint, so you can access it easily from everywhere.

3- you can create a datatable to store the list of structs too, and then pass all this data to the array in the gameinstance at the begin play. This process is not necesary if you define your data directly on the array, but is a clean solution when you want to add new objects and do not want to touch the blueprints, only the data table.

4- once you got your objets data defined in the gameinstance array, you can give the object id or name you need to your actual worlds objects, using a variable, so when you touch one of those in the scene you know the Id or the name of the object you just got it.

5- Now you can have your update inventory code in your character, or even better in the gameinstance( remember the game instance is persistent across levels!) Create an array to store the ids or names of the objects, but dont define any element, we can do it dynamically.

6- the code on the object you just touch it, only need to pass the id or named of the objectasociated to him to the custom event input where you inventory code is. So the inventory know witch object to add.

7- In the inventory code you got two options now, if you permit several copies of the same object or not. If you don’t want copies on your inventory, just use “AddUnique” to your inventory array using the id or name of the object passed. Now you got a new element with your object id or name on it.

8- when you want to show you inventory, you only need to iterate across all the element on you inventory array, get the id or name object from every array element, and then using it, get the rest of the object data ( you got it on the objects array we prepared in the step 2. So now, you now the name of the object, his mesh, his icon, etc, sou you can change the image.

Width this system, if you later want to add more objest, only need to add it in the array, you don’t need to change the code anymore. And you inventory pool is not limited to a numbers of objects, is dynamic so you can have many.

If you need some help with the code. let me know.

Always check post dates, this one is 2,5 years old…

Ouch, thats true! i have been confused by the previous message a few hours ago.
Anyway, thanks for the advise.