02-29-12, 11:23 AM | #1 |
Traversing Nested Tables
I really don't know where to start in trying to explain this...
I'm writing a module for my UI to help assist me in exporting my saved variables for the various addons I use in my private compilation. I plan to include this data in my UI and overwrite the addons' default saved variables upon installation. However, some of the addons' saved variables house information that I don't want included. I could copy the data I want manually, but I'm looking for a more automated solution. As you'll see in my code below, I have a table populated with the names of the addons and their corresponding global variable names. For addons where I don't want to copy everything, I've listed specific table branching to the data I do want. TL;DR: I can't wrap my head around how to traverse nested tables while also keeping track of the current path in relation to the global variable that it needs to be copied from. I hope this makes sense to someone. lua Code:
|
|
02-29-12, 04:50 PM | #2 |
So what I'm assuming you're trying to do is not have your users copy any saved variables and just have this addon 'store' all the data in Lua and be able to update that data easily?
|
|
02-29-12, 06:11 PM | #3 |
Yes, that is what I said already.
|
|
02-29-12, 07:34 PM | #4 |
I'd use a slightly different approach; rather than trying to generalize everything to the nth degree, I'd just write individual functions for the "special case" addons:
Code:
local SavedVariables = { ["_NPCScan.Overlay"] = { ["_NPCScanOverlayOptions"] = true, }, ["Altoholic"] = { ["AltoholicDB"] = function(t) return { ["global"] = { ["options"] = { ["ShowMinimap"] = t.global.options.ShowMinimap, } } } end, }, } local function copyTable(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] = copyTable(v, dst[k]) elseif type(v) ~= type(dst[k]) then dst[k] = v end end for k, v in pairs(dst) do if type(src[k]) == nil then -- "not src[k]" won't work as desired for values of "false" dst[k] = nil end end end ExportedSV = {} function ExportSV() for addon, svars in pairs(SavedVariables) do ExportedSV[addon] = ExportedSV[addon] or {} for name, data in pairs(svars) do local svar = _G[name] if svar then if type(data) == "boolean" then -- we want the whole thing if type(svar) == "table" then ExportedSV[addon][name] = copyTable(data) else -- not a table, just copy the value ExportedSV[addon][name] = svar end elseif type(data) == "function" then -- we only want part of it ExportedSV[addon][name] = data(svar) end end end end end Also, I made a small change in the "copy this table" function; see the in-code comment. |
|
02-29-12, 09:01 PM | #5 |
Phanx,
Thank you for your suggestion and the provided code example. I suppose I was just making things more complicated than they needed to be. |
|
03-01-12, 03:36 PM | #6 |
On second thought, I'd define the "copy this table and all its sub-tables" function before you define the list of SavedVariables to copy. That way, you can use the copy function inside your custom SavedVariable functions; eg. if you want to copy the whole "AddonDB.global" node.
|
|
WoWInterface » Developer Discussions » Lua/XML Help » Traversing Nested Tables |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|