Thread Tools Display Modes
06-26-16, 11:59 PM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
(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?
  Reply With Quote
06-27-16, 12:06 AM   #2
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-27-16 at 12:09 AM.
  Reply With Quote
06-27-16, 12:09 AM   #3
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
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

Last edited by Layback_ : 06-27-16 at 12:18 AM.
  Reply With Quote
06-27-16, 12:24 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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)
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-27-16 at 12:33 AM.
  Reply With Quote
06-27-16, 12:35 AM   #5
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
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?
  Reply With Quote
06-27-16, 12:38 AM   #6
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,877
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.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 06-27-16 at 01:20 AM.
  Reply With Quote
06-27-16, 12:42 AM   #7
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by Fizzlemizz View Post
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
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » (About SavedVariable) What would be the problem with this code?


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