Thread: Update 2.5
View Single Post
08/01/16, 11:55 AM   #40
Weolo
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 79
Shadow of the Hist - Storing addon data

Shadow of the Hist now comes with API functions to allow your addon data to survive a character rename, class change, race change and even figure out if the player has deleted an old character.

Old way of storing addon data
Lua Code:
  1. local settings =  ZO_SavedVars:NewAccountWide("Test Addon", 1, nil, defaults)
  2. local name = zo_strformat("<<1>>",GetRawUnitName("player"))
  3. settings[name] = {
  4.   [something] = 1,
  5.   [show] = true
  6. }
This would produce a saved variables file like this:
Lua Code:
  1. {
  2.   ["Fred Smith"] = {
  3.     ["something"] = 1,
  4.     ["show"] = false,
  5.   },
  6.   ["John Smith"] = {
  7.     ["something"] = 2,
  8.     ["show"] = true,
  9.   }
  10. }

The problem with that is if the player purchases a rename token they will not have access to their old addon data.

This can all be fixed with these few functions

GetNumCharacters()
GetCharacterInfo()
GetCurrentCharacterId()

The "CharacterId" is unique for each character you have.

Example of the new way to store addon data
Lua Code:
  1. local id = GetCurrentCharacterId()
  2. settings[id] = {
  3.   [something] = 1,
  4.   [show] = true
  5. }
This would produce a saved variables file like this:
Lua Code:
  1. {
  2.   ["11111111"] = {
  3.     ["something"] = 1,
  4.     ["show"] = false,
  5.   },
  6.   ["22222222"] = {
  7.     ["something"] = 2,
  8.     ["show"] = true,
  9.   }
  10. }

And they can also be used to convert the old format over to the new one.
Lua Code:
  1. for i = 1, GetNumCharacters() do
  2.   local name, gender, level, championRank, classId, raceId, alliance, id, locationId = GetCharacterInfo(i)
  3.   name = zo_strformat("<<1>>",name)
  4.   if settings[name] then
  5.     --Found the old data, now convert it
  6.     settings[id] = {
  7.       [something] = settings[name].something,
  8.       [show] = settings[name].show
  9.     }
  10.     --Now tidy up/remove the old data
  11.     settings[name] = nil
  12.   end
  13. end
  14.  
  15. local id = GetCurrentCharacterId()
  16. settings[id] = {
  17.   [something] = 1,
  18.   [show] = true
  19. }

This last example just tidies up old/deleted characters
Lua Code:
  1. local found = {}
  2. for i = 1, GetNumCharacters() do
  3.   local name, gender, level, championRank, classId, raceId, alliance, id, locationId = GetCharacterInfo(i)
  4.   found[id] = true
  5. end
  6.  
  7. for id,data in pairs(settings) do
  8.   if not found[id] then
  9.     settings[id] = nil
  10.   end
  11. end
  12.  
  13. local id = GetCurrentCharacterId()
  14. settings[id] = {
  15.   [something] = 1,
  16.   [show] = true
  17. }

I did find one bug with GetCharacterInfo() on PTS which is being fixed and that was if you created a brand new character they did not appear in GetCharacterInfo(). But that is easy enough to fix with a small bit of code for now. I need to test live once Shadow of the Hist is all patched up to see if the bug fix is there.
  Reply With Quote