1. #1

    Get in bag item count by item id

    So, I am trying to create a LUA function to count the number of a specific item, the problem that I am coming up with is if there are stacks of the item in more than one bag.

    Code:
    local function countItem(bag, item)
    	for slot = 0, GetContainerNumSlots(bag) do
    		local itemid = GetContainerItemID(bag, slot)
    		if(item == itemid) then
    		        --Increment the item count here using GetContainerItemInfo
    		end
            end
    end
    I see two options, I can store the item count for each bag, and add those 5 numbers to get the total item count, or I can reset the item count to 0 and call countItem for every bag on every BAG_UPDATE event. I'm not a huge fan of either solution, but don't see a better way. Any suggestions?
    Author of Instance Profit Tracker
    Find out how much gold you earn soloing raids and dungeons

    Curse | GitHub
    WowInterface

  2. #2
    Deleted
    Just loop through (0 through NUM_BAG_SLOTS) and add them all up.
    Code:
    local function countItem(item)
        local c
        for bag=0,NUM_BAG_SLOTS do
            for slot=1,GetContainerNumSlots(bag) do
                if item == GetContainerItemID(bag,slot) then
                    c=c+(select(2,GetContainerItemInfo(bag,slot)))
                end
            end
        end
        return c
    end


    ---------- Post added 2011-09-11 at 09:05 PM ----------

    Checking the API doc, why not just use GetItemCount?

  3. #3
    Quote Originally Posted by Treeston View Post
    Checking the API doc, why not just use GetItemCount?
    Somehow I overlooked that, works perfectly for my situation
    Author of Instance Profit Tracker
    Find out how much gold you earn soloing raids and dungeons

    Curse | GitHub
    WowInterface

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •