Originally Posted by Resike
Originally Posted by Phanx
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:
if not type(dst) then dst = { } 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