Quantcast GetCurrencyInfo returns 0 CurrentAmount - WoWInterface
Thread Tools Display Modes
11-30-19, 07:54 PM   #1
fail2reap
A Murloc Raider
Join Date: Nov 2019
Posts: 3
GetCurrencyInfo returns 0 CurrentAmount

The following code attempts to store the characters name and current count of Prismatic Manapearls into table a once it recognizes that the add-on has been loaded.

The issue I am having is that when iterating through the table, or even just after getting the value, it always returns CurrentAmount as 0. As a test I also iterated through all currencies and their CurrentAmount values, and surprising they were all 0.

Is the CurrencyAmount loaded at a later time?

Code:
local frame = CreateFrame("FRAME"); 
frame:RegisterEvent("ADDON_LOADED"); 
frame:RegisterEvent("PLAYER_LOGOUT");

function frame:OnEvent(event, arg1)    
    if event == "ADDON_LOADED" and arg1 == "MyPearls" then
        if PearlCount == nil then
            a = {};
        else
            a = PearlCount;
        end

        CurrentAmount = select(2, GetCurrencyInfo(1721));
        a[GetUnitName("player", false)] = CurrentAmount;
        print(CurrentAmount); -- to test its contents

    elseif event == "PLAYER_LOGOUT" then
        PearlCount = a; 
    end
end

frame:SetScript("OnEvent", frame.OnEvent);
SLASH_HOWMANYPEARLS1 = "/pearls";

SlashCmdList["HOWMANYPEARLS"] = function()
    for name, pCount in pairs(a) do
        print(name, " -- ", pCount)
    end
end
Thanks for any info you can provide.
  Reply With Quote
11-30-19, 08:45 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,085
Instead of ADDON_LOADED, try PLAYER_LOGIN. I assume PearlCount is your saved variable? CurrentAmount should be a local variable.
  Reply With Quote
11-30-19, 09:13 PM   #3
fail2reap
A Murloc Raider
Join Date: Nov 2019
Posts: 3
Originally Posted by myrroddin View Post
Instead of ADDON_LOADED, try PLAYER_LOGIN. I assume PearlCount is your saved variable? CurrentAmount should be a local variable.
That did the trick. Seems only after this event is fired, do the values for the player actually get loaded.

Here's the revised code.
Code:
local frame = CreateFrame("FRAME"); 
frame:RegisterEvent("ADDON_LOADED"); 
frame:RegisterEvent("PLAYER_LOGIN");
frame:RegisterEvent("PLAYER_LOGOUT");

function frame:OnEvent(event, arg1)    
    if event == "ADDON_LOADED" and arg1 == "HeyThere" then
        if PearlCount == nil then
            a = {};
        else
            a = PearlCount;
        end

    elseif event == "PLAYER_LOGIN" then
        local _, CurrentAmount = GetCurrencyInfo(1721);
        a[GetUnitName("player", false)] = CurrentAmount;

    elseif event == "PLAYER_LOGOUT" then
        PearlCount = a; 
    end
end

frame:SetScript("OnEvent", frame.OnEvent);
SLASH_HOWMANYPEARLS1 = "/pearls";

SlashCmdList["HOWMANYPEARLS"] = function()
    for name, pCount in pairs(a) do
        print(name, " -- ", pCount)
    end
end
  Reply With Quote
12-01-19, 04:03 AM   #4
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,085
You don't need ADDON_LOADED anymore. Use PLAYER_LOGIN to set up everything including your saved variable.

Added bonus: you won't need to unregister PLAYER_LOGIN as it only fires once, whereas if you stick with ADDON_LOADED you ought to unregister it after you are done setting up.

PLAYER_LOGIN > ADDON_LOADED.
  Reply With Quote
12-01-19, 08:23 AM   #5
fail2reap
A Murloc Raider
Join Date: Nov 2019
Posts: 3
Ah cool, thanks for the tip!
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » GetCurrencyInfo returns 0 CurrentAmount

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