Max Stack Items

How can I determine a maximum amount of items per stack?


My bad, I originally thought you were talking about arrays, but I did find a forum related to items per stack

Sorry for the troubles

1 Like

I haven’t finished (or even started) the RPG Inventory course, so this comes from someone that only had a quick glimpse of the code.

I saw two ways you could set a limit for the stacks;

  • Make the Inventory script handle it. Create a serializable variable that delimits the max size of every stack.
  • If you want customization per item, you could add the stack size for each item as a serializable variable in the InventoryItem script.

You could have a combination of both, let the Inventory script handle the max size of the stacks, then add a bool variable to the InventoryItem to see if the item is stackable or not.

I discuss the Game Design of each approach in the following paragraphs, if you are not interested in that, you can totally skip it.

Predetermined stacks are great because the player can easily remember how many items can be carried at any given point, but it limits the customization which might affect the overall experience of the player depending on what you are going for.

Having custom stack sizes for each item makes the inventory a game unto itself, Which type of potion should I carry? Minor Potions with a 20-stack limit? Great potions with a 5-stack limit? A combination of both at the risk of losing a slot in my inventory? That sounds very compelling to me, but for others, it’s just a nuance or they simply won’t care and just go for the potion with the highest healing capabilities. Is it worth investing all that time to balance out all these things? Depends on your targeted market. Resident Evil 4 (the original, haven’t played the remake), does a fantastic job on this, you might want to study it.

Focus on the experience you want to give to the player and see which system is the best fit for your game, you can also test things out.

Hope this helps!


Thanks for the tips, its helps a lot, but, my doubt is more about the code, what i need change for the inventory holds, max stack?

1 Like

Thanks, but, how this fit on the inventory system of the course?


Oh I see what you mean, I have the course but haven’t done the inventory part, I’m sorry, but I unfortunately won’t be much of a help, luckily though there are very smart people helping you out as we speak.

1 Like

Don’t copy-paste the following, I just had a quick glimpse of the code and lectures, but it might give you a good starting point.

        [SerializableField] int maxStackSize; //Add this line
        public bool AddItemToSlot(int slot, InventoryItem item, int number)
           // Add the second condition
            if (slots[slot].item != null || slots[slot].number >= maxStackSize)
                return AddToFirstEmptySlot(item, number); ;

            var i = FindStack(item);
            if (i >= 0)
                slot = i;
            slots[slot].item = item;
            slots[slot].number += number;
            if (inventoryUpdated != null)
            return true;

I think that alone should work… but I’m not 100% sure. I’m in a bit of a hurry right now, but if no one has answered you after I return, I’ll be sure to check your issue in detail. In the meantime, Why not search the forums for other answers?

Here’s one that might help you:

1 Like

Thanks, anyway.

Thanks for the help, I will try this.

No problem, sorry I wasn’t able to help.

1 Like

A few considerations…
The Drag and Drop system gets the Max stack size from the InventorySlotUI (well, specifically because it implements IDragDestination<T>

The method in question is MaxAcceptable, which in EquipmentSlotUI, we have return 1, but in InventorySlotUI and ActionSlotUI we currently return the max int, or for all intents and purposes “infinite”.

These methods need a way to find the maximum stack size, and the best place to put this is in the InventoryItem itself.

[SerializeField] int stackSize;

This needs to be exposed publicly, but we also want some sanity checks on the system, so I like to make the public method prevent non-stackable items from having a stack size larger than 1

public int StackSize()
    return stackable? stackSize: 1;

This returns the stackSize if it’s stackable, or 1 if it’s not.

Then in our InventorySlotUI.MaxAcceptable(), rather than returning int.MaxValue, return item.StackSize(); Do the same in ActionSlotUI.

This handles all drag and drop effectively.

1 Like

I think is this, thank you very much.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms