View Single Post
10/25/20, 09:58 AM   #4
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
Did you add the name of your SavedVariables to your txt file like I have described?
## SavedVariables: TTCPSavedVars

Else the global table _G["TTCPSavedVars"] won't be created and nothing will be saved upon logout/reloadui/zone change loading screens.

TTCP.CheckTable() needs to be called ONCE ONLY in your function TTCP:Initialize().
Else you will overwrite it always on each combat state change again with NIL and the default values from TTCP.Table!
Just use TTCP.Saved after loading the savedvariables ONCE to access/change your SV data.


And you should add an unregister to EVENT_ADD_ON_LOADED once your addon was loaded as after that the function shouldn't be called aymore.
Remeber: It will be called once for EACH addon/library!
Code:
TTCP = {}
TTCP.name = "TTCP"
TTCP.Table = {
    item1 = "123",
    item2 = "456"
}

function TTCP.LoadSavedVars()
    TTCP.Saved = ZO_SavedVars:NewAccountWide("TTCPSavedVars",1,nil,TTCP.Table)
end

function TTCP:Initialize()
    TTCP.LoadSavedVars()
    self.inCombat = IsUnitInCombat("player")
    EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)

end

function TTCP.OnAddOnLoaded(event, addonName)
    if addonName == TTCP.name then
        EVENT_MANAGER:UnregisterForEvent(TTCP.name, EVENT_ADD_ON_LOADED)
        TTCP:Initialize()
    end
end

function TTCP.OnPlayerCombatState(event, inCombat)
    if inCombat ~= TTCP.inCombat then
        TTCP.inCombat = inCombat

        if inCombat then

        d("Entering combat.")
    else
        d(os.date("%M", os.time()))
        d(TTCP.Saved.item2)
        d("Exiting combat.")
    end

    end
end

EVENT_MANAGER:RegisterForEvent(TTCP.name, EVENT_ADD_ON_LOADED, TTCP.OnAddOnLoaded)
Explanation:
SavedVariables are a global table defined with your SV name "TTCPSavedVars", saved in the global table _G.
As you put the name into your addon's txt file ## SavedVariables: tag it will create that table in _G -> _G["TTCPSavedVars"] (same as just TTCPSavedVars).
You are able to access that table now ingame with your addon already, even without using ZO_SavedVars.

As you use ZO_SavedVars:New... it will create a reference to that global table, adding some subtables and values like the version, accountName/characterId etc. depending on what you use as ZO_SavedVars:New... (AccountWide, CharacterIdSettings).
And you assign this reference to your addons table TTCP.Saved now as a new reference.
So TTCP.Saved -> points to TTCPSavedVars.
And each time a reloadui/logout/zone change happens ZO_SavedVars transfers the data from TTCPSavedVars to your file live/SavedVariables/TTCP.lua

Last edited by Baertram : 10/25/20 at 10:08 AM.
  Reply With Quote