Quantcast WoWInterface - View Single Post - SavedVariablesPerCharacter
View Single Post
12-09-12, 10:56 PM   #3
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.

-- 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
	return dst

local addon = CreateFrame("Frame", "MyAddon")
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:
Author/maintainer of Grid, PhanxChat, oUF_Phanx, and many more.
Troubleshoot an addonTurn any code into an addonMore addon resources
Need help with your code? Post all of your actual code! Attach or paste your files.
Please don’t PM me about addon bugs or code questions. Post a comment or forum thread instead!
  Reply With Quote