Issue with adding a max stack number to stackable items

Hey,

I tried to add a maximum stack number for inventory items that are stackable, but I didn’t manage to do it as I would have expected.

I tried a method that was suggested in the RPG discord channel, but the solution is not really working how I would’ve expected.

The suggestion was the following:

Inside the InventorySlotUI.cs i added the following:

        [SerializeField] private int maxStackSize = 10;

public int MaxAcceptable(InventoryItem item)
        {
            if (inventory.HasSpaceFor(item))
            {
                if (GetItem() == item && item.IsStackable())
                {
                    return maxStackSize - GetNumber();
                }

                return maxStackSize;
            }

            return 0;
        }

I then made some pickups that had different numbers of quantities:

  • 1 pickup with 10 potions
  • 1 pickup with 20 potions
  • 1 pickup with 30 potions

My expectations were as follows:
If I picked up the 10 stacked potions, they would go to the 1st available inventory slot. This worked as expected since the maxStackSize is 10.
When I went ahead and picked up the stack of 20 potions, my expectation was that, if I already had 10 stacked potions in my inventory and the maxStackSize is 10, then 20 stacked potions would split accordingly, meaning that 10 would go to the next available inventory slot and the rest 10 to the next one and so on. The same with the 30 stacked potions.

What actually happens is that, no matter the size of the stack of potions that I pickup, they will all go in the same inventory slot, so even if the maxStackSize is 10, if I pickup a total of 100 potions, they will all go in the same inventory slot.
The problem with this is that if you want to move that stack of potions around the inventory, to place them in a different slot, you can’t. They will always snap back to the initial slot that they were in. I think this is because of the maxStackSize.

If you put the potions 100 stacks on the action bar, then try to move them back in the inventory, only a stack of 10 potions will be added to the inventory slot that you dragged the potions to. If you then try to move more in a different inventory slot, those potions will be added to that initial 10 stack, making it a 20 stack and so on, in the end making it unable to move the potions around.

Maybe it was wrong of me to think that it would work this way with how the Inventory / Dragging is made. If this requires more implementation, then I am stuck trying to figure out where changes are needed for this to work as I expected.

The MaxAcceptable is only part of the equation. It manages drag and drop, but that’s not how we pickup items in the world. When you click on a pickup, it bypasses the InventorySlotUI completely. This means that you’ll need to make adjustments to AddToFirstEmptySlot in Inventory.cs, and probably to Pickup itself (to leave the remaining items, if there are still leftover items).

Unfortunately, I’m headed to work soon, so I don’t have time to get into specifics, but the idea is that when you add an item to a stack, in Inventory.cs, you’ll need to calculate the correct number to add to the stack, reduce the number in the pickup, then AddToFirstEmptySlot again, until you’ve picked up everything in the pickup or you’ve run out of empty slots.

That will require adjustments to FindSlot as well, because if a slot is already full, you don’t want to consider it for AddToFirstEmptySlot.

Thanks for the explanation Brian. I kind of realized that more implementation would be needed since the course was not created with this behavior in mind.

Sadly all those things are above me until I get more experience. I guess this feature will stay on the TODO list for a while.