Thread Tools Display Modes
11-16-22, 03:28 PM   #1
Walkerbo
A Cobalt Mageweaver
 
Walkerbo's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 233
Looping through bags + reagent bag

Hi all

I need to loop through all equipped bags including the reagents bag.

After 10.2 changed the way we reach bags and the new reagents bag my original loop was;
Lua Code:
  1. for bag = 0, 4 do -- check each bag
  2.     for slot = 0, GetContainerNumSlots(bag) do -- check each bag slot
  3.         local itemID = GetContainerItemID(bag, slot)
This used to loop through all my equipped bags, starting with the backpack(0) and each following bag(1-4)

From reading the new changes it shows that the new reagents bag(-3)
So to test I used;
Lua Code:
  1. name = C_Container.GetBagName(-3)
  2. print(name)
This returns nothing, no errors, not even a nil;

So then I tried using the bag constants;
Lua Code:
  1. for bag = BACKPACK_CONTAINER, NUM_BAG_SLOTS,BACKPACK_CONTAINER+NUM_BAG_SLOTS+1 do
  2.     for slot = 1, C_Container.GetContainerNumSlots(bag) do
  3.         local itemLink = C_Container.GetContainerItemLink(bag, slot)
  4.         if itemLink then
  5.             print(bag, slot, itemLink)
  6.         end
  7.     end
  8. end
This looped through the backpack only.

If I remove the backpack;
Lua Code:
  1. for bag = NUM_BAG_SLOTS,BACKPACK_CONTAINER+NUM_BAG_SLOTS+1 do
  2.    for slot = 1, C_Container.GetContainerNumSlots(bag) do
  3.       local itemLink = C_Container.GetContainerItemLink(bag, slot)
  4.       if itemLink then
  5.          print(bag, slot, itemLink)
  6.       end
  7.    end
  8. end
Which loops through all the bags excluding the backpack.

At this point I am lost, I still have not found a fool, (me being the fool), proof way to loop through all the bags in one sweep.

What am I missing here?
How do I loop through all equipped bags, backpack, bags 1-4 and the reagent bag in one go?
__________________
"As someone once told me, frames are just special types of tables, and tables are special types of pointers."
Fizzlemizz
  Reply With Quote
11-16-22, 10:27 PM   #2
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
The "numeric for" iteration in Lua has three expressions:

for index = a, b, c do

a is the start
b is the end
c is optional and is how much to increment each iteration (+1 if omitted)

The following are some examples and what index will be:

for index = 1, 9 do
1, 2, 3, 4, 5, 6, 7, 8, 9

for index = 9, 1, -2 do
9, 7, 5, 3, 1

for index = 1, 2, 3 do
1

Notice how the third one only has 1, which is because the increment of 3 tried to go to 4 but that's beyond the max of 2.

BACKPACK_CONTAINER is a constant of 0
NUM_BAG_SLOTS is a constant of 4

Both, along with the new reagent bag of 5 (which doesn't have a direct constant), are defined in FrameXML\Constants.lua. However, there is also the Enum.BagIndex table and a new constant NUM_TOTAL_EQUIPPED_BAG_SLOTS.

So your first attempt is "for i = 0, 4, 5 do", which will only do 0. Removing the 0 in your second attempt ends up with "for i = 4, 5 do" which will go through 4 and 5, 4 being the leftmost "real" bag and 5 is the new reagent bag.

Try the following:

Lua Code:
  1. for bag = BACKPACK_CONTAINER, NUM_TOTAL_EQUIPPED_BAG_SLOTS do
  2.     for slot = 1, GetContainerNumSlots(bag) do

And finally, -3 is the reagent bank.
  Reply With Quote
11-17-22, 08:13 AM   #3
Xruptor
A Flamescale Wyrmkin
 
Xruptor's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2005
Posts: 137
Yep they changed the bags with the latest patch. You can find the changes here.

https://wowpedia.fandom.com/wiki/BagID

It should give you constants you need to loop through the bags you want
__________________
Click HERE for the ultimate idiot test.

if (sizeof(sadness) > sizeof(happiness)) { initDepression(); }
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Looping through bags + reagent bag

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