Thread: Unload event
View Single Post
08/02/15, 11:39 AM   #4
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
You don't have to do this.


When player logout, reloadui, change cvar or get kicked for inactivity, saved variables are automatically saved. there's nothing to do.

You must not prehook or use those events if it's for saved variables usage.


For people who want to to this, here's how I do :


At ADD_ON_LOADED :
- I set a flag, this one goes to saved var. it will be used to guess at next ADD_ON_LOADED what kind of exit user did. If there's nothing to do at next add_on_loaded specifically for this kind of exit, don't do this.

Lua Code:
  1. -- ex: inactivity = 1, reloadui = 2, logout = 3, setCvar = 4, quit = 5
  2. local myaddon.db.exitMethod = 1
  3.  
  4. -- then I prehook :
  5.  
  6.  
  7.     -- PreHook ReloadUI, LogOut & Quit
  8.     ZO_PreHook("ReloadUI", function()
  9.         myaddon.db.exitMethod = 2
  10.         myaddon.doThingsAtReloadUI()
  11.     end)
  12.    
  13.     ZO_PreHook("Logout", function()
  14.         myaddon.db.exitMethod = 3
  15.         myaddon.doThingsAtLogOut()
  16.     end)
  17.    
  18.     ZO_PreHook("SetCVar", function()
  19.         myaddon.db.exitMethod = 4
  20.         myaddon.doThingsAtSetCVar()
  21.     end)
  22.  
  23.     ZO_PreHook("Quit", function()
  24.         myaddon.db.exitMethod = 5
  25.         myaddon.doThingsAtQuit()
  26.     end)
  27.  
  28.  
  29. -- then at ADD_ON_LOADED, just before setting the flag, I read value.
  30.  
  31. if myaddon.db.exitMethod == 1 then
  32.   -- last exit was a reloadui, so I do things specificaly at addon startup after a reload.
  33.   myaddon.reloadThings()
  34. end



But I repeat, if it's just only to save things on SavedVars, do not use this trick.

It's mainly used for addons which store things per session and don't want thoses ones being resetted after a reloadui or a characer change.

typical example : the chatbox being wiped, or stats. (KillCounter is a very good exemple too). some DPS meter could use this too but it's less important.
  Reply With Quote