View Single Post
05/26/14, 10:46 AM   #11
Aicam
Join Date: Apr 2014
Posts: 16
the real problem traversing the SavedVars is that the actual data is stored in a metatable.
i use the following (rather long) snipped to auto generate default get/set functions for my SavedVars.
Lua Code:
  1. local function Factory( input, output, pre )
  2.   if pre ~= "" then
  3.     pre = pre .. "."
  4.   end
  5.  
  6.   for k, v in pairs( input ) do
  7.     if k ~= "version" and type( v ) ~= "function" then
  8.       output[ k ] = {}
  9.       if type( v ) == "table" then
  10.         Factory( v, output[ k ] , pre..k )
  11.       else
  12.         output[ k ][ "get" ] = function()
  13.           return input[ k ]
  14.         end
  15.  
  16.         output[ k ][ "set" ] = function( new )
  17.           local old = input[ k ]
  18.           input[ k ] = new
  19.           CM:FireCallbacks( "VariableChanged", Addon.id, pre..k, old, new )
  20.         end
  21.  
  22.       end
  23.     end
  24.   end
  25. end
  26.  
  27. local SavedVars, Accessors = ZO_SavedVars:New( Addon.id, Addon.SavedVarsVersion, nil, Addon.Defaults, nil ), {}
  28.  
  29. if t and type( SavedVars ) == "table" then
  30.   local meta = getmetatable( SavedVars )
  31.   if meta and meta.__index and type( meta.__index ) == "table" then
  32.     Factory( meta.__index, Accessors, "" )
  33.   end
  34. end

Last edited by Aicam : 05/26/14 at 11:12 AM. Reason: variable renaming in code snippet
  Reply With Quote