I want the player to be able to own multiple of 1 item, how should i go about this without having the item appear more than once in the inventory, but rather compile it into one stack of that item?
You can go through a lot of customizability with stackable items in your case. Personally, the thought I was having when getting to create my inventory was have a few variables inside each item file such as:
MaxObtainable : number
(This is so if you have one, you’d make it so you cannot pick up anymore)
CanStack : boolean
MaxStack : number
CurrentStack : number
This does require each item to have it’s own table but if you lay it out like this, you can decide how you want to organize these items in a much more precise way. You might make unique stacks of items which originate from another stack due to some of these modifications (like stats) you made to one of the items in the stack. And keeping them in the same file could severely complicate things.
This is how I do it, which is because I have unique stats for items.
Although this completely depends on how your inventory is done since you could just do something rather simple such as ["Item1"] += 1 inside your inventory. Then the client reads the index which is what it is and value which would be the quantity. And then store all the information that doesn’t need saved in another file.
Getting into how you’d read it in the code, fairly simple on both ends.
On the server, when trying to add an item to a stack you’ll just need a simple verification of whether the user has any of these types of items and whether that item is stackable. In my example I’ll be having a dictionary where I hold the content of what’s in the inventory and a item file that says whether the item can stack, what it’s max obtainable amount is, what the max stack the item can have.
local function GetItemFile(item_name : string)
-- // All this will do is return a table which
-- // Allows the system to see whether the item can stack
-- // Or other additional content per item you may need
return file -- // Just pretend it did return something in this case
end
-- // PlayerProfile is just a file that holds the saved data for a user
-- // Passing the item name instead of a table for ease of example
local function AddItemToInventory(playerProfile, item_name : string)
local itemFile = GetItemFile(item_name)
-- // We'll check if there are any current items
-- // In the player's inventory before arbitrarily adding it
-- // Probably need a better method than this to get a specific stack
-- // Which might also check whether the CurrentStack == MaxStack
local amount = 0 -- // you'll probably want to count how many you have
local inventoryItem = playerProfile.Inventory[item_name]
if (inventoryItem) then
-- // Check if you can obtain more
-- // Check whether it can stack
if (itemFile.MaxObtainable <= amount then
-- // Just don't add any more of this item could probably return
elseif (itemFile.CanStack) then
inventoryItem.CurrentStack += 1
-- // You could also make it convenient for checking
-- // Such as
inventoryItem.IsStackFull = inventoryItem.CurrentStack >= itemFile.MaxStack
end
else
-- // Just add the item as you normally would
end
end
Then the client just be given the inventoryItem and it can just as easily arrange those x?? numbers that you posted in that image.
This is probably not a very intuitive answer of mine but I hope it gives you a better idea how the way you want to handle inventory item’s stacking.