GetItemInfoDelayed

From Wowpedia
Jump to: navigation, search
This is a user-defined function that you can copy and paste into your addon.


Process item info and take into account whether it is in the local cache or not.

Usage

GetItemInfoDelayed(itemID or "itemString" or "itemName" or "itemLink")
  • Change the print(...) line in the process() function to do what you want with the info, then call the function.

Results

  • As it is written below, the item info will be printed to the chat frame.

Code

local f -- the OnUpdate frame.
local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice

-- #table only works if all the indexes are in sequential order and there are no breaks.
-- This function 1 if there are more than 0 elements. It does not return the actual number of elements. This is to save processing time.
local function count(tab)
    for _ in pairs(tab) do
        return 1
    end
    return 0
end

-- Data processor.
-- self == f
local function process(self, item, ...)
    itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice = ...
    if itemName then
        -- This is where you want to do stuff to the information you requested.
        -- Change this line and leave everything else as-is.
        print(itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, itemTexture, itemSellPrice)
        
        -- remove the item from the queue
        self.itemQueue[item] = nil
    end
end

-- OnUpdate function.
-- self == f
local function update(self, e)
    self.updateTimer = self.updateTimer - e
    if self.updateTimer <= 0 then
        for _, item in pairs(self.itemQueue) do
            process(self, item, GetItemInfo(item))
        end
        self.updateTimer = 1
        
        -- Hide the OnUpdate frame if there are no items in the queue.
        if count(self.itemQueue) == 0 then
            self:Hide()
        end
    end
end

-- Add the requested item to the queue.
-- If the item is in the local cache, it will be available immediately.
-- If the item is not in the local cache, wait in one second increments and try again.
-- Call this in place of GetItemInfo(item).
-- This function does not return any data.
function GetItemInfoDelayed(item)
    -- Create the frame only when it's needed and don't create it again.
    if not f then
        f = CreateFrame("Frame")
        f:SetScript("OnUpdate", update)
        f.itemQueue = {}
    end
    
    -- Set the timer to 0, add the item to the queue, and show the OnUpdate frame.
    f.updateTimer = 0
    f.itemQueue[item] = item
    f:Show()
end