WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   (About SavedVariable) What would be the problem with this code? (https://www.wowinterface.com/forums/showthread.php?t=53784)

Layback_ 06-26-16 11:59 PM

(About SavedVariable) What would be the problem with this code?
 
Hi fellow forum users,

I was trying to create simple resource tracker addon using 'SavedVariables' and here's what I have done so far.

Code:

local ResourceTrackFrame = CreateFrame("Frame");
ResourceTrackFrame:RegisterEvent("PLAYER_LOGOUT");

local function ResourceTrackFrame_OnEvent(event, arg1)
        if event == "PLAYER_LOGOUT" then

                realm = GetRealmName();
                name = UnitName("player");
                _, englishClass = UnitClass("player");
                _, currentAmount = GetCurrencyInfo(824);
               
                if type(Resource) ~= "table" then
                        Resource = {};
                end

                if type(Resource[realm]) ~= "table" then
                        Resource[realm] = {};
                end

                if type(Resource[realm][name]) ~= "table" then
                        Resource[realm][name] = {};
                end

                Resource[realm][name]["Class"] = englishClass;
                Resource[realm][name]["GarrisonResource"] = currentAmount;
        end
end

ResourceTrackFrame:SetScript("OnEvent", ResourceTrackFrame_OnEvent);

The current problem is that the value that is being saved are always nil which I honestly don't get why.

According to wikia, it says that "Variables are saved and loaded in the global environment". Would that be something related to this since I declared frame and function as local?

Fizzlemizz 06-27-16 12:06 AM

Did you list
## SavedVariables: Resource

in your .toc file? The game will create Resource as the global to set your table to and save it when you logout.

Layback_ 06-27-16 12:09 AM

Quote:

Originally Posted by Fizzlemizz (Post 316008)
Did you list
## SavedVariables: Resource

in your .toc file? The game will create Resource as the global to set your table to and save it when you logout.

Yeap, definitely did it! But it doesn't seem to be working :(

Fizzlemizz 06-27-16 12:24 AM

If you have/want "PLAYER_LOGOUT" as the only registered event, get rid of

Code:

if event == "PLAYER_LOGOUT" then
and it's corresponding end.

Otherwise change the function prototype to:
Code:

local function ResourceTrackFrame_OnEvent(self, event, arg1)

Layback_ 06-27-16 12:35 AM

Quote:

Originally Posted by Fizzlemizz (Post 316010)
If you have/want "PLAYER_LOGOUT" as the only registered event, get rid of

Code:

if event == "PLAYER_LOGOUT" then
and it's corresponding end.

Otherwise change the function prototype to:
Code:

local function ResourceTrackFrame_OnEvent(self, event, arg1)

Wow... adding "self" to parameter lists did do the trick!

Code:

local ResourceTrackFrame = CreateFrame("Frame");
ResourceTrackFrame:RegisterEvent("PLAYER_LOGOUT");

local function ResourceTrackFrame_OnEvent(self, event, arg1)
        if event == "PLAYER_LOGOUT" then
                realm = GetRealmName();
                name = UnitName("player");
                _, englishClass = UnitClass("player");
                _, currentAmount = GetCurrencyInfo(824);
       
                if type(Resource) ~= "table" then
                        Resource = {};
                end
                if type(Resource[realm]) ~= "table" then
                        Resource[realm] = {};
                end
                if type(Resource[realm][name]) ~= "table" then
                        Resource[realm][name] = {};
                end
                Resource[realm][name]["Class"] = englishClass;
                Resource[realm][name]["GarrisonResource"] = currentAmount;
        end
end

ResourceTrackFrame:SetScript("OnEvent", ResourceTrackFrame_OnEvent);

Do you know what was causing the problem?

Fizzlemizz 06-27-16 12:38 AM

Each parameter has to be getting what you expect. In this case, the event parameter was getting passed self (in this case the ResourceTrackFrame table) and the event landed in arg1.

Add "PLAYER_ENTERING_WORLD" to the registered events and
Code:

local function ResourceTrackFrame_OnEvent(self, event, arg1)
print(self, event, arg1)

reload the ui.

What you name the paramters doesn't matter, it is what they are passed that counts.
Code:

local function ResourceTrackFrame_OnEvent(amanda, marge, sue)
print(amanda, marge, sue)

reload the ui.

Layback_ 06-27-16 12:42 AM

Quote:

Originally Posted by Fizzlemizz (Post 316012)
Each parameter has to be getting what you expect. In this case, the event parameter was getting passed self (in this case the ResourceTrackFrame table) and the event landed in arg1.

Add "PLAYER_ENTERING_WORLD" to the registered events and
Code:

local function ResourceTrackFrame_OnEvent(self, event, arg1)
print(self, event, arg1)

reload the ui.

oh.............. you are totally right..........

I was so dumb D:

Thank you for your correction :D


All times are GMT -6. The time now is 05:03 AM.

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