Thread Tools Display Modes
08-07-14, 08:33 AM   #1
CrazyCactuaR
A Deviate Faerie Dragon
Join Date: Oct 2011
Posts: 13
How do i correctly load saved varibles?

Hi WOWInterface, I've come begging for help as this one is causing me quite the grief.

So I've got myself a table full of varibles (tblMySettings), the addon saves this table using #SavedVaribles: tblMySettings and seem's to load what is saved fine.

The downside is, while still writing this addon, adding variables to the table the obvious problem is it does not load the newly added variables unless i manually set the value myself. Also obviously if somebody else was using the addon and i gave it them as a update, they also would not receive the new varible values unless they deleted my addon's savedvaribles files from the WTF folder.

I did manage to stumble across this;

Code:
function CopyDefaults(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] = CopyDefaults(v, dst[k])
		elseif type(v) ~= type(dst[k]) then
			dst[k] = v
		end
	end
	return dst
end
However truth be told I really am not sure how to correctly use it on ADDON_LOADED to load any varibles that was saved but also load what hasn't been saved.

A BIG thank you to whoever helps regarding this.
  Reply With Quote
08-07-14, 08:59 AM   #2
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 102
You would have a "defaults" table defined in your AddOn that you then pass to CopyDefaults in ADDON_LOADED, so something like the following:

lua Code:
  1. local defaults = {
  2.   foo = "hello world",
  3.   bar = { key = "value" },
  4.   anumber = 42
  5. }
  6.  
  7. local function addonLoaded(name) -- replace this with whatever you are using to handle ADDON_LOADED
  8.   if name ~= myAddonName then return end
  9.   -- Also assign it in case tblMySettings is nil
  10.   tblMySettings = CopyDefaults(defaults, tblMySettings)
  11. end

The CopyDefaults function you have won't replace values in the saved variables that are already of the same type as the corresponding key in the defaults table, so it will leave existing entries intact.
  Reply With Quote
08-07-14, 09:37 AM   #3
CrazyCactuaR
A Deviate Faerie Dragon
Join Date: Oct 2011
Posts: 13
Originally Posted by Sharparam View Post
You would have a "defaults" table defined in your AddOn that you then pass to CopyDefaults in ADDON_LOADED, so something like the following:

lua Code:
  1. local defaults = {
  2.   foo = "hello world",
  3.   bar = { key = "value" },
  4.   anumber = 42
  5. }
  6.  
  7. local function addonLoaded(name) -- replace this with whatever you are using to handle ADDON_LOADED
  8.   if name ~= myAddonName then return end
  9.   -- Also assign it in case tblMySettings is nil
  10.   tblMySettings = CopyDefaults(defaults, tblMySettings)
  11. end

The CopyDefaults function you have won't replace values in the saved variables that are already of the same type as the corresponding key in the defaults table, so it will leave existing entries intact.
Thank you!

From trying this i can confirm that it does copy the default settings to another table, However a problem still exists.

Code:
default = {
one="hello",
two="bye",
}
profile = { }
default will move to profile.

If i were to add; three="meep" to default by editing the file and saving, after reloading wow or the ui it does not copy "three" to the profile table.
  Reply With Quote
08-07-14, 10:49 AM   #4
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 102
Can you post the complete code (including TOC)?
  Reply With Quote
08-07-14, 11:04 AM   #5
CrazyCactuaR
A Deviate Faerie Dragon
Join Date: Oct 2011
Posts: 13
Originally Posted by Sharparam View Post
Can you post the complete code (including TOC)?
myAddon.toc
Code:
## Interface: 50401
## Title: myAddon
## Author: CrazyCactuaR
## DefaultState: Enabled
## SavedVariables: mySettings
myAddon.lua
myAddon.lua
Code:
myDefault = {
	one="hey",
	two="hoo",
}

mySettings = { }

function CopyDefaults(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] = CopyDefaults(v, dst[k])
		elseif type(v) ~= type(dst[k]) then
			dst[k] = v
		end
	end
	return dst
end

function myAddon_OnLoad()
	mySettings = CopyDefaults(myDefault, mySettings)
end

myAddon_OnLoad()
If i did;
Code:
/script print(mySettings.one)
It would return "hey". So i decide here is where i edit myDefault, add three="meep", save, reloadui on wow and try the print of mySettings.three, but this returns nil.

Only way I could get it to copy three to mySettings was to delete myAddon.lua and myAddon.lua.bak from SavedVariables in WTF.

The obvious problem I see with this is if my initial addon I make is given out and people use it, then i continue it's development even further, anymore variables to that table i include will not appear to those who currently use my addon only to those who have not used it yet.
  Reply With Quote
08-07-14, 11:25 AM   #6
Sharparam
A Flamescale Wyrmkin
 
Sharparam's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2011
Posts: 102
The issue is most likely that you're running the myAddon_OnLoad function at the end of the file, rather than when your addon has actually loaded. So the Saved Variables have not been initialized at the time of calling the function.

You'll need to listen for the ADDON_LOADED event and run the function when your addon has loaded (by checking the name arg).

lua Code:
  1. local frame = CreateFrame("Frame")
  2.  
  3. -- Normally you'd use vararg (...) for the event args, but we're only listening for one specific event
  4. local function onEvent(self, event, name)
  5.   if name ~= "myAddon" then return end -- don't process event if it wasn't our addon that loaded
  6.   mySettings = CopyDefaults(myDefaults, mySettings)
  7.   self:UnregisterEvent("ADDON_LOADED") -- don't process further loaded events
  8. end
  9.  
  10. frame:SetScript("OnEvent", onEvent)
  11. frame:RegisterEvent("ADDON_LOADED")

Also, don't pollute the global namespace with names like "myDefaults", "CopyDefaults" etc, they are very generic names and there could be conflicts with other addons that use names like that. If you really need them to be global, prefix them with something like "myAddon_" or put them in a global table named like your addon.
  Reply With Quote
08-07-14, 12:17 PM   #7
CrazyCactuaR
A Deviate Faerie Dragon
Join Date: Oct 2011
Posts: 13
Originally Posted by Sharparam View Post
The issue is most likely that you're running the myAddon_OnLoad function at the end of the file, rather than when your addon has actually loaded. So the Saved Variables have not been initialized at the time of calling the function.

You'll need to listen for the ADDON_LOADED event and run the function when your addon has loaded (by checking the name arg).

lua Code:
  1. local frame = CreateFrame("Frame")
  2.  
  3. -- Normally you'd use vararg (...) for the event args, but we're only listening for one specific event
  4. local function onEvent(self, event, name)
  5.   if name ~= "myAddon" then return end -- don't process event if it wasn't our addon that loaded
  6.   mySettings = CopyDefaults(myDefaults, mySettings)
  7.   self:UnregisterEvent("ADDON_LOADED") -- don't process further loaded events
  8. end
  9.  
  10. frame:SetScript("OnEvent", onEvent)
  11. frame:RegisterEvent("ADDON_LOADED")

Also, don't pollute the global namespace with names like "myDefaults", "CopyDefaults" etc, they are very generic names and there could be conflicts with other addons that use names like that. If you really need them to be global, prefix them with something like "myAddon_" or put them in a global table named like your addon.
Yup you are spot on pal, Thank you very much for that.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How do i correctly load saved varibles?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off