Search code examples
filterluafilteringworld-of-warcraft

Lua adding and filtering items in a table


I am writing a WoW addon that will track and filter a list of items.

Items that are not already on the list are added; items that are already listed should not be added.

The issue I am getting is my check function does not consistently prevent duplicate items from being added to the list.

Adding item A the first time works fine, trying to re-add it again if it was the last thing added is correctly not re-added.

Adding item B the first time works fine, trying to re-add it again if it was the last thing added is correctly not re-added.

The problem is that if I try to re-add item A when it was not the last thing added it incorrectly re-adds the item to the list; so essentially I can re-add items as long as they were not the last item to be added.

Here is a gif that shows you what is happening;

And here are my two functions.

I have also linked my full lua code here and the toc 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

I am not getting any errors at all.

I have also linked my full lua code here and the toc here.

Hopefully someone can explain how I have messed up and can also point me in the right direction to fix it.


Solution

  • in your function isItemOnList you return false inside for loop, so loop cant be executed for more than 1 iteration. You should put return false outside of for loop:

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

    also you can do without return , sonil will be returned by default , and for if checks nil is the same as false