Quantcast Purpose of Ace3's OnDisable() function? - WoWInterface
Thread Tools Display Modes
04-09-19, 04:06 PM   #1
LudiusMaximus
A Wyrmkin Dreamwalker
 
LudiusMaximus's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2018
Posts: 56
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?
  Reply With Quote
04-11-19, 02:17 AM   #2
humfras
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 130
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.
__________________
Author of VuhDo CursorCastBar OptiTaunt Poisoner RaidMobMarker

Last edited by humfras : 04-11-19 at 02:28 AM.
  Reply With Quote
04-11-19, 05:06 AM   #3
LudiusMaximus
A Wyrmkin Dreamwalker
 
LudiusMaximus's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2018
Posts: 56
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!
  Reply With Quote
04-12-19, 04:55 AM   #4
myrroddin
A Molten Giant
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 991
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.

Last edited by myrroddin : 04-13-19 at 03:33 AM. Reason: Adjust for unintended emoticon
  Reply With Quote
04-12-19, 05:04 AM   #5
myrroddin
A Molten Giant
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 991
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

Last edited by myrroddin : 04-13-19 at 03:33 AM. Reason: thanks humfras!
  Reply With Quote
04-13-19, 02:27 AM   #6
humfras
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2009
Posts: 130
@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".
__________________
Author of VuhDo CursorCastBar OptiTaunt Poisoner RaidMobMarker
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Purpose of Ace3's OnDisable() function?

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