How to stack inventory items when the las slot of your inventory

I’ve been learning unreal for a few months, I started working on my first game in Unreal. I wanted to start simple so it is a pet game, that’s entirely ui basically. I’m trying to put in an inventory system, I’ve gotten a lot of things working but just a couple of problems.

I got stacking to work but I want my inventory to have a limit of 4 slots so i added a inventory array check, but once it reaches the end the item won’t stack any more. I also want to make it so when an item’s max stack is reached, that’s it, no more of that certain item can be added, in current slot or next slot, but i’m just not sure how to go about it. I used the for loop in the inventory widget to make the slots. Here are some pictures.

Problem (images are placeholders)

Max Slot Check

Find Empty Slot

Add Item

The completed return of your Find Empty Space function is probably overriding the return inside the loop on last index since they’re essentially fired at the same time (blueprints don’t handle early returns the same way c++ does). Instead of doing a return inside the loop, try making local vars for the found index and bool and set them in the loop. Then make the completed pin return the local vars. Might also want to change the loop to a for each with break, and wire the true branch in loop back to break, so it doesn’t waste time looping thru array after finding a spot.

Probably also want to keep track of items not added to a stack. As it is, if you tried to add 5 doggo treats or whatevs and there was only room for one more in last stack, 4 would go poof. In my current game, I have the item stacking logic nested in a a while loop, with the condition being if “number to add” >0 (number is local var which is an input used when giving items to an actor). Every time the stack logic successfully stacks, I subtract the amount stacked from the number to add. When the while loop completes, I call the inventory changed dispatcher, and return unadded item count ( so they’ll stay in a chest/store/corpse/etc.. inventory).