【问题标题】:Lua add item to table issueLua将项目添加到表问题
【发布时间】:2015-07-21 11:45:43
【问题描述】:

下面函数的目标是检查容器是否有空间存放新项目,如果有,则将项目添加到带有容器的表中 名称作为键/值或堆栈在另一个预先存在的项目之上。

当项目需要更改其中一个键/值时出现问题,它最终将所有具有相同名称的项目更改为新的键/值。

我找不到任何迭代当前项目并更改除 quantity 之外的任何值的代码,所以我假设添加新项目时出现错误,据我了解可能存在函数中的实例化或引用问题?不过,我对 lua 很陌生。

这是函数的控制台输出,添加了一些 print() 以提供更多上下文

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.
...

除了新项目将所有其他同名项目设置为同一个容器之外,一切似乎都在工作。

项目传递的初始函数

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

不可堆叠物品示例

{
    name = "knife",
    type = "weapon",
    qty = 1,
    stackable = false,
    bulk = 900
}

可堆叠物品示例(可堆叠物品在没有可用堆叠时执行相同操作。)

{
    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

我也尝试过table.insert(items, item),但没有成功,在阅读下面的链接后听起来在这种情况下它的行为并没有什么不同。

http://lua-users.org/wiki/TablesTutorial

【问题讨论】:

    标签: lua lua-table


    【解决方案1】:

    一切都说得通,我不是在“复制”表格,而只是在引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 2019-08-16
      • 1970-01-01
      相关资源
      最近更新 更多