(PROBLEM: INFINITE LOOP IN FUNCTION “F_SpaceInInventory?”)
Hello, I am trying to create an inventory system and I am currently working on the pickup section but I have a problem.
My logic follows:
Check if there is space in inventory for item (widget)
Add item to inventory (variable)
The problem occurs when I try to check if there is space in the inventory widget, I get an infinite loop.
The inventory system structure is like this:
Variable Inventory - Map (Item Base Class to store the type of item, integer to store the amount of item)
Widget Inventory - Consists of a uniform grid which has pre-added inventory slots added
Widget Inventory Slots - the slots that show the item and how many there are in the stack.
Stack Value - Integer variable to keep track of many of how many this item is worth, for example if the item stack value is 3 then if you pick up the item you will receive 3 of them.
Stack Left - Integer variable to keep track of how many of the stack value remain, for example if you pickup an item with SV=5 but you only pickup 3 then SL = 2.
Current Stack - Integer variable inside the inventory slot Widget to keep track of that slots stack value.
SlotFull? - Boolean variable inside the inventory slot Widget to keep track if the slots current stack is == to the item class max stack value
I see two infinite loop paths here, if the item is not valid or there’s no space in inventory the the while loop will go infinite, there’s no way for it to exit as long as the array length > 0
I suggest you remove the item from the array when you couldn’t add it this way the array will eventually have 0 items and the loop can exit.
One other problem you have is in the OnCompleted loop where you play a sound, you don’t know if you actually picked up the item or not, so perhaps add a bool and set it to true when an item is added to the inventory.
Hello, thank you I have added the remove from overlapping and the check (item added? is set after item is added in the add item function); however, I still get the infinite loop (inside the “Space in inventory” function). I do agree that those could have caused an infinite loop but I will haven’t tested those areas yet so it could not have been from those areas initially (but yes they would have cause an infinite loop if I didn’t remove them).
So basically, the infinite loop occurs inside the SpaceInInventory function by my understanding, please let me know if you are able to come up with any solutions
I think you don’t need that while loop inside F Inventory SpaceInfunction, your ForEach loop should handle the stack just fine. By the end of the loop you either added the stack or couldn’t find space for it or partially stacked, no need to keep looping, one loop is enough.
Hello, thanks for responding. I just realized I’m adding an item to the first slot every time because I’m not simulating the inventory. Sorry about wasting your time like this. I’m going to refactor my entire function and hopefully it then works