【问题标题】:Lua adding and filtering items in a tableLua在表格中添加和过滤项目
【发布时间】:2019-06-14 17:30:21
【问题描述】:

我正在编写一个将跟踪和过滤项目列表的 WoW 插件。

添加尚未在列表中的项目;不应添加已列出的项目。

我遇到的问题是我的检查功能不能始终如一地防止重复项目被添加到列表中。

第一次添加项目 A 工作正常,如果它是最后添加的内容,则尝试再次重新添加它,但未正确添加。

第一次添加项目 B 可以正常工作,如果它是最后添加的内容,则尝试再次重新添加它没有重新添加正确。

问题是,如果我尝试重新添加不是最后添加的项目 A,它会错误地将项目重新添加到列表中;所以基本上我可以重新添加项目,只要它们不是要添加的最后一个项目。

Here is a gif that shows you what is happening;

这是我的两个函数。

我还链接了我的full lua code heretoc here

-- check if item is already listed
local function isItemOnList(incomingItemID)
    for k, v in pairs(AAAGlobalItemLinkList) do
        if v.itemID == incomingItemID then
            print(v.itemID, ':matched:', incomingItemID)
            return true
        end
        print('no match', incomingItemID)
        return false
    end
end
-- add item link to list function
local function addItemLinkToList()
    local cursorItemType, cursorItemID, cursorItemLink = GetCursorInfo()
    print(cursorItemID)
    if not isItemOnList(cursorItemID) then
        local itemName,
            itemLink,
            itemRarity,
            itemLevel,
            itemMinLevel,
            itemType,
            itemSubType,
            itemStackCount,
            itemEquipLoc,
            iconFileDataID,
            itemSellPrice,
            itemClassID,
            itemSubClassID,
            bindType,
            expacID,
            itemSetID,
            isCraftingReagent = GetItemInfo(cursorItemID)
        tempItemList = {
            itemID = cursorItemID,
            itemName = itemName,
            itemLink = itemLink,
            itemRarity = itemRarity,
            itemLevel = itemLevel,
            itemMinLevel = itemMinLevel,
            itemType = itemType,
            itemSubType = itemSubType,
            itemStackCount = itemStackCount,
            itemEquipLoc = itemEquipLoc,
            iconFileDataID = iconFileDataID,
            itemSellPrice = itemSellPrice,
            itemClassID = itemClassID,
            itemSubClassID = itemSubClassID,
            bindType = bindType,
            expacID = expacID,
            itemSetID = itemSetID,
            isCraftingReagent = isCraftingReagent
        }
        table.insert(AAAGlobalItemLinkList, 1, tempItemList)
        print(cursorItemLink, 'added to list')
    else
        print(cursorItemLink, 'already listed')
    end
    updateScrollFrame()
    ClearCursor()
end
-- update scroll frames function
local function updateScrollFrame()
    wipe(listItems)
    for index = 1, #AAAGlobalItemLinkList do
        --if testItemRarity(AAAGlobalItemLinkList[index]) then
        table.insert(listItems, AAAGlobalItemLinkList[index]['itemLink'])
        --end
    end
    FauxScrollFrame_Update(testingScrollFrame, #listItems, NumberOfButtons, HeightOfButtons)
    for index = 1, NumberOfButtons do
        local offset = index + FauxScrollFrame_GetOffset(testingScrollFrame)
        local button = testingScrollFrame.buttons[index]
        if index > #listItems then
            button:SetText('')a
            button.index = nil
        else
            button.index = offset
            --local itemName, itemLink = GetItemInfo(listItems[offset])
            button:SetText(listItems[offset])
        end
    end
end

我没有收到任何错误。

我还在此处链接了我的完整 lua 代码和此处的目录。

希望有人可以解释我是如何搞砸的,也可以为我指出正确的方向来解决它。

【问题讨论】:

  • 你的AAAGlobalItemLinkList 似乎不时地被updateScrollFrame 改变。可能是在您更新AAAGlobalItemLinkList 时,验证检查失败。
  • 您是否使用多个线程?如果您没有使用线程安全列表,您可能会遇到同步/竞争条件错误。
  • @Walkerbo 但是我看到你注册了事件的更新功能,会不会有问题?
  • 在函数isItemOnList中你在for循环中返回false,你应该在for循环之后返回false
  • @AndrewKashpur 你应该把比作为答案

标签: filter lua filtering world-of-warcraft


【解决方案1】:

在您的函数 isItemOnList 中,您在 for 循环内返回 false,因此循环不能执行超过 1 次迭代。您应该将return false 放在for 循环之外:

-- check if item is already listed
local function isItemOnList(incomingItemID)
    for k, v in pairs(AAAGlobalItemLinkList) do
        if v.itemID == incomingItemID then
            print(v.itemID, ':matched:', incomingItemID)
            return true
        end
    end
    print('no match', incomingItemID)
    return false
end

你也可以不返回,所以nil会默认返回,if检查nilfalse是一样的

【讨论】:

  • 为答案干杯
猜你喜欢
  • 2014-09-22
  • 1970-01-01
  • 2015-07-21
  • 2014-03-05
  • 2020-11-17
  • 1970-01-01
  • 2013-09-05
  • 2016-03-19
  • 1970-01-01
相关资源
最近更新 更多