WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   GetCurrencyInfo returns 0 CurrentAmount (https://www.wowinterface.com/forums/showthread.php?t=57712)

fail2reap 11-30-19 07:54 PM

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.

myrroddin 11-30-19 08:45 PM

Instead of ADDON_LOADED, try PLAYER_LOGIN. I assume PearlCount is your saved variable? CurrentAmount should be a local variable.

fail2reap 11-30-19 09:13 PM

Quote:

Originally Posted by myrroddin (Post 334680)
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


myrroddin 12-01-19 04:03 AM

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.

fail2reap 12-01-19 08:23 AM

Ah cool, thanks for the tip!


All times are GMT -6. The time now is 06:38 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI