Hello, I’m following an inventory tutorial but now I want to expand it and stack items. I want to check if an object is already in the inventory and in that case stack it. I suppose that the “contains” node is used for this, but it always returns false even when the variable matches the array. What is wrong? Thanks
you would want to go though your inventory looking for a unique identifier for the item, most likely the name of said object. use a each loop with a break so you can break it off when you find a suitable index
then you need to check if the current quantity you have hits the stack limit of that item.
If both things are true you will want a local variable that will hold the index to add to for your inventory array, as well as a local bool variable to say a stack is found. you would put the stack is found bool as true if you found one and false if one is not found. on the true branch roll it back around to the loop and break it.
You will take the completed line from the for each loop down into a branch which is connected to the “stack found” bool. now this is where it gets a little hard to follow.
you will need to add the quantity of what you have to the quantity you are trying to add. (unreal crashed at this point so no more pictures)
then you check to see if that new quantity is less then or equal to your stack limit.
You will then set array element, and plug your “index to add to” variable to the index, break the item info and set the new info with the updated quantity.
IF your quantity is more than your stack limit, you will do much the same as if it wasn’t but just plug your stack limit into the new quantity. make a new local variable called “left over” and call your add to stack function again. for the input break it open and put the name and such from the “item to add” but use your “left over” variable for the quantity.
if a stack wasn’t found just add the item as normal off the “stack found” branch false line.
with how this is set up it will go through your array until the item is all put away. if you have a inventory limit you that should be handled by your add item function to make sure you have enough inventory space.
Are you sure you’re not just looking at two separate object instances that both happen to be of the same class?
(Although the path name BC_ContainerItem_C_2 seems to be the same!)
Also, your screen shots do not show us what the very next node is taken from the branch.
In general, when I have this type of stacking, I end up using a Map instead of an array. The key is the “kind of item” and the value is either a counter, an object containing information about the item in inventory (which slot, etc,) or an array of such information objects, depending on how fancy it is. Typical RPGs where stacks have a max count and you can have more than one stack, need the latter.
Use the object type as identifier, look it up in the map, and manage all your apples based on that result.
i did mention that unreal crashed on me while i was making the post so i didnt have any more pictures.
And yes a map would work but then you have to look through the key and the value. Using maps ended up being overly complicated but that may be due to the fact of the kind of games i make where a struct is more useful then a map. I have found that using an array of a struct to be a simpler solution as well as easier to manage. i can also add more information to the struct as needed while iterating through making a inventory system.
this inventory system i demonstrated here is actually based off/inspired by ryan layleys ue5 inventory system tutorial. ill link it Here.
following this tutorial should give OP what they need and its easily expandable and modified as needed.
You misunderstand what the map is for. You still use a struct, but you decouple the “object kind definition” from “inventory management” concerns.
Anyway, my “you don’t show the next node” comment was for the OP.
ok, AndracoDragons’ answer worked for me. The thing is that I followed Dengojaba’s tutorials in which he makes a Tarkov-style inventory with different slots and containers and in which he does not use structures, only arrays.
Then I must check not only an inventory, but the inventory of each item that I have equipped. I don’t know if it’s the best way to do it, since I’ve seen everyone using structures for inventories. But I wanted to try, I’m pretty new to UE5.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.