View Single Post
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,000
>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