WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Purpose of Ace3's OnDisable() function? (https://www.wowinterface.com/forums/showthread.php?t=57104)

LudiusMaximus 04-09-19 04:06 PM

Purpose of Ace3's OnDisable() function?
 
Looking at other people's Ace3-based addons, it seems to be best practice to use the OnDisable() function to undo everything you did in OnEnable(); like hooking functions, registering events etc.

I have been doing the same for my addons but I never really understood why. Because whenever I disable my addon (i.e. untick it in the "AddOn List"), the OK button turns into "Reload UI". So I assume my OnDisable() function is never called at all? Why do I need it then?

Furthermore, when I use hooksecurefunc in my OnEnable(), there is not really any way of undoing this in OnDisable(), right?

humfras 04-11-19 02:17 AM

Ace expects those 3 functions from its addons.

Most AddOns don't use OnDisable at all but provide it 'just in case'.
If it is not going to be used, you can leave it empty.
Most AddOns simply silence* themselves for specific configurations/specs/instances/encounters but do not disable completely.
*deactivate certain functionalities, unregister certain events etc.

If you disable your AddOn via DisableAddOn, it won't be loaded for the following logins/reload.
Using an AceAddOns OnDisable, you can deactivate said AddOn's functionalities as defined in the function without a reload (hooks and frames persist).

And yes, you can not remove hooks, only with a reload.
But if you need to have an option to ignore a hook, you can add a return, eg
Lua Code:
  1. local runhook = true
  2. hooksecurefunc(func, function(...)
  3.   if not runhook then return end
  4.   -- your code here
  5. end)


see https://www.wowace.com/projects/ace3...etting-started

Code:

Standard methods
AceAddon typically expects your addon to define (typically in your main Lua file) 3 methods that it calls at various points:

function MyAddon:OnInitialize()
  -- Code that you want to run when the addon is first loaded goes here.
end
The OnInitialize() method of your addon object is called by AceAddon when the addon is first loaded by the game client. It's a good time to do things like restore saved settings (see the info on AceConfig for more notes about that).

function MyAddon:OnEnable()
    -- Called when the addon is enabled
end

function MyAddon:OnDisable()
    -- Called when the addon is disabled
end
The OnEnable() and OnDisable() methods of your addon object are called by AceAddon when your addon is enabled/disabled by the user. Unlike OnInitialize(), this may occur multiple times without the entire UI being reloaded.


LudiusMaximus 04-11-19 05:06 AM

Hey thanks! This was pretty enlightening indeed. :-)

So basically I am providing the OnDisable() function for anyone else who wants to silence my Addon in certain situations without having to reload the UI! That's fair enough.

Also thanks for the runhook tip!

myrroddin 04-12-19 04:55 AM

You can remove hooks if you use AceHook-3.0.

OnDisable is used if you want to create a "standby" mode for your addon. It isn't truly disabled, as the DisableAddOn API is not called. You would use the function to wipe variables, remove hooks, unregister events, etc, that you don't want to keep in memory or eating CPU cycles during "standby".

In your options table, you need to create an option to toggle enable/disable, calling OnEnable and OnDisable respectively.

If you leave out the toggle in your options, then your addon will fire OnInitialize and OnEnable as normal, but completely ignore OnDisable.

myrroddin 04-12-19 05:04 AM

I'm not sure if it is in the documentation other than reading the Ace3 code, so here's a tip: if you create the toggle mentioned above in your options table, you can set the load state as per user settings.
Lua Code:
  1. function MyAddOn:OnInitialize()
  2.     -- set up your saved variables and defaults as normal for Ace3
  3.  
  4.     -- keep user setting for enabled/disabled
  5.     self:SetEnabledState(self.db.profile.enabled) -- this line, assuming you name the toggle enabled in the defaults table
  6. end

humfras 04-13-19 02:27 AM

@myrroddin:
You missed a bracket ;)

@LudiusMaximus
As myrroddin states, if you use AceHook, you can unhook your hooks.
This is because AceHook itself hooks the function/script and only calls your code when you have told it to do so. AceHook's initial hooks persists though.
So it's a question of "do I want to handle hooks myself or should AceHook do it".


All times are GMT -6. The time now is 06:48 PM.

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