Thread Tools Display Modes
12-09-12, 11:41 AM   #1
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
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: 309
This should help you.
http://www.wowpedia.org/Saving_varia..._game_sessions
__________________
AddOns: Tim @ WoWInterface
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: 123
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,326
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: 123
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-12-12, 06:33 AM   #7
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-10-12, 10:39 PM   #8
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

WoWInterface » Developer Discussions » Lua/XML Help » SavedVariablesPerCharacter


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