WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   Searching bags for item(s) (https://www.wowinterface.com/forums/showthread.php?t=56512)

myName 08-08-18 05:29 AM

Searching bags for item(s)
 
What I am trying to do here is loop thru my bags to find items that match an item id. FaithfulDeleteList is a saved variable table of ids that are previously added, but it never reaches the line that should print "is this thing on?" I can't seem to find where I am going wrong?

Code:

if event == "ITEM_PUSH" then
                -- Search my bags for item
                for bag=0,4 do
                        for slot=1,32 do
                                local bagItemID=GetContainerItemID(bag,slot)
                                for i = 1, #FaithfulDeleteList, 1 do
                                        if bagItemID then
                                                if bagItemID == FaithfulDeleteList[i] then
                                                        print("is this thing on?")
                                                end
                                        end
                                end
                        end
                end
        end


myrroddin 08-08-18 06:06 AM

Not all your bags have 32 slots in them. Your main backpack certainly doesn't. You should get the bag size first.

myName 08-08-18 06:45 AM

Quote:

Originally Posted by myrroddin (Post 329380)
Not all your bags have 32 slots in them. Your main backpack certainly doesn't. You should get the bag size first.

Code:

if event == "ITEM_PUSH" then
                -- Search my bags for item
                for bag=0,4 do
                        for slot=1, GetContainerNumSlots(bag) do
                                local bagItemID=GetContainerItemID(bag,slot)
                                for i = 1, #FaithfulDeleteList, 1 do
                                        if bagItemID then
                                                if bagItemID == FaithfulDeleteList[i] then
                                                        print("is this thing on?")
                                                end
                                        end
                                end
                        end
                end
        end

So i figured using GetContainerNumSlots would address the issue you brought up, but it doesn't seem any different. /bangsheadondesk

Vrul 08-08-18 07:35 AM

ITEM_PUSH just lets the UI know to do the animation of the item going into a bag. BAG_UPDATE is when the item has actually made it into the bag.

myName 08-08-18 08:04 AM

It making it to be bag is not of much concern, where as the inventory still needs to be scanned to check if itemid from saved table is in the bags to begin with if that makes sense, though I have implemented the BAG_UPDATE event you mentioned, still not working as intended.

Edit: Would it make better sense to start from the table. Get first id in table, scan each bag slot for the id, or like I'm trying to have it do now is slot 1 scan all ids, next slot scan all ids etc. I hope this makes sense.

Rilgamon 08-08-18 08:32 AM

Drycoded (copy and pasted from one of my addons) because I cant log into wow to test it atm ...

Lua Code:
  1. local FaithfulDeleteList = {
  2.     [1] = true,
  3.     [6289] = true,
  4.     [6308] = true,
  5.     [6309] = true,
  6.     [17057] = true,
  7.     [9] = true,
  8.     [2455] = true,
  9. }
  10. local function getItemId(link)
  11.     if(link) then
  12.         local _, _, itemString = string.find(link, "^|c%x+|H(.+)|h%[.*%]")
  13.         local _,id,_ = strsplit(":",itemString)
  14.         return tonumber(id)
  15.     end
  16. end
  17. local function BAG_UPDATE()
  18.     local a = 0
  19.     while(a<=NUM_BAG_SLOTS) do
  20.         local b = 1
  21.         while(b<=GetContainerNumSlots(a)) do
  22.             local link = GetContainerItemLink(a, b)
  23.             if(link) then
  24.                 local bagItemID = getItemId(link)
  25.                 if(bagItemID and FaithfulDeleteList[bagItemID]) then
  26.                     print("is this thing on?")
  27.                 end
  28.             end
  29.             b = b + 1
  30.         end
  31.         a = a + 1
  32.     end
  33. end
  34. local f = CreateFrame("FRAME")
  35. f:SetScript("OnEvent", BAG_UPDATE)
  36. f:RegisterEvent("BAG_UPDATE")

Vrul 08-08-18 09:25 AM

Is there a reason your FaithfulDeleteList is an array and not a dictionary? Changing the way you store the info could cut out a lot of needless iterating.

myName 08-08-18 10:02 AM

Quote:

Originally Posted by Vrul (Post 329385)
Is there a reason your FaithfulDeleteList is an array and not a dictionary? Changing the way you store the info could cut out a lot of needless iterating.

This is what it presently looks like. Not sure if this shreds any light on the situation.
Code:


FaithfulDeleteList = {
        "1", -- [1]
        "6289", -- [2]
        "6308", -- [3]
        "6309", -- [4]
        "17057", -- [5]
        "9", -- [6]
        "2455", -- [7]
}


Vrul 08-08-18 10:53 AM

You are saving the itemID as a string and not a number so it will never match in your loop. This is why you should always post your entire code. When I get off work I'll post how to do it with a dictionary instead if someone hasn't already.

Rilgamon 08-08-18 11:14 AM

I've updated the code above.

Vrul 08-08-18 04:28 PM

Here is a version that throttles the amount of processing:
Code:

local FaithfulDeleteList = {
    [    1 ] = true,  -- <unknown>
    [    9 ] = true,  -- <unknown>
    [  2455 ] = true,  -- Minor Mana Potion
    [  6289 ] = true,  -- Raw Longjaw Mud Snapper
    [  6308 ] = true,  -- Raw Bristle Whisker Catfish
    [  6309 ] = true,  -- 17 Pound Catfish
    [ 17057 ] = true    -- Shiny Fish Scales
}

local frame = CreateFrame("Frame")
frame:Hide()

frame:SetScript("OnEvent", frame.Show)
frame:RegisterEvent("BAG_UPDATE")

frame:SetScript("OnUpdate", function(self)
    self:Hide()
    for bag = 0, NUM_BAG_SLOTS do
        for slot = 1, GetContainerNumSlots(bag) do
            local itemID = GetContainerItemID(bag, slot)
            if itemID and FaithfulDeleteList[itemID] then
                print(("Item found: %s [bag: %s, slot: %s]"):format(itemID, bag, slot))
            end
        end
    end
end)


MuffinManKen 08-08-18 08:56 PM

Wouldn't it be better to fire on BAG_UPDATE_DELAYED instead?


All times are GMT -6. The time now is 03:36 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI