ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Saving Companiondata on user Logout (https://www.esoui.com/forums/showthread.php?t=10404)

cabwav 01/09/23 06:00 AM

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

Baertram 01/09/23 06:07 AM

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

cabwav 01/09/23 07:28 AM

Solved!
 
That worked, awesome! :D

Many thanks!


All times are GMT -6. The time now is 05:42 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI