Thread Tools Display Modes
10/04/14, 11:48 PM   #1
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Copying Saved Variables

I'm trying to create savable profiles for all of my addons settings. I've gotten everything to work, with one small problem. The table changes when I reload the UI. It might be easiest if I just explain what I did.

A user pushes a button which runs some code & I attempted to copy the saved variable table into another table so it could be reloaded later.

I tried to create a copy of the table using (spoiler is code used to copy the table):
Warning: Spoiler

and I used the following code to save the profile:
Lua Code:
  1. local tSavedVars = JunkIt.deepcopy(JunkIt.SavedVariables)
  2. table.insert(JunkIt.AccountSavedVariables["PROFILES"]["PROFILE"], {_sProfileName, tSavedVars})
That created a copy and it worked just fine, until I reloaded the UI.

Heres what the tables (saved profiles) look like before & after I reload the UI.
Profile 2 on the left shows what it looks like after I create a new profile, before I reload the UI.
Profile 1 on the right shows what it looks like after I reload the UI.


Before reloading the UI I could reload all of the saved variables by doing:
Lua Code:
  1. for k,v in pairs(JunkIt.AccountSavedVariables["PROFILES"]["PROFILE"]) do
  2.       if v[1] == _sProfileName then
  3.             JunkIt.SavedVariables = v[2]
  4.       end
  5. end

EDIT:

So I figured out the table is not adding an extra ["default"] level. Duh that is my default saved variables table. The problem is that everything else is disappearing when I reload the UI. Everything that has that (m) in front of it. I'm assuming that has something to do with metamethods & metatables, which I don't really understand very well. Does this have something to do with it?.

I found another post suggesting to use ZO_DeepTableCopy:
Lua Code:
  1. local tSavedVars = ZO_DeepTableCopy(JunkIt.SavedVariables)
  2. table.insert(JunkIt.AccountSavedVariables["PROFILES"]["PROFILE"], {_sProfileName, tSavedVars})
But ZO_DeepTableCopy doesn't copy metatables. That only copied the ["default"] table and GetInterfaceForCharacter() function. It did not copy the current settings, see picture below:



Is there an easier way to accomplish what I'm trying to do?

Last edited by circonian : 10/06/14 at 02:30 AM.
  Reply With Quote
10/06/14, 02:26 AM   #2
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
I'm answering my own question in case anyone else is interested, I managed to figure out how to get this to work.

Code to backup/save settings into a table:
Lua Code:
  1. local sDisplayName = GetDisplayName()
  2. local sUnitName = GetUnitName("player")
  3.    
  4. local tSavedVars = ZO_DeepTableCopy(_G["AddonSavedVarsName"]["Default"][sDisplayName][sUnitName])

Code to restore/load settings from table back into saved vars:
Lua Code:
  1. local sDisplayName = GetDisplayName()
  2. local sUnitName = GetUnitName("player")
  3.  
  4. ZO_DeepTableCopy(tBackupSettingsTable, _G["AddonSavedVarsName"]["Default"][sDisplayName][sUnitName])

Where AddonSavedVarsName is the name you gave your saved variables in your Addon.txt file:
Lua Code:
  1. ## SavedVariables: JunkItSavedVars

ZO_DeepTableCopy does not copy metatables, but I was apparently copying the wrong table before. The table were copying now (in the above code) does not contain metatables so we can use ZO_DeepTableCopy. It can be used as:
Lua Code:
  1. tDestination = ZO_DeepTableCopy(tSource)
  2. -- or
  3. ZO_DeepTableCopy(tSource, tDestination)

Last edited by circonian : 10/06/14 at 02:45 AM.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Copying Saved Variables


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