Thread Tools Display Modes
12-09-12, 11:41 AM   #1
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 122
SavedVariablesPerCharacter

I was able to successfully create the file to save the variables of my addon.

Code:
## SavedVariablesPerCharacter: MyAddon_Settings
But if I want call these variables every time I load the addon, what should I do?
  Reply With Quote
12-09-12, 11:57 AM   #2
Tim
A Rage Talon Dragon Guard
 
Tim's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 308
This should help you.
http://www.wowpedia.org/Saving_varia..._game_sessions
__________________
AddOns: Tim @ WoWInterface
Characters: Mage, Priest, Devoker, Pally
Battle Tag: Mysterio#11164
Current PC Setup: PCPartPicker List
  Reply With Quote
12-09-12, 10:56 PM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I cleaned up that wiki page some to remove some really outdated information, so it should be relevant again now. If you read it before now, I'd suggest going back to re-read it, and then removing any references to the VARIABLES_LOADED event from your addon (use ADDON_LOADED where arg1 is the folder name of your addon instead) and probably removing PLAYER_LOGOUT (it's completely unnecessary for the vast majority of addons).

Also, if you want to use a table to hold multiple variables, here is a simple function you can use to initialize your default values. It works no matter how many levels of sub-tables are in your defaults.

Code:
-- Define your default table:
local MyDefaults = {
	enable = true,
	showTheThing = false,
	x = 47,
	foo = "bar",
	t = {
		[12] = "twelve",
		[68] = true,
		cats = "meow",
	}
}

-- This function will make sure your saved table contains all the same
-- keys as your table, and that each key's value is of the same type
-- as the value of the same key in the default table.
local function CopyDefaults(src, dst)
	if type(src) ~= "table" then return {} end
	if type(dst) ~= "table" then dst = {} end
	for k, v in pairs(src) do
		if type(v) == "table" then
			dst[k] = CopyDefaults(v, dst[k])
		elseif type(v) ~= type(dst[k]) then
			dst[k] = v
		end
	end
	return dst
end

local addon = CreateFrame("Frame", "MyAddon")
addon:RegisterEvent("ADDON_LOADED")
addon:SetScript("OnEvent", function(self, event, arg1)
	if event == "ADDON_LOADED" and arg1 == "MyAddon" then
		-- Call the function to update your saved table:
		MyAddonSavedVariables = CopyDefaults(MyDefaults, MyAddonSavedVariables)
		-- Unregister this event, since there is no further use for it:
		self:UnregisterEvent("ADDON_LOADED")
	end
end)
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
12-10-12, 06:43 PM   #4
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 122
MyAddonSavedVariables is the file specified in line ## SavedVariablesPerCharacter:?
  Reply With Quote
12-10-12, 07:26 PM   #5
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
There is no control over the actual file it writes to or where it's located, just what global variables WoW stores in it. To shorten Phanx's explanation, the SavedVariablesPerCharacter directive just tells WoW what globals to save for your addon. These globals will be available when ADDON_LOADED fires for your addon.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
12-10-12, 10:39 PM   #6
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 122
Code:
local addon = CreateFrame("Frame", "MyAddon")
addon:RegisterEvent("ADDON_LOADED")
addon:SetScript("OnEvent", function(self, event, arg1)
	if event == "ADDON_LOADED" and arg1 == "MyAddon" then
		-- Call the function to update your saved table:
		MyAddonSavedVariables = CopyDefaults(MyDefaults, MyAddonSavedVariables)
                print(MyAddonSavedVariables)
		-- Unregister this event, since there is no further use for it:
		self:UnregisterEvent("ADDON_LOADED")
	end
end)
This code print the valor (?) of MyAddonSavedVariables. But if I write the print function outside this, the code doesn't work

Code:
local addon = CreateFrame("Frame", "MyAddon")
addon:RegisterEvent("ADDON_LOADED")
addon:SetScript("OnEvent", function(self, event, arg1)
	if event == "ADDON_LOADED" and arg1 == "MyAddon" then
		-- Call the function to update your saved table:
		MyAddonSavedVariables = CopyDefaults(MyDefaults, MyAddonSavedVariables)
		-- Unregister this event, since there is no further use for it:
		self:UnregisterEvent("ADDON_LOADED")
	end
end)

print(MyAddonSavedVariables)
  Reply With Quote
12-10-12, 10:39 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Benalish View Post
MyAddonSavedVariables is the file specified in line ## SavedVariablesPerCharacter:?
Yes, though "MyAddonSavedVariables" is a variable name, not a file name, and you can specify it in either the SavedVariables field or the SavedVariablesPerCharacter field, depending on whether you want all of the user's characters to share a single copy of the variable (so changes made on one character would apply to all characters) or each have their own copy (so changes made on one would not affect any others).
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
12-11-12, 01:49 PM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Benalish View Post
... if I write the print function outside this, the code doesn't work

Code:
local addon = CreateFrame("Frame", "MyAddon")
addon:RegisterEvent("ADDON_LOADED")
addon:SetScript("OnEvent", function(self, event, arg1)
	if event == "ADDON_LOADED" and arg1 == "MyAddon" then
		-- Call the function to update your saved table:
		MyAddonSavedVariables = CopyDefaults(MyDefaults, MyAddonSavedVariables)
		-- Unregister this event, since there is no further use for it:
		self:UnregisterEvent("ADDON_LOADED")
	end
end)

print(MyAddonSavedVariables)
This is because the script you registered in addon:SetScript() hasn't run yet. Look at the function there as a callback. It only runs when the frame receives any event that is registered to it. Since it's running the callback function when ADDON_LOADED fires, it's running long after your print() call. Try loading your addon, then typing /dump MyAddonSavedVariables into chat.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 12-11-12 at 01:52 PM.
  Reply With Quote
12-11-12, 09:33 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Code outside of a function runs immediately when WoW reads your file.

Code inside of a function does not run until that function is called. In your example, the code inside the function you set as your frame's OnEvent script does not run until the game fires the ADDON_LOADED event with your addon's name as the first argument. That happens after all of your addon's files and saved variables have been read.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
12-12-12, 06:33 AM   #10
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Originally Posted by SDPhantom View Post
These globals will be available when ADDON_LOADED fires for your addon.
Is the use of:

PLAYER_LOGIN

similar ?

So I can save to check with ADDON_LOADED the arg1 ...
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
12-12-12, 06:38 AM   #11
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
I did something like this to check if the addon db was in an old format and eventually convert.

Lua Code:
  1. frame_cleu:RegisterEvent("ZONE_CHANGED_NEW_AREA")
  2. frame_cleu:RegisterEvent("PLAYER_ENTERING_WORLD")
  3. frame_cleu:RegisterEvent("PLAYER_LOGIN")
  4.  
  5. frame_cleu:SetScript("OnEvent", function(self, event, ...)
  6.  
  7.     if event == "PLAYER_LOGIN" then    
  8.        
  9.         if RemGankCFG["newdbver"] == nil then RemGank_DB_Convert() end
  10.        
  11.         if RemGankCFG["enabled"] == true then
  12.             print(string_format("%s: enabled", prgname))
  13.         else
  14.             print(string_format("%s: disabled. Type /remgank enable to start", prgname))           
  15.         end
  16.        
  17.     end
  18.  
  19.     if event == "ZONE_CHANGED_NEW_AREA" or event == "PLAYER_ENTERING_WORLD" then   
  20.  
  21.               -- blabla
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote
12-12-12, 03:01 PM   #12
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by gmarco View Post
Is the use of:

PLAYER_LOGIN

similar ?

So I can save to check with ADDON_LOADED the arg1 ...
It's not recommended to use PLAYER_LOGIN in replacement of ADDON_LOADED. For major reasons, PLAYER_LOGIN is not guaranteed to fire in all circumstances. Firing at inappropriate times or not at all is a side effect of picking a wrong and irrelevant event to register.

The purpose of PLAYER_LOGIN is to tell addons that all unit-specific data for the player is now available, like UnitName(). This fires only once and might not fire when the UI is reloaded. If an addon is LoD, it may never see this event at all.

ADDON_LOADED is specifically meant for this procedure. It signals the entire environment that an addon has finished loading and all global API and saved data provided by said addon is now available.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
12-12-12, 04:55 PM   #13
gmarco
An Onyxian Warder
 
gmarco's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2009
Posts: 362
Understood :-)
__________________
This is Unix-Land. In quiet nights, you can hear the Windows machines reboot.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » SavedVariablesPerCharacter

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