View Single Post
01-02-18, 12:35 AM   #1
seyan777
An Aku'mai Servant
Join Date: Feb 2017
Posts: 35
Why does the following code initially print an incorrect output?

Hi,

I wrote the following codes to check the amount of completed artifact for each races.

Lua Code:
  1. local b = CreateFrame("Button", nil, UIParent, "OptionsButtonTemplate")
  2. b:RegisterEvent("PLAYER_LOGIN")
  3. b:SetPoint("TOP", 0, -100)
  4. b:SetText(GetArchaeologyInfo())
  5. b:SetScript("OnEvent", function(self, event, ...)
  6.     if event == "PLAYER_LOGIN" then
  7.         self:UnregisterEvent("PLAYER_LOGIN")
  8.  
  9.         if not IsAddOnLoaded("Blizzard_ArchaeologyUI") then
  10.             LoadAddOn("Blizzard_ArchaeologyUI")
  11.         end
  12.     end
  13. end);
  14. b:SetScript("OnClick", function()
  15.     ArchaeologyFrameCompletedButton:Click()
  16.  
  17.     local totalCompletion = 0
  18.  
  19.     for i = 1, GetNumArchaeologyRaces() do
  20.         local j = GetNumArtifactsByRace(i)
  21.  
  22.         local raceName = GetArchaeologyRaceInfo(i)
  23.  
  24.         local currentCompletion = 0
  25.  
  26.         for k = 1, j do
  27.             local _, _, _, _, _, _, _, _, completionCount = GetArtifactInfoByRace(i, k)
  28.  
  29.             currentCompletion = currentCompletion + completionCount
  30.         end
  31.  
  32.         totalCompletion = totalCompletion + currentCompletion
  33.  
  34.         if j > 1 then
  35.             print(string.format("\124cffffff00%s:\124r %d", raceName, currentCompletion))
  36.         end
  37.     end
  38.  
  39.     print("======================")
  40.     print(string.format("\124cffffff00Total:\124r %d", totalCompletion))
  41. end)

The problem is that it prints an incorrect data on its initial click, then if I click the button again, it starts to print a correct amount.

I don't get where I'm doing it wrong

edit: Same behavior occurs after pressing solve button

Last edited by seyan777 : 01-02-18 at 01:09 AM.
  Reply With Quote