Quantcast Searching bags for item(s) - WoWInterface
Thread Tools Display Modes
08-08-18, 05:29 AM   #1
myName
A Deviate Faerie Dragon
 
myName's Avatar
Join Date: Jul 2018
Posts: 10
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
  Reply With Quote
08-08-18, 06:06 AM   #2
myrroddin
A Molten Giant
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 942
Not all your bags have 32 slots in them. Your main backpack certainly doesn't. You should get the bag size first.
  Reply With Quote
08-08-18, 06:45 AM   #3
myName
A Deviate Faerie Dragon
 
myName's Avatar
Join Date: Jul 2018
Posts: 10
Originally Posted by myrroddin View Post
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
  Reply With Quote
08-08-18, 07:35 AM   #4
Vrul
A Frostmaul Preserver
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 291
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.
  Reply With Quote
08-08-18, 08:04 AM   #5
myName
A Deviate Faerie Dragon
 
myName's Avatar
Join Date: Jul 2018
Posts: 10
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.

Last edited by myName : 08-08-18 at 08:09 AM. Reason: just an idea
  Reply With Quote
08-08-18, 08:32 AM   #6
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 725
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")
__________________
The cataclysm broke the world ... and the pandas could not fix it!

Last edited by Rilgamon : 08-08-18 at 11:14 AM.
  Reply With Quote
08-08-18, 09:25 AM   #7
Vrul
A Frostmaul Preserver
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 291
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.
  Reply With Quote
08-08-18, 10:02 AM   #8
myName
A Deviate Faerie Dragon
 
myName's Avatar
Join Date: Jul 2018
Posts: 10
Originally Posted by Vrul View Post
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]
}
  Reply With Quote
08-08-18, 10:53 AM   #9
Vrul
A Frostmaul Preserver
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 291
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.
  Reply With Quote
08-08-18, 11:14 AM   #10
Rilgamon
Premium Member
 
Rilgamon's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 725
I've updated the code above.
__________________
The cataclysm broke the world ... and the pandas could not fix it!
  Reply With Quote
08-08-18, 04:28 PM   #11
Vrul
A Frostmaul Preserver
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 291
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)
  Reply With Quote
08-08-18, 08:56 PM   #12
MuffinManKen
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 95
Wouldn't it be better to fire on BAG_UPDATE_DELAYED instead?
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Searching bags for item(s)

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off