I’ve been trying to make a simple inventory system, and at the current stage, I’m using a debug key to a add a test item and see if it can add different amounts o said item correctly. The way the system works is by allowing some of those items to be stacked, and if said stack is at it’s maximum value, then search for the next open slot to start another stack.
For now, it is working whenever I try to add a full stack. Apples can only be stacked as a total of 50 in a single slot, for example, so if I send the command to add 50 apples, that’s it. It works. The problem, however, comes fomr the two following options of asking to add less of the maximum stack amount possible or higher than it, giving the two following responses:
Add more than 50 apples: The inventory only gets a full stack, not realizing there’s an empty slot available right at it’s side. So if I send “add 121 apples”, instead of making two stacks of 50 and one of 21, it only gives one of 50 for each command I send.
Add less than 50 apples: The game breaks with an Infinity loop error (Infinite loop detected. Blueprint: Blueprint Component_InventorySystem Function: Sequence Call Stack:), but I can’t seem to find where exactly this loop is happenning.
Here’s the blueprints currently tied to the blueprint component BPC_Inventory:
All slots already exist in a hidden way, and I just turn them visible once an item is included. After I clear it, the slot becomes invisible once again. Is that what you meant to ask?
Ah, OK. Sorry, I was a little too tired mentally at the moment I wrote at first. No, I don’t clear it, but I don’t think it’ll change much, but I’ll take a look here.
In FindSlotToStore, you have a sequence at the end that may be an issue. Your first path in that sequence may not be finished by the time you fire the second, ending the function. Also, the for each with break on that first line doesn’t do anything other than break. You don’t set anything before breaking so not sure what the point is. That BP is hard to read since it’s smaller. Not sure that’s what your issue is, but was something that stood out to me as a possible issue.
There is a lot of code here for what you are trying to do, and numerous strange things within.
I do not know exactly what is causing your issues, but here are a couple of things that jump out:
You are identifying slots to top-up (existing correct items, from Find Items) or fill (Find Empty Slots) and composing an array of slot structures by copying them from the inventory, you loop over this temp array and Find the corresponding struct in the inventory, which is probably problematic by itself, and almost certainly the case here as you are modifying the inventory as you go. You should identify by index, and have arrays of indices instead of slot structs, replace the Find with Get.
You are storing data table row handles in the slot structure, which again is strange by itself, but doubly so as you only ever use it to get the row name, which your slot struct already contains (ID). This won’t be related to you issue(s), but, well… strange.
You are clearly trying to break the problem down, which is of course not a bad thing in general but I think you are over doing it here and have made a somewhat convoluted mess as a result
Some tips:
A logging function for the inventory component will help debug:
Call before and after you add (in those cases where you don’t end up in an infinite loop), and then reproduce problematic cases using the debugger to step through the code and see where it goes wrong.
Consider throwing it all away and starting again, I think you have made this too complicated; you should probably have a single function with two loops, first loop looks for existing slots with right item type and tops them up if not full, then the second assigns anything left over into empty slots.
Edit: This maybe not what you wanted, but I implemented the suggested rewrite; didn’t test extensively but it appears to handle all cases, and not get in an infinite loop
Uses three locals, Amount Remaining, Stack Size & TMP Amount Picked Up, all integers.