View Single Post
09/28/16, 06:24 PM   #5
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
A little something that ZOS added with name changing.

Case : You're using NewAccountWide.
You have a table per character inside your db.




How to handle a name change ?

Lua Code:
  1. -- Load the saved variables
  2. db = ZO_SavedVars:NewAccountWide("MYADDON_SAVEDVARS", 1, nil, defaults)
  3.        
  4. -- db.char contains all my characters
  5. if NAME_CHANGE:DidNameChange() then
  6.     db.char[GetUnitName("player")] = db.char[NAME_CHANGE:GetOldCharacterName()]
  7.     db.char[NAME_CHANGE:GetOldCharacterName()] = nil
  8. end

Like this, the data of the concerned character will automatically be moved to the new name.




Now, how to handle a deleted char ?

If you change the name of a character, and log another character, the old name will still be here, name change will be "on hold".


Multiple answers : Here's mine !
  • New addon : Save the ID of your characters. ALWAYS. An Id inside the table will be the best choice for you to handle the name change. Look at id, if correct, just rename key. if id is not in loop, it's a deleted char.
  • Old addon: Be strong and clean the data. it should auto rebuild at next login. and you'll have same process as a new addon. auto cleaning, no user input.


How to do ?

Lua Code:
  1. -- Character renamed
  2. if NAME_CHANGE:DidNameChange() then
  3.     db.char[GetUnitName("player")] = db.char[NAME_CHANGE:GetOldCharacterName()]
  4.     db.char[NAME_CHANGE:GetOldCharacterName()] = nil
  5. end
  6.  
  7. -- Add id to protect
  8. for i = 1, GetNumCharacters() do
  9.     local name, _, _, _, _, _, characterId = GetCharacterInfo(i)
  10.     name = zo_strformat(SI_UNIT_NAME, name)
  11.     if db.char[name] and not db.char[name].id then
  12.         db.char[name].id = characterId
  13.     end
  14. end
  15.  
  16. -- No id = Char deleted
  17. for charName, charData in pairs(db.char) do
  18.     if charName ~= GetUnitName("player") and not charData.id then
  19.         db.char[charName] = nil
  20.     end
  21. end

Now your addon is namechange proof and delete entries itself if user delete a char.
  Reply With Quote