The goal of the function's below is to check containers if there is space for the new item and if so add the item to a table with the containers name as a key/value or stack ontop of another pre-existing item.
Something goes wrong when the item needs to change one of it's key/value's, it ends up changing all items with the same name to the new key/value.
I can't find any code which iterates over the current items and changes any values apart from quantity so I'm assuming it's an error when adding a new item, from my understanding there might be an instancing or referencing issue within the function? I'm very new to lua though.
This is a console output of the function's with some print()'s added to give some more context
Adding Item: knife
Not Stackable
Slot Found in: leg
Item Added.
...
Current Items:
knife 1 900 leg
...
Current Containers:
chest 0
leg 1
back 0
hand 0
...
Adding Item: twig
Stackable
No Stack Found.
...
Slot Found in: leg
Item Added.
...
Current Items:
knife 1 900 leg
twig 7 2 leg
...
Adding Item: knife
Not Stackable
Slot Found in: hand
Item Added.
...
Current Items:
knife 1 900 hand
twig 7 2 leg
knife 1 900 hand
...
Current Containers:
chest 0
leg 1
back 0
hand 1
...
Adding Item: knife
Not Stackable
Slot Not Found
Item Not Added.
...
Everything seems to be working apart from the new item setting all other items of the same name to the same container.
initial function with the item passed
local stackFree = checkStackFree(item)
local slotFree, slotContainer = checkSlotFree(item)
--If Item was Not Stacked
if not stackFree then
--If Slot Free & Space Available
if slotFree then
item.container = slotContainer
items[#items+1] = item
containers:addItems(slotContainer,1)
containers:addBulk(slotContainer,1)
return true
else
--No Space for New Item
return false
end
--New Item was Stacked
return true
end
example non-stackable item
{
name = "knife",
type = "weapon",
qty = 1,
stackable = false,
bulk = 900
}
example stackable item (stackable items do the same thing once there are no stacks available.)
{
name = "twig",
type = "misc",
qty = 1,
stackable = true,
stackMax = 20,
bulk = 900
}
checkStackFree()
--Check if New Item is Stackable
if item.stackable then
--Iterate over Current Items
for i,v in ipairs(items) do
--Find a match
if v.name == item.name then
--Check Matching Item + New Item Quantity less than Max
if v.qty+item.qty < v.stackMax then
--Check Matching Item Container has Space
if containers:getBulk(v.container)+(item.qty*item.bulk) < containers:getMaxBulk(v.container) then
--Modify Matching Item Quantity
v.qty = v.qty+item.qty
return true
end
end
end
end
end
return false
checkSlotFree()
--Check Current Items < Max Items
if containers:getItems("chest") < containers:getMaxItems("chest") then
--Check Current Bulk + New Item Bulk < Max Bulk
if containers:getBulk("chest")+item.bulk*item.qty < containers:getMaxBulk("chest") then
return true, "chest"
end
end
... repeat for other containers
--If no Containers already escaped, We're Full
return false, nill
I have also tried without success table.insert(items, item) which after reading the link below doesn't sound like it would behave differently in this circumstance.