Quantcast Why does the following code initially print an incorrect output? - WoWInterface
Thread Tools Display Modes
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
01-02-18, 02:40 AM   #2
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 417
The data is probably not available when you call the functions the first time.
https://github.com/Gethe/wow-ui-sour....lua#L157-L166
__________________
  Reply With Quote
01-02-18, 04:26 AM   #3
seyan777
An Aku'mai Servant
Join Date: Feb 2017
Posts: 35
Originally Posted by MunkDev View Post
The data is probably not available when you call the functions the first time.
https://github.com/Gethe/wow-ui-sour....lua#L157-L166
The question is where would those incorrect data come from.

Would they be a cached data from last session?

Last edited by seyan777 : 01-02-18 at 04:41 AM.
  Reply With Quote
01-02-18, 08:44 AM   #4
Ammako
A Frostmaul Preserver
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 256
I already have code that does what you want.
Give me like maybe an hour or so until I'm ready to leave bed so I can get the code from my PC.

ArchaeologyFindsTotals.toc:
Code:
## Interface: 70100
## Title: Archaeology Find Totals
## Notes: Displays the total number of finds for each race on the summary page of the archaeology journal.
## LoadOnDemand: 1
## LoadWith: Blizzard_ArchaeologyUI
ArchaeologyFindsTotals.lua
ArchaeologyFindsTotals.lua:
lua Code:
  1. -- total number of artifacts per race; change the 99s to actual numbers
  2. local perRace = {
  3.     10, -- Demonic
  4.     8, -- Highmountain Tauren
  5.     10, -- Highborne
  6.     12, -- Ogre
  7.     21, -- Draenor Clans
  8.     12, -- Arakkora
  9.     12, -- Mogu
  10.     12, -- Pandaren
  11.     10, -- Mantid
  12.     7, -- Vrykul
  13.     17, -- Troll
  14.     13, -- Tol'vir
  15.     10, -- Orc
  16.     9, -- Nerubian
  17.     25, -- Night Elf
  18.     17, -- Fossil
  19.     10, -- Draenei
  20.     31, -- Dwarf
  21. }
  22.  
  23. -- GetNumArtifactsByRace includes the current project, which may not have been completed before.
  24. -- This returns the number of artifacts that have been completed.
  25. local function GetCompletedByRace(raceIndex)
  26.     local count = 0
  27.     local numArtifacts = GetNumArtifactsByRace(raceIndex)
  28.  
  29.     if numArtifacts and numArtifacts > 0 then
  30.         for i = 1, numArtifacts do
  31.             if select(9, GetArtifactInfoByRace(raceIndex, i)) > 0 then
  32.                 count = count + 1
  33.             end
  34.         end
  35.     end
  36.  
  37.     return count
  38. end
  39.  
  40. -- request history when archaeology window opened
  41. ArchaeologyFrame:HookScript("OnShow", RequestArtifactCompletionHistory)
  42.  
  43. -- this function updates the summary page and runs in reaction to ARTIFACT_HISTORY_READY triggered by the above request
  44. hooksecurefunc(ArchaeologyFrame.summaryPage, "UpdateFrame", function(self)
  45.     for i = 1, ARCHAEOLOGY_MAX_RACES do
  46.         local raceIndex = i + (ARCHAEOLOGY_MAX_RACES*(self.currentPage-1))
  47.         local button = _G["ArchaeologyFrameSummaryPageRace" .. i]
  48.  
  49.         if button:IsEnabled() then
  50.             local name, _, _, currencyAmount, projectAmount = GetArchaeologyRaceInfo(raceIndex)
  51.             local done = GetCompletedByRace(raceIndex)
  52.  
  53.             button.raceName:SetText(format("%s\n%d/%d\n%d of %d", name, currencyAmount, projectAmount, done, perRace[raceIndex]))
  54.         end
  55.     end
  56. end)
  57.  
  58. -- this moves the 12 buttons up a bit to make room for the extra line of text
  59. local anchorPoint, relativeTo, relativePoint, x, y = ArchaeologyFrameSummaryPageRace1:GetPoint()
  60. ArchaeologyFrameSummaryPageRace1:SetPoint(anchorPoint, relativeTo, relativePoint, x, y+20)
  61.  
  62. -- makes the text darker
  63. for i = 1,ARCHAEOLOGY_MAX_RACES do
  64.     _G["ArchaeologyFrameSummaryPageRace" .. i].raceName:SetTextColor(0, 0, 0)
  65. end

(Credits go to Gello for this.)

This will add counters to the archaeology page showing you how many finds have been completed per race.
If you want it to print to chatbox instead, you should be able to change it so instead of changing the text on the archaeology pages, it would print the values to chat instead.

Last edited by Ammako : 01-02-18 at 09:28 AM.
  Reply With Quote
01-02-18, 08:14 PM   #5
seyan777
An Aku'mai Servant
Join Date: Feb 2017
Posts: 35
Sweet

Will give it a shot!

Thank you, Ammako!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Why does the following code initially print an incorrect output?

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