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:
-- Load the saved variables
db = ZO_SavedVars:NewAccountWide("MYADDON_SAVEDVARS", 1, nil, defaults)
-- db.char contains all my characters
if NAME_CHANGE:DidNameChange() then
db.char[GetUnitName("player")] = db.char[NAME_CHANGE:GetOldCharacterName()]
db.char[NAME_CHANGE:GetOldCharacterName()] = nil
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:
-- Character renamed
if NAME_CHANGE:DidNameChange() then
db.char[GetUnitName("player")] = db.char[NAME_CHANGE:GetOldCharacterName()]
db.char[NAME_CHANGE:GetOldCharacterName()] = nil
end
-- Add id to protect
for i = 1, GetNumCharacters() do
local name, _, _, _, _, _, characterId = GetCharacterInfo(i)
name = zo_strformat(SI_UNIT_NAME, name)
if db.char[name] and not db.char[name].id then
db.char[name].id = characterId
end
end
-- No id = Char deleted
for charName, charData in pairs(db.char) do
if charName ~= GetUnitName("player") and not charData.id then
db.char[charName] = nil
end
end
Now your addon is namechange proof and delete entries itself if user delete a char.