View Single Post
08-27-21, 12:03 PM   #1
xubo
A Murloc Raider
Join Date: Aug 2021
Posts: 6
Need help understanding why loop is not always processing entire table

I'm in the process of creating my first addon and teaching myself lua... I'm not an expert by any means so any help would be appreciated.

The addon I'm creating is a simple mount collection log which mimics the functionality that simple armory mount collection shows. It lists all the obtainable mounts, categorizes them and marks off those you have collected.

I've managed to get the main GUI and functionality sorted but it's not performing consistently. Meaning sometimes when you initialize the addon, the entire lua table has not been processed leaving a lot mounts missing in the GUI.

This issue is less of a problem when there are no addons enabled, but happens a lot more when there are other addons enabled at the same time. The main function of the code is below, I imagine the problem is to do with how many loops there are.

Any help is appreciated. I can provide the entire files if need be.

Lua Code:
  1. local mountFrames = {}
  2. local collectableMounts = 0
  3. --Creating a frame to place expansion content in.
  4. --For each value section and category run this loop
  5. for k,v in pairs(sectionNames) do
  6.     --Reset location of frames
  7.     local yTwo = -20
  8.     local xTwo = 20
  9.     local rowC = 0
  10.     local frameName = MCL:GetName()
  11.     --Cycle through each section and check if database entry matches any string.
  12.     for s = 1, #sectionStrings do
  13.         if v.name == sectionStrings[s] then
  14.             --If string matches a section then we loop through all categories for that section and create a frame. 
  15.             for i = 1, #v.category do
  16.                 local a = self:CategoryFrame(v.category[i], sections[s], xTwo, yTwo)
  17.                 local rowC = 0
  18.                 local x = 10
  19.                 local y = -25
  20.                 for b,n in pairs(mountList) do
  21.                     if n.name == sectionStrings[s] then
  22.                         for h,j in pairs(n) do
  23.                             if j.name == v.category[i] then
  24.                                 if j.mounts then
  25.                                     local total = 0
  26.                                     local totalc = 0
  27.                                     for t = 1, #j.mountID do
  28.                                         if j.mountID[t] then
  29.                                             local _, _, icon, _, _, _, _, isFactionSpecific, faction, _, isCollected, ID = C_MountJournal.GetMountInfoByID(j.mountID[t])
  30.                                             if (isFactionSpecific == true and faction == playerFaction) or isFactionSpecific == false then
  31.                                                 if rowC > 0 then
  32.                                                     x = x+48
  33.                                                 end
  34.                                                 rowC = rowC+1                              
  35.                                                 local mountFrame = self:CreateMountIcons("TOPLEFT", x, y, a, isCollected, icon)
  36.                                                 collectableMounts = collectableMounts+1
  37.                                                 table.insert(mountFrames, collectableMounts, mountFrame)
  38.                                             end
  39.                                             if isCollected == true then
  40.                                                 totalc = totalc+1
  41.                                                 totalCollected = totalCollected+1
  42.                                                 if v.category[i] ~= "Blizzard Store" then
  43.                                                     totalCollectedNoStore = totalCollectedNoStore+1
  44.                                                 end
  45.                                             end
  46.                                         end                                
  47.                                     end                    
  48.                                     for t = 1, #j.mounts do
  49.                                         if C_MountJournal.GetMountFromItem(j.mounts[t]) then
  50.                                             local mountID = C_MountJournal.GetMountFromItem(j.mounts[t])
  51.                                             local _, _, _, _, isUsable, sourceType, _, isFactionSpecific, faction, _, isCollected, mountID = C_MountJournal.GetMountInfoByID(mountID)                                      
  52.                                             if (isFactionSpecific == true and faction == playerFaction) or isFactionSpecific == false then
  53.                                                 if rowC > 0 then
  54.                                                     x = x+48
  55.                                                 end
  56.                                                 rowC = rowC+1
  57.  
  58.                                                 local mountFrame = self:CreateIcons("TOPLEFT", x, y, j.mounts[t], a, isCollected)
  59.                                                 collectableMounts = collectableMounts+1
  60.                                                 table.insert(mountFrames, collectableMounts, mountFrame)                                                   
  61.                                                 if isCollected == true then
  62.                                                     totalc = totalc+1
  63.                                                     totalCollected = totalCollected+1
  64.                                                     if v.category[i] ~= "Blizzard Store" then
  65.                                                         totalCollectedNoStore = totalCollectedNoStore+1
  66.                                                     end
  67.                                                 end
  68.                                                 local item, itemLink = GetItemInfo(j.mounts[t]);
  69.                                                 mountFrame:SetScript("OnClick", function(self, button, down)
  70.                                                     if (itemLink) then
  71.                                                         print(itemLink)
  72.                                                     end
  73.                                                 end)
  74.                                             --This will determine when to start a new row of mounts. Currently set to 6 mounts per row.                            
  75.                                                 if(rowC == 50) then
  76.                                                     y = y-45
  77.                                                     x = 10
  78.                                                     rowC = 0
  79.                                                 end
  80.                                                 total = total+1
  81.                                             end
  82.                                         end                            
  83.                                     end
  84.                                     a.title:SetText(v.category[i].." ("..totalc.."/"..total..")")
  85.                                 end
  86.                             end
  87.                         end
  88.                     end
  89.                 end
  90.                 yTwo = yTwo - 80
  91.             end
  92.         end
  93.     end
  94. end

Last edited by xubo : 08-28-21 at 11:13 AM.
  Reply With Quote