Thread Tools Display Modes
01/09/23, 06:00 AM   #1
cabwav
Join Date: Aug 2020
Posts: 7
Saving Companiondata on user Logout

Hi Guys

I am writing an addon that saves all kinds of character data to SavedVariables on user logout. The data is saved to a SavedVariables block in a function that is called at the EVENT_PLAYER_DEACTIVATED event. All character data is saved as expected, but at the time of the event, the active Companion is already de-activated, so I cannot save Companion data like e.g. rapport. When I /reloadui the companion data is saved as expected, so apparently the Companion is still active at this point, but not at the event as mentioned above.

Is there a way to detect that a normal logout is about to happen, and where the currently selected Companion is still active?

I tried the EVENT_ACTIVE_COMPANION_STATE_CHANGED but this has the same result: no active Companion on user Logout

Many thanks in advance, Cabwav
  Reply With Quote
01/09/23, 06:07 AM   #2
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,913
Add a ZO_PreHook to the logout and/or quit function instead or in addition.
They will be called as you logout/quit by the menu or /logout slash command or /quit slash command

PreHooks will be called before the original functions are called. So you can add your callback function there, like you add it to an event, and it will run your code first, and then the original code.
Make sure you DO NOT "return true" in your prehook or it will cancel all following coe, including vanilla game code (like the logout or quit).

To find the logout and quit functions to hook into:
Search the esoui sources (https://github.com/esoui/esoui/tree/master/esoui) for the quit and/or logout to find the function names.
If you search for logout e.g. you'll find
ShowLogoutDialog
which is local and locals cannot be hooked into! So you need to find the global function (API function) used to call that dialog function ShowLogoutDialog -> OnLogoutDeferred (local too) ->
Code:
EVENT_MANAGER:RegisterForEvent("Globals", EVENT_LOGOUT_DEFERRED, OnLogoutDeferred)
EVENT_MANAGER:RegisterForEvent("Globals", EVENT_LOGOUT_DISALLOWED, OnLogoutDisallowed)
-> Here you got 2 other events to maybe use instead of a ZO_PreHook

There does not seem to exist any global variable to hook into, but the dialog names are shown in the code:
https://github.com/esoui/esoui/blob/...lobals.lua#L25
LOGOUT_DEFERRED
QUIT_DEFERRED

If you search for those you'll find the definition of these dialogs and it's "Yes" button's callback function, which should be the logout/quit function then.
I did find a function "Logout" and "Quit" e.g. that way, and a preHook would look like this then:

Lua Code:
  1. local function myLogoutOrQuitHandler()
  2.   --do your savedvariables update of companion stuff here
  3. end
  4.  
  5. ZO_PreHook("Logout", function()
  6.   myLogoutOrQuitHandler()
  7. end)
  8.  
  9. ZO_PreHook("Quit", function()
  10.   myLogoutOrQuitHandler()
  11. end)


References:
https://github.com/esoui/esoui/blob/...logs.lua#L1841
https://github.com/esoui/esoui/blob/...hared.lua#L139
https://github.com/esoui/esoui/blob/...ands_pc.lua#L1
https://github.com/esoui/esoui/blob/...yboard.lua#L37

Last edited by Baertram : 01/09/23 at 06:21 AM.
  Reply With Quote
01/09/23, 07:28 AM   #3
cabwav
Join Date: Aug 2020
Posts: 7
Thumbs up Solved!

That worked, awesome!

Many thanks!
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Saving Companiondata on user Logout

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