Thread Tools Display Modes
08/03/15, 08:28 PM   #1
Talen-Shei
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 6
Issue with saving

So what I wanted to accomplish was to build a table like so:

Code:
function buildatable(saveType)
    local temptable = {}
    local numitems = getNumItems() -- inconsequentially named function, 
    -- this refers to a function that simply outputs the number of different
    -- items in the array I am attempting to save the values of
    for item = 1, numitems, 1 do
        -- here I assign things to the temptable and iterate through with more for
        -- loops and more table declarations like temptable[item].subitems = {}
        -- Eventually this becomes a very large multi-dimensional array of data.

     end
     if saveType == "Global" then
         myGlobalVarReference.table = temptable -- this does not work
     else
         myPerCharacterVarReference.table = temptable -- this also does not work
     end
end
Then, as the above code suggests, get an entire table to inject into the saved vars table, For the life of me I cannot find a native function to do so (so far)

I have little time today but I thought of a decent idea to instead of passing the variable saveType to call this function from within a function and pass the reference to which table to inject this data into into. Is that a feasable option?
something like
Code:
function beforebuildatable(saveType)
    if saveType == nil then
        saveType = myPerCharacterVarReference.saveType
    end
    if saveType == "Global" then
       buildatable(myGlobalVarReference.table)  -- use global reference
    else
       buildatable(myPerCharacterVarReference.table)
    end
end

function buildatable (table) -- use same code as above but 
  -- instead build the table inside the referenced table instead of injecting
Will this work?

Note: I have really shorthanded all of this, but I believe that the basic concept can be easily read (correct me if I'm wrong) I do not have access to the real code at this time.
  Reply With Quote
08/04/15, 06:25 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,962
I don't know if I understood you right but here is some code which should work to store a deep table inside your addon's SavedVariables:

Lua Code:
  1. --Add this somewhere in your addon
  2. local yourAddon = {}
  3. --The name of the SavedVariables (the 1st key inside the SavedVariables lua file)
  4. yourAddon.SavedVariablesName = "myAddonsSavedVariablesName"
  5. --A subkey inside the SavedVariables lua file to organzie the variables. Another one could be "standards" for example and you could read your "saveMode" from the "standards" to easily switch between account wide and character wide settings -> see below
  6. yourAddon.SavedVariablesKey  = "Settings"
  7. --The subkey for the saved variables with the standard values of your addon
  8. yourAddon.SavedVariablesStandardsKey  = "Standards"
  9. --The version of your addon. Changing this will build new SavedVariables and delete old ones!!!
  10. yourAddon.addonVersion = "0.1"
  11. --Will store the standard SavedVariables of your addon
  12. yourAddon.SavedVariablesStandardTable = {}
  13. --Will store all the SavedVariables of your addon
  14. yourAddon.SavedVariablesTable = {}
  15.  
  16.  
  17. --Add this in your callback function as your addon is loaded: EVENT_ADD_ON_LOADED)
  18. --example for standard values for your Addon
  19. standards = {
  20.    saveType   = "Global",
  21. }
  22. --example for default values for your SavedVariables
  23. defaults = {
  24.    option1   = true,
  25.    option2   = false,
  26.    table = {},
  27. }
  28. --get the standard settings of your addon
  29. yourAddon.SavedVariablesStandardTable = ZO_SavedVars:NewAccountWide(yourAddon.SavedVariablesName, yourAddon.addonVersion , yourAddon.SavedVariablesStandardsKey, standards)
  30. --Check if global or local saveType is used
  31. if (yourAddon.SavedVariablesStandardTable.saveType == "Local") then
  32.     yourAddon.SavedVariablesTable = ZO_SavedVars:New(yourAddon.SavedVariablesName, yourAddon.addonVersion , yourAddon.SavedVariablesKey, defaults)
  33. elseif (yourAddon.SavedVariablesStandardTable.saveType == "Global") then
  34.     yourAddon.SavedVariablesTable = ZO_SavedVars:NewAccountWide(yourAddon.SavedVariablesName, yourAddon.addonVersion, yourAddon.SavedVariablesKey, defaults)
  35. end
  36.  
  37.  
  38.  
  39. --Now to your addons deep table that you build
  40. function buildatable(saveType)
  41.     local temptable = {}
  42.     local numitems = getNumItems() -- inconsequentially named function,
  43.     -- this refers to a function that simply outputs the number of different
  44.     -- items in the array I am attempting to save the values of
  45.     for item = 1, numitems, 1 do
  46.         -- here I assign things to the temptable and iterate through with more for
  47.         -- loops and more table declarations like temptable[item].subitems = {}
  48.         -- Eventually this becomes a very large multi-dimensional array of data.
  49.  
  50.      end
  51.  
  52.      --The table "yourAddon.SavedVariablesTable" stores all of your settings, global (account wide) and local (each character).
  53.      --The game distinguishes by using a different structure (key with @AccountName or key with CharacterName) inside the SavedVariables table itsself,
  54.      --so you do not need to use different tables to store the values for global and local saveType.
  55.      --You only need to define the ".table" array inside the SavedVariables table before and initialize it, either by using the "defaults" array
  56.      --or doing it manually by "yourAddon.SavedVariablesTable.table = {}" before you try to use it
  57.  
  58.      yourAddon.SavedVariablesTable.table = temptable -- this should work
  59. end
  Reply With Quote
08/04/15, 01:14 PM   #3
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by Talen-Shei View Post
if saveType == "Global" then
myGlobalVarReference.table = temptable -- this does not work
else
myPerCharacterVarReference.table = temptable -- this also does not work
end
It looks like Baertram gave you an example of how to use that code. I'm going to throw in a little extra info about saved vars.

I'm not sure what you are trying to accomplish.
  • Maybe you don't know how to use the saved variables?
  • Or maybe your trying to separate this data from the regular saved variables for some reason?

If you don't know how to use saved variables:
Warning: Spoiler


Either way my guess is that it is probably saving, just not where you want or your not accessing it correctly afterwords.
Warning: Spoiler


If for some reason your trying to separate this information your trying to save from the rest of the saved variable data, you could add a profile like this:
Warning: Spoiler

Last edited by circonian : 08/04/15 at 01:19 PM.
  Reply With Quote
08/05/15, 02:02 AM   #4
Talen-Shei
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 6
Strange

Seems like the way I was injecting the data should have worked by what you guys are saying. I have since used the method I talked about in my first post to pass the saved variables table as an argument to the function. It works well for me but this is some great information!

To clarify that my table wasn't injecting I did reload the ui then check my saved variables lua. To my disappointment there was no information from the table inside. After simply changing to a hard-coded build into the saved variables tables I was shooting for it did work wonderfully, populated with all of the expected data.

I'll definitely read through everything you guys replied with a few times. Glad to see there's such a helpful community here!

Also to clarify, I was simply having trouble getting that table into my saved variables.

What I wanted to end up with:

Lua Code:
  1. temptable ={
  2.                     ["Target"] = "Main",
  3.                     ["SaveType"] = "Global",
  4.                     ["SyncMode"] = "Manual",
  5.                     ["version"] = 0.1000000000,
  6.                     ["SecondDimension"] =
  7.                     {
  8.                         ["Target"] = "Main",
  9.                         ["SaveType"] = "Global",
  10.                         ["SyncMode"] = "Manual",
  11.                         ["version"] = 0.1000000000,
  12.                     },
  13.                 }
  14.  
  15. mySavedVariables.table = temptable -- Where mySavedVariables is predefined with ZO_SavedVars

acting the same as

Lua Code:
  1. mySavedVariables.table = {      -- Where mySavedVariables has been predefined using ZO_SavedVars
  2.                     ["Target"] = "Main",
  3.                     ["SaveType"] = "Global",
  4.                     ["SyncMode"] = "Manual",
  5.                     ["version"] = 0.1000000000,
  6.                     ["SecondDimension"] =
  7.                     {
  8.                         ["Target"] = "Main",
  9.                         ["SaveType"] = "Global",
  10.                         ["SyncMode"] = "Manual",
  11.                         ["version"] = 0.1000000000,
  12.                     },
  13.                 }

But instead this does nothing for me. Maybe I did something wrong. :/

Last edited by Talen-Shei : 08/05/15 at 02:58 AM.
  Reply With Quote
08/05/15, 03:00 AM   #5
Wandamey
Guest
Posts: n/a
could you post what you wrote in you addon manifest (.txt file in your folder)
and how you defined your savedvars first with ZO_SavedVars:New... please?

it'll be easier to see what went wrong.



Originally Posted by Baertram
if (yourAddon.SavedVariablesStandardTable.saveType == "Local") then
32. yourAddon.SavedVariablesTable = ZO_SavedVars:New(yourAddon.SavedVariablesName, yourAddon.addonVersion , yourAddon.SavedVariablesKey, defaults)
33.elseif (yourAddon.SavedVariablesStandardTable.saveType == "Global") then
34. yourAddon.SavedVariablesTable = ZO_SavedVars:NewAccountWide(yourAddon.SavedVariablesName, yourAddon.addonVersion, yourAddon.SavedVariablesKey, defaults)
35.end
btw wouldn't it be easier to create both global and per profile, declare both in the manifest and use them conditionally when needed? (i haven't tried that till now but i've already seen multiple savedvars in some addons)

Last edited by Wandamey : 08/05/15 at 03:54 AM.
  Reply With Quote
08/05/15, 06:33 AM   #6
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,962
I guess there are different ways to achieve the difference between account wide and cahracter based SavedVariables, yes.
The one solution I'm using is working well and performant so I didn't think about changing the "running system"
  Reply With Quote
08/05/15, 02:23 PM   #7
Talen-Shei
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 6
Originally Posted by Wandamey View Post
could you post what you wrote in you addon manifest (.txt file in your folder)
and how you defined your savedvars first with ZO_SavedVars:New... please?

it'll be easier to see what went wrong.





btw wouldn't it be easier to create both global and per profile, declare both in the manifest and use them conditionally when needed? (i haven't tried that till now but i've already seen multiple savedvars in some addons)
I have to access a per-user and account-wide table at the same time to accomplish this so I simply defined both as separate variables.

Lua Code:
  1. function InitializePlugin () --the very first function called
  2.     mySavedVariables = ZO_SavedVars:New("mySettings", 0.1, nil, defaultPerCharacterSettings, nil)
  3.     mySavedGlobalVariables = ZO_SavedVars:NewAccountWide("mySettings", 0.1, nil, defaultGlobalSettings, nil)
  4.     EVENT_MANAGER:UnregisterForEvent("myaddon", EVENT_ADD_ON_LOADED)
  5.     if mySavedVariables.SyncMode == "Automatic" then
  6.         Sync()
  7.     end
  8. end
  9.  
  10.  
  11. -- From manifest
  12. ## SavedVariables: mySettings

I don't understand why anyone would do different :P

The only thing I'm struggling with at the moment is maintaining the saved variables. I can't seem to keep them up-to-date. say I have a value x containing table {x,y,z} and to the and of that I want to append the variable a. I would do table.insert(x,a) and the value a would become the fourth value of the table x.

But let's say that table x is in my saved variables as such:

Lua Code:
  1. local a = "a"
  2. mySavedVariables.x = {x,y,z}
  3. table.insert(mySavedVariables.x,a)

on uireload the information is there, but when attempting to access through LAM2.0 it is invisible data! do I simply need to refresh the data in LAM2.0?

to clarify LAM2.0 will also recognize the new information as being a part of the table when I reload the ui and it works beautifully. It's just same session data changes that seem to never happen. LAM docs are also hard to find for some reason.

Note: I do have this on my panel

registerForRefresh = true,

but I would like to be able to manually refresh this when I change data since it seems to be unreliable in my use case.

To further clarify, I am not having trouble changing the setting but the available choices on a dropdown menu item.

Lua Code:
  1. [3] = {
  2.             type = "dropdown",
  3.             name = "Load Table",
  4.             choices = mySavedGlobalVariables.AvailTables, --this is where things go wrong
  5.             default = "Main",
  6.             getFunc = function() return mySavedVariables.Target end,
  7.             setFunc = function(bValue) mySavedVariables.Target = bValue end,
  8.         },

Last edited by Talen-Shei : 08/05/15 at 02:27 PM.
  Reply With Quote
08/05/15, 02:42 PM   #8
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Reading the other posts I now realize although I offered information, I didn't actually offer a solution:

Warning: Spoiler


One other thing to note is that in these:
Lua Code:
  1. characterSpecificSavedVars = ZO_SavedVars:New(savedVariableTable, version , nil, defaults)
  2. AccountWideSavedVars = ZO_SavedVars:NewAccountWide(savedVariableTable, version, nil, defaults)
You will most likely will NOT want to use your addon version number. The reason is because every time you increase the version number it would wipe out the saved variable table (it does that automatically when the version number increases). I usually define two version numbers in my addon like:
Lua Code:
  1. local CODE_VERSION = 2.3
  2. local SAVED_VAR_VERSION = 1.2
Then just use them each wherever appropriate.
Lua Code:
  1. characterSpecificSavedVars = ZO_SavedVars:New(savedVariableTable, SAVED_VAR_VERSION, nil, defaults)
  2. AccountWideSavedVars = ZO_SavedVars:NewAccountWide(savedVariableTable, SAVED_VAR_VERSION, nil, defaults)

As the addon version number increases I just change the CODE_VERSION. If the saved variables need to be wiped due to addon changes, then increase the SAVED_VAR_VERSION...and don't define them right next to each other (one line right after another), you'll end up accidently changing the wrong one by mistake and wiping everyone's saved vars
  Reply With Quote
08/05/15, 05:41 PM   #9
Talen-Shei
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 6
Thanks!

Thanks for the tip circonian, I'm actually using the version number currently to quickly wipe my saved data to start from scratch during testing to ensure that everything is working as expected and nothing has been broken.

Also the change that you suggested is really just a rename. As you can see from the post prior I actually did declare those in my initialization function, again, for some reason it just didn't work for me.
  Reply With Quote
08/05/15, 06:27 PM   #10
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,578
Originally Posted by Talen-Shei View Post
on uireload the information is there, but when attempting to access through LAM2.0 it is invisible data! do I simply need to refresh the data in LAM2.0?

to clarify LAM2.0 will also recognize the new information as being a part of the table when I reload the ui and it works beautifully. It's just same session data changes that seem to never happen. LAM docs are also hard to find for some reason.

Note: I do have this on my panel

registerForRefresh = true,

but I would like to be able to manually refresh this when I change data since it seems to be unreliable in my use case.

To further clarify, I am not having trouble changing the setting but the available choices on a dropdown menu item.
The dropdown menu won't automagically know when you change the table. You need to tell it that there has been an update.
You can add a reference name to the options table and then call the UpdateChoices method.

Lua Code:
  1. [3] = {
  2.                 type = "dropdown",
  3.                 name = "Load Table",
  4.                 reference = "MyDropdown",
  5.                 choices = mySavedGlobalVariables.AvailTables, --this is where things go wrong
  6.                 default = "Main",
  7.                 getFunc = function() return mySavedVariables.Target end,
  8.                 setFunc = function(bValue) mySavedVariables.Target = bValue end,
  9.             },

Lua Code:
  1. MyDropdown:UpdateChoices()
  Reply With Quote
08/05/15, 06:41 PM   #11
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by Talen-Shei View Post
As you can see from the post prior I actually did declare those in my initialization function, again, for some reason it just didn't work for me.
Ah, I was probably in the middle of typing all of that up when you posted that. I didn't see it.
  Reply With Quote
08/05/15, 06:58 PM   #12
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
To add to what sirinsidiator said:
Lua Code:
  1. You can assign the choices multiple ways:
  2. -- With a function that returns the table of choices:
  3. choices = GetAvailableAdvancedFilters(),
  4.  
  5. -- the table itself, saved in saved variables:
  6. choices = JunkIt.SavedVariables["WHITELIST"],
  7.  
  8. -- Or define the table itself, right there:
  9. choices = {"Off", "Equipped", "All"},

Make sure you realize that if you call:
Lua Code:
  1. MyDropdown:UpdateChoices()
Which in the case of the code you posted looks like what you want, but just fyi ... That will re-initialize the choices from whatever/wherever/however you defined them. Since you defined yours as a reference to the table saved in saved vars (which your changing) it will regrab that table & use whatever is in it at that time.
But if you had defined the table like this:
Lua Code:
  1. choices = {"Off", "Equipped", "All"},
  2. -- then calling this
  3. MyDropdown:UpdateChoices()
Wouldn't do anything, it would just re-initialize the same choices.

If you want to change the choices used (instead of using whatever you defined with choices = ....) you can pass a table to the UpdateChoices function. For example if I had two different tables of choices & depending upon some other setting I want to show different choices in the dropdown. You could initialize it with:
Lua Code:
  1. choices = JunkIt.SavedVariables["WHITELIST"],
  2.  
  3. -- And instead of changing the data in that table
  4. -- Maybe I just want to use a different table of data,
  5. -- so I could just pass in a different table
  6. JUNKIT_WHITELIST_DROPDOWN:UpdateChoices(JunkIt.SavedVariables["A_DIFFERENT_WHITELIST"])

Last edited by circonian : 08/05/15 at 07:02 PM.
  Reply With Quote
08/06/15, 03:14 AM   #13
Talen-Shei
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 6
Originally Posted by circonian View Post
Ah, I was probably in the middle of typing all of that up when you posted that. I didn't see it.
That's okay, you've been extremely helpful and a great wealth of knowledge to have around

@sirinsidiator, Thank you so much! And while I am at it I must say, why is this not in the examples?! I guess I would have realized it if I actually looked at the comment block at the top of LAM's dropdown.lua
>.>

dropdown.lua:
Warning: Spoiler



And thanks yet again Circonian for the details on how this works!
  Reply With Quote
08/06/15, 03:47 AM   #14
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,578
That's because I haven't yet had the time or motivation to improve the documentation since I have taken over LAM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Issue with saving

Thread Tools
Display Modes

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