Thread: Saved Variables
View Single Post
12-12-15, 12:20 PM   #9
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,327
Originally Posted by Resike View Post
Originally Posted by Phanx View Post
Code:
	-- This function copies values from one table into another:
	local function copyDefaults(src, dst)
		-- If no source (defaults) is specified, return an empty table:
		if type(src) ~= "table" then return {} end
		-- If no target (saved variable) is specified, create a new table:
		if type(dst) then dst = {} end
		-- Loop through the source (defaults):
		for k, v in pairs(src) do
			-- If the value is a sub-table:
			if type(v) == "table" then
				-- Recursively call the function:
				dst[k] = copyDefaults(v, dst[k])
			-- Or if the default value type doesn't match the existing value type:
			elseif type(v) ~= type(dst[k]) then
				-- Overwrite the existing value with the default one:
				dst[k] = v
			end
		end
		-- Return the destination table:
		return dst
	end
I know this thread is pretty old, but i would like to correct a major error with Phanx'es code, which is probably just a typo:

Lua Code:
  1. if not type(dst) then
  2.     dst = { }
  3. end

If the not is not there it will auto reset your settings to the default one every time you log in.
type() always returns a string no matter what you give it. This means using it directly as a conditional will always return true and using not on it will always return false. The correction on this should be the following.
Code:
if type(dst) ~= "table" then dst = { } end
__________________
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