Thread Tools Display Modes
06/19/23, 09:42 AM   #1
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 265
Question General saved variable questions

Hiya I have an addon with account wide saved variables but I'm looking to save 1 specific option as character specific.

Ive been trying different ways to format "the saving of" the variable to turn it into a sort of named table for each character that would save an ID number that would look something like this:
Code:
MyAddonVars =
{
    ["Default"] = 
    {
        ["@MyUsername"] = 
        {
            ["$AccountWide"] = 
            {
                ["savedVariable"] = something,--<<<< the normal account wide variables
                ["savedVariable"] = something,
                ["savedVariable"] = something,
                ["savedVariable"] = something,
                ["charVariables"] = character1 = 10,--<<< table for each char saving a skill ID i need
                                    character2 = 12,
                                    character3 = 14,
                                    character4 = 16,

I suppose the format below would be ok as well incase I wanted to save more to each character later..

{
    ["Default"] = 
    {
        ["@MyUsername"] = 
        {
            ["$AccountWide"] = 
            {
                ["savedVariable"] = something,--<<<< the normal account wide variables
                ["savedVariable"] = something,
                ["savedVariable"] = something,
                ["savedVariable"] = something,
                ["character1"] = skillId = 10,--<<< this style would also work
                ["character2"] = skillId = 12,
                ["character3"] = skillId = 14,
                ["character4"] = skillId = 16,
is there a way to format "the saving of" my variables to save & access that table that actually works? Currently using "Myaddon.savedVariables.XXXXXX to save the upper ones listed there. Like the equivalent of using something like:
Code:
Myaddon.savedVariables.[charVariables].GetUnitName("player") = 10
or
Myaddon.savedVariables.GetUnitName("player").skillId = 10
or even
Myaddon.savedVariables.GetUnitName("player") = 10
OR

is it possible to define 2 sets of variables in one file like account wide AND character name specific with these? Id prefer option 1 though if possible.
Code:
MyAddon.savedVariables = ZO_SavedVars:NewAccountWide( MyAddon.svName, MyAddon.svVersion, nil, defaultSavedVars )
I think I know its possible and how to create a second saved variable file that character specific but prefer that as a last option

Last edited by sinnereso : 06/19/23 at 11:00 AM.
  Reply With Quote
06/19/23, 12:47 PM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,077
>Check Edit3, should be the easiest way!


First answer:
Lua Code:
  1. MyAddon.savedVariables.yourTableToSaveCharacterIdDataIn[characterIdOfYourToon] = {
  2.  [key1] = value1,
  3.  [key2] = value2,
  4. }


yourTableToSaveCharacterIdDataIn must be in your SVs defaults' table as empty table "defaultSavedVars" ! Else you will get an nil error.

Where "characterIdOfYourToon" is the unique chracterId of the character you want to save the data for.
You can get all characterIds of your account's characters as shown iny our other thread, where you wanted to know how to get the character names and ids of all toons of your account.

See GetNumCharacters() and then loop from 1 to GetNumCharacters() and use GetCharacterInfo() to get the uniqueIds of each character at that index in the loop.
And GetCurrentCharacterId() for the currently loged in characterId if you only need that.


GetUnitName should not be used if this is about your current character as names can change! You should especially not use that names and strings in savedvars! Use the IDs which never change, as mentioned many times now.


Edit
btw this is completely wrong syntax

Myaddon.savedVariables.[charVariables].GetUnitName("player")

1st Myaddon.savedVariables.[charVariables] cannot work. Either . or [key]!
So either Myaddon.savedVariables[charVariables] or Myaddon.savedVariables.charVariables, but the latter does not work if charVariables is a variable, you need to use the [] then around it. The . syntax only works if the key of your table is a string, e.g.
Myaddon.savedVariables.myTable1 is the same as Myaddon.savedVariables["myTable1"]

2nd Myaddon.savedVariables.[charVariables].GetUnitName("player") won't accept any API function CALLS directly fter a . !
You also need to put that into [] as the API function GetUnitName("player") returns a string!
so: Myaddon.savedVariables[charVariables][GetUnitName("player")]

and keep in mind that Myaddon.savedVariables[charVariables] mustbe defined as {} empty table somwhere to make it "exist", else you cannot fill it with [GetUnitName("player")] and you'd get a "try to index a nil value" -> means you try to put something inside a table but the table is nil (does not exist). index always points you to a table type!


Edit2:
In your txt manifest you have defined a ## SavedVariables table, e.g. MyAddonsSV. This is a global variable which is accessible.
You can even change that directly without using ZO_SavedVars wrapper to reference it to MyAddon.savedVariables.
So you could also change MyAddonsSV[GetDisplayName()[GetCurrentCharacterId()] = {
subtable1 = {
[key] = value,
}
}

and read it from there too


BUT: As you already use a ZO_SsavedVars wrapper keep using it and just update the reference MyAddon.savedVariables then with your subtable for the character dependent data.


Edit3:
You can also specify another 3rd parameter "profile" at the ZO_SavedVars wrapper:
1st account wide SVs
MyAddon.savedVariables = ZO_SavedVars:NewAccountWide( MyAddon.svName, MyAddon.svVersion, nil, defaultSavedVars )


2nd account wide character saved data:
MyAddon.savedVariablesForCharacters = ZO_SavedVars:NewAccountWide( MyAddon.svName, MyAddon.svVersion, "CharacterData", defaultSavedVars )

Within your global SavedVariables table there will be created a subtable CharacterData then and you reference it via
MyAddon.savedVariablesForCharacters instead of MyAddon.savedVariables then

Last edited by Baertram : 06/19/23 at 01:31 PM.
  Reply With Quote
06/19/23, 06:18 PM   #3
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 265
these are litereally only temporary saved variables for use on each character that has has initiated the auto meticulous cp swap but not completed it before a reload or relog... once it completes on the correct character i intend to set the variable to nil which is why id like to avoid making a new file.

Im just trying to cover 4th base of thing that could happen.
  Reply With Quote
06/19/23, 08:30 PM   #4
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 265
THANK YOU BEAR!!! luvulongtime! that helped me soo much! I went with:

Code:
MyAddon.charVariables = ZO_SavedVars:NewAccountWide( MyAddon.svName, MyAddon.svVersion, GetUnitName("player"), nil )

and

MyAddon.charVariables.autometSkill = something
and appears to be working perfectly.. I literally only had to add the top line and change the other variable references and BAM working perfectly but currently in further testing by others with more alts. I only ever made 1 toon. Im templar4lyfe

Last edited by sinnereso : 06/19/23 at 11:15 PM.
  Reply With Quote
06/20/23, 08:59 AM   #5
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,077
Yeah, you are welcome.
But !doh! I said "do not use NAMES" but IDs, as they are renamesave, and you use: "NAME"...

Forgive me, but why?

Instead of

Code:
MyAddon.charVariables = ZO_SavedVars:NewAccountWide( MyAddon.svName, MyAddon.svVersion, GetUnitName("player"), nil )
Simply use:
Code:
MyAddon.charVariables = ZO_SavedVars:NewAccountWide( MyAddon.svName, MyAddon.svVersion, GetCurrentCharacterId(), nil )
All was there
  Reply With Quote
06/20/23, 09:51 PM   #6
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 265
I know I know!!! The ID remain the same even after a namechange. I get it and does make sense but for the moment im happy with this so much and is easier to read in the files. Just being able todo this opens up many options ive been pondering for some time. Ill make note of the ID thing for now.
  Reply With Quote
Yesterday, 08:05 PM   #7
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 265
Question

Resurrecting my own thread for an additional queston related directly to the initial one...

if I were to need to iterate over the previously mentioned sub table in my saved variables how would I got about it? I've tried everything but at best have only been able to see a table and two functions but no data thats there.. Samples of what im working with below...

Code:
--saved variable definition:
RidinDirty.subtable = ZO_SavedVars:NewAccountWide( RidinDirty.svName, RidinDirty.svVersion, "some table", defaultSavedVars )

--which looks like this in the saved variable file:
RidinDirtyVars =
{
    ["Default"] = 
    {
        ["@sinnereso"] = 
        {
            ["$AccountWide"] = 
            {
                ["travelOutside"] = true,
                ["apDeposit"] = true,
                ["bankALL"] = false,
                ["pvpDeaths"] = 2,
                ["some table"] = 
                {
                    [4487] = "item name1",--<<  these are itemID as the key and item names as a text decription
                    [43663] = "item name2",
                    [533] = "item name3",

im able to save and access them individually by itemid via:
RidinDirty.subtable[itemId] = itemName
somevariable = RidinDirty.subtable[itemId]

what I'd like to be able to do is iterate over the entire subtable without previously knowing the keys/itemid's and print it to the chat window via something like:
for i, v in pairs(RidinDirty.subtable) do--<< i used in pairs because the keys are itemID's and not sequential
	if i ~= nil then
		print(i, v)
	end
end

any suggestions?
  Reply With Quote
Yesterday, 08:17 PM   #8
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,077
RidinDirty.subtable = ZO_SavedVars:NewAccountWide( RidinDirty.svName, RidinDirty.svVersion, "some table", defaultSavedVars )

Lua Code:
  1. RidinDirtyVars =  -- ## global table with name defined in your manifest txt at ## SavedVariables: RidinDirtyVars
  2. {
  3.     ["Default"] =  --namespace param of ZO_SavedVars wrapper
  4.     {
  5.         ["@sinnereso"] = --displayName param of ZO_SavedVars wrapper (or if displayName == nil it will be automatically filled wtith logged in GetDisplayName() )
  6.         {
  7.          ["optionalProfileHere"] ---Optional: Profile param of ZO_SavedVars wrapper
  8.          {
  9.             ["$AccountWide"] = --automatically there if using ZO_SavedVars:NewAccountWide / Will be GetCurrentCharacterId() if using :NewCharacterIdSettings
  10. wrapper
  11.              {




The namespace param where you have used "some table" is not pointing to any table IN your savedvariables
but it's the table "Default" which is ABOVE your savedvariables table, to structure it.
Check the ZO_SavedVars wrapper description in ZOs code:
https://github.com/esoui/esoui/blob/...edvars.lua#L11

So your pointer to the SV table, RidinDirty.subtable, actually points to nothing at the moment, just maybe an empty table where defaultSavedVars have been set to then.

If you want to point to the correct table in your example you need to use:
RidinDirty.subtable = ZO_SavedVars:NewAccountWide( RidinDirty.svName, RidinDirty.svVersion, nil, defaultSavedVars )

Either set the namespace to nil which will be replaced internally in ZO_SavedVars wrapper with "Default" then, or pass in "Default" yourself.

After that you can iterate over RidinDirty.subtable and you should get all available data BELOW ["$AccountWide"], including the subtable ["some table"].

Or you directly use RidinDirty.subtable["some table"]


Read more about SV here too: https://wiki.esoui.com/Storing_data_and_accessing_files


And maybe use helper addons like merTorchbug ingame to inspect the SavedVars table /tb RidinDirtyVars
to understand it's structure better and see current values.
If you use ZO_SavedVars the table will always have some functions at the 1st level, GetInterfaceForCharacter and ResetToDefaults, which will be added by the ZO_SavedVars wrapper! default is your passed in table with the default values then -> defaultSavedVars


Just click the __index in tbug inspector then to go on to the real table contents

Last edited by Baertram : Yesterday at 08:25 PM.
  Reply With Quote
Yesterday, 08:23 PM   #9
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 265
The namespace param where you have used "some table" is not pointing to any table IN your savedvariables
but it's the table "Default" which is ABOVE your savedvariables table, to structure it.
Check the ZO_SavedVars wrapper description in ZOs code:
https://github.com/esoui/esoui/blob/...edvars.lua#L11

So your pointer to the SV table, RidinDirty.subtable, actually points to nothing at the moment, just maybe an empty table where defaultSavedVars have been set to then.

If you want to point to the correct table in your example you need to use:
RidinDirty.subtable = ZO_SavedVars:NewAccountWide( RidinDirty.svName, RidinDirty.svVersion, nil, defaultSavedVars )

Either set the namespace to nil which will be replaced internally in ZO_SavedVars wrapper with "Default" then, or pass in "Default" yourself.

After that you can iterate over RidinDirty.subtable and you should get all available data BELOW ["$AccountWide"], including the subtable ["some table"].

Or you directly use RidinDirty.subtable["some table"]


Read more about SV here too: https://wiki.esoui.com/Storing_data_and_accessing_files
OH!! I never tried accessing it from the primary saved variable table just that specific subtable.. I'll give it a whirl

One last question.. there is a space in the table name. is that gonna be a problem? the table is named like "some table" with the itemid's and names inside it.

Last edited by sinnereso : Yesterday at 08:26 PM.
  Reply With Quote
Yesterday, 08:42 PM   #10
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 5,077
Spaces in variables names are never a good idea

I guess it could work if you always use myTable["name here"] to access it but I actually never tried that -> I expect it to fail
But myTable.name here definately would not work!
  Reply With Quote
Yesterday, 08:53 PM   #11
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 265
Originally Posted by Baertram View Post
Spaces in variables names are never a good idea

I guess it could work if you always use myTable["name here"] to access it but I actually never tried that -> I expect it to fail
But myTable.name here definately would not work!
What you suggested is working using the primary saved variable with the["some table"] added to it ty. Now my next hurdle which is on me and I think I can handle it is converting my text names to item links to do what I have plans for it to do..

I may need to reset the table for this and while I'm at it ill maybe remove the space as well.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » General saved variable questions


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