View Single Post
09-16-05, 01:40 PM   #8
Iriel
Super Moderator
WoWInterface Super Mod
Featured
Join Date: Jun 2005
Posts: 578
Originally Posted by Kaelten
as in preset options?

hmm it may be prefereable to attempt to write a script that executes once on load of a new version and sets all those options.

You can actually put the code from the SV file into a function.
The 'cleanest' solution I can think of, which is possibly semi-nasty 8-) is to create your own addon, i'll call it XYZInit, with the following:

1) A saved variable of its own called ''XYZInit_Applied"
1) A frame which registers for ADDON_LOADED and VARIABLES_LOADED
2) A lua file containing code that does the following..

1) Initialize a big table, more or less derived from your saved variables, something like:

Code:
local compilationInit = {
  ["FirstAddon"] = {
    ["FirstAddonVariable"] = 123,
    ["FirstAddonOtherVariable"] = "Hello",
  }
}
(Essentially those sub-tables are the data from your saved variables file(s), in table form)

Now in your event handler you need something like:

Code:
if (event == "ADDON_LOADED") then
  if (arg1 = "XYZInit") then
    if (not XYZInit_Applied) then
      XYZInit_Applied = {};
    end
    return;
  end
  -- Skip those that are already initialized
  if (XYZInit_Applied[arg1]) then
    return; 
  end
  -- Skip those which are not in the compilation
  local data = compilationInit[arg1];
  if (not data) then
    return;
  end
  -- Apply variables before addon gets to see them
  for k,v in pairs(data) do
    setglobal(k, v);
  end
  XYZInit_Applied[arg1] = 1;
  return;
end

if (event == "VARIABLES_LOADED") then
  -- Loading is done, clear the array so it can be cleaned up
  compilationInit = {};
end
Finally, and this is very important, you need to make XYZInit an optional dependency of all of the addons for whihc it has defaults.. That ensures it gets loaded first, and thus gets ADDON_LOADED before the addon does, giving it time to put the saved variables into place.
  Reply With Quote