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.