Thread Tools Display Modes
10/25/20, 09:11 AM   #1
mihannnik
Join Date: Oct 2020
Posts: 4
One more question about ZO_SavedVars

When I searched the API for information about ZO_SavedVars I could not find a way to load the variables after the first load. Is there a way to update stored variables more than once, or does this update only happen once?
I've tried doing:
Saved = nil
Saved = ZO_SavedVars:NewAccountWide("SavedVars", 1, nil, Table)
But it didn't work.
  Reply With Quote
10/25/20, 09:28 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
SavedVariables via ZO_SavedVars will be assigned to your table.
If you update any value in your table they will be written to the file on zone change, logout, quit.

So you need to assign the SavedVariables in your addon's
EVENT_ADD_ON_LOADED
callback function e.g. and then just update the table you have assigned to the ZO_SavedVars.
EVENT_ADD_ON_LOADED will be called for EACH addon so make sure you will check the event's parameter addonName if it's "your addon" and only then do your addon related stuff AND unregister the EVENT_ADD_ON_LOADED again so it will not fire again afterwards.
Eacht ime you do a reloadui etc. the event will be registerted again and load your addon code again, so this way your SavedVariables will be read from the disk files to your ingame table again.

Be sure to also add the name you have chosen for your SavedVariables in your addon's manifest txt file as well after the
##SavedVariables:
tag!

AND do not choose the same name for your global addon variable and the SavedVariables name as the SavedVariables name will create a global table with that name as well.

In your example this would be something like this:
Lua Code:
  1. local defaultTable = {
  2.  myVarInSavedVariables = 1
  3. }
  4. local mySavedVariables = ZO_SavedVars:NewAccountWide("SavedVars", 1, nil, defaultTable)
  5.   --Change a value in the SavedVariables
  6.  myVarInSavedVariables.myVarInSavedVariables = 2

In your addon's txt file add:
## SavedVariables: SavedVars

But I'd choose something else then just SavedVars! Add your addon name up in front like MyAddon_SavedVars so it is unique! Else other addons might overwrite it.

Read this tutorial for further information:
https://wiki.esoui.com/SimpleNotebookTutorial/part1

Part4 contains savedVariables:
https://wiki.esoui.com/SimpleNotebookTutorial/part4

There also exist basic addon examples where you can spy the code:
https://www.esoui.com/downloads/info...late.html#info

Last edited by Baertram : 10/25/20 at 09:32 AM.
  Reply With Quote
10/25/20, 09:39 AM   #3
mihannnik
Join Date: Oct 2020
Posts: 4
Originally Posted by Baertram View Post
SavedVariables via ZO_SavedVars will be assigned to your table.
I already check some tutorials and use create functions:

TTCP = {}
TTCP.name = "TTCP"
TTCP.Table = {
item1 = "123",
item2 = "456"
}

function TTCP:Initialize()
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
TTCP:Initialize()
end
end

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

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

if inCombat then

d("Entering combat.")
else
TTCP.CheckTable()
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)
But TTCP.CheckTable() work only one time and cant update variables. I want change this table in other program and create ingame notifications. I use OnPlayerCombatState only because it very easy to triggering.
Can i update saved variables every time when i need to check file? Or i can do it another way? Thanks!
  Reply With Quote
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,912
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
10/25/20, 10:19 AM   #5
mihannnik
Join Date: Oct 2020
Posts: 4
Originally Posted by Baertram View Post
Did you add the name of your SavedVariables to your txt file like I have described?
## SavedVariables: TTCPSavedVars
My TTCP.txt
Code:
## Title: TTCP
## APIVersion: 100032
## SavedVariables: TTCPSavedVars
TTCP.lua
Else the global table _G["TTCPSavedVars"] won't be created and nothing will be saved upon logout/reloadui/zone change loading screens.
I dont need to save anything, I only want to read file every time when player leave combat
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!
Add in my addon in Initialize() function!

Or, maybe, i can read file using file : open(file)? In this way how to use absolute path in LUA?

Last edited by mihannnik : 10/25/20 at 10:21 AM. Reason: Question added
  Reply With Quote
10/25/20, 10:42 AM   #6
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,912
READ what I wrote at "Explanation" in my last post.
There is no manual file read or write in ESO! It ONLY happens at loading screens, login, logout, reloadui.
Not as you call a function.

All you need to do is update the table and it will be automatically written/read to/from your SV file upon loading screen/reloadui/logout. That's all.
Access to files via lua in ESO is forbidden.

https://wiki.esoui.com/Storing_data_and_accessing_files

Last edited by Baertram : 01/07/21 at 06:29 AM.
  Reply With Quote
10/25/20, 10:47 AM   #7
mihannnik
Join Date: Oct 2020
Posts: 4
Originally Posted by Baertram View Post
READ what I wrote at "Explanation" in my last post.
There is no manual file read or write in ESO! It ONLY happens at loading screens, login, logout, reloadui.
Not as you call a function.

All you need to do is update the table and it will be automatically written to your SV file upon loading screen/reloadui/logout. That's all.
Access to files via lua in ESO is forbidden.
Ok, thank you!
  Reply With Quote

ESOUI » Developer Discussions » Tutorials & Other Helpful Info » One more question about ZO_SavedVars

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off