Thread Tools Display Modes
02-07-12, 03:16 PM   #1
Animor
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Mar 2011
Posts: 136
Ace3 OnDisable() and RefreshConfig() usage

Hi,

I've read the info on Ace3 libs, but there are still things I'm not sure how to handle on my addon:

1. How do I use Myaddon:OnDisable() ?
I have a GUI with globalEn toggle button. Right now globalEn only disables internal functions in my code.
Should I run Myaddon: Disable() when the user uncheck this button? What else will it do besides calling Myaddon:OnDisable()? Is it a good practice to do so?

2. The doc guided to use:
Code:
function MyAddon:OnInitialize()
  self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults)
  self.db.RegisterCallback(self, "OnProfileChanged", "RefreshConfig")
  self.db.RegisterCallback(self, "OnProfileCopied", "RefreshConfig")
  self.db.RegisterCallback(self, "OnProfileReset", "RefreshConfig")
end

function MyAddon:RefreshConfig()
  -- would do some stuff here
end
I've looked in some other addons and saw that they pass variables into this functions, but every addon does it a bit differently. For example:
MyAddon:RefreshConfig(_, event, database)
or
MyAddon:RefreshConfig(event, database, newProfileKey)

What is the correct way to use RefreshConfig (which vars to pass)?
Only thing I have there now is the db upvalue (db = self.db.profile). What else should I put there?

Thanks!
  Reply With Quote
02-07-12, 10:16 PM   #2
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Animor View Post
1. How do I use Myaddon:OnDisable() ?
Personally I've never seen the point in using AceAddon or AceEvent, but if you are using them, then calling MyAddon:Disable() will automatically unregister all events/messages that were registered with your addon through AceEvent. If you want other things to occur (eg. stop OnUpdates, hide frames, disable buttons) then you need to make those things occur yourself with code in a MyAddon:OnDisable() function.

If you already had a "disable addon" button, then probably anything you run when that button is clicked should be moved into MyAddon:OnDisable() instead, and clicking the button should just call MyAddon:Disable().

Originally Posted by Animor View Post
What is the correct way to use RefreshConfig (which vars to pass)?
You are already doing it correctly. You don't need to pass/accept anything. I've actually never seen an addon doing that, and can only imagine it being useful for very complex addons using multiple databases.

Originally Posted by Animor View Post
Only thing I have there now is the db upvalue (db = self.db.profile). What else should I put there?
If your addon has any "static" settings such as window positions, fonts, or colors you need to change them when the profile is changed. Essentially, anything you call when your addon loads (to place frames, set fonts, etc.) should be called again here with the new values.
  Reply With Quote
02-08-12, 03:10 AM   #3
Animor
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Mar 2011
Posts: 136
Thanks!

You said that "calling MyAddonisable() will automatically unregister all events/messages that were registered with your addon through AceEvent".

However, from the doc I understood that I have to put the UnregisterEvent myself in OnDisable() :
Code:
function MyAddon:OnDisable()
  -- Unhook, Unregister Events, Hide frames that you created.
  -- You would probably only use an OnDisable if you want to 
  -- build a "standby" mode, or be able to toggle modules on/off.
end
So did I understand it wrong?

Last edited by Animor : 02-08-12 at 03:22 AM.
  Reply With Quote
02-08-12, 07:36 PM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
No; I just haven't used AceEvent in years, since I realized it didn't actually do anything the normal event API didn't do, and just added overhead for no reason.

Anyway, your understanding of the documentation is correct. Looking at the AceAddon code, it appears to no longer has any interaction with AceEvent, so you (probably) need to manually unregister events.

Of course, if you just use the normal event API, you would need to do that anyway. If you still want to use AceAddon (it is convenient for modules, etc) here is a simple way to do it:

Code:
local addon = LibStub("AceAddon-3.0"):NewAddon(CreateFrame("Frame"), "MyAddon", ...)
Now your AceAddon-3.0 object is a frame, instead of a plain table, and as such has all the normal frame methods and properties, including:

Code:
-- Replace the main functionality of AceEvent in 3 lines of code:
addon:SetScript("OnEvent", function(self, event, ...)
     return self[event] and self[event](self, ...)
end)

-- Register events as usual:
addon:RegisterEvent("PLAYER_LOGIN")
addon:RegisterEvent("UNIT_AURA")

-- And define handlers as usual:
function addon:PLAYER_LOGIN()
     print("You have logged in!")
end

function addon:UNIT_AURA(unit)
     if unit == "player" then
          print("The buffs and/or debuffs on you changed!")
     end
end
This also has the advantage of not dying with obnoxious errors if you try to register an event that's already registered, or unregister one that wasn't registered, both of which are more convenient than running a bunch of checks every time you need to register/unregister events based on changing conditions (eg. user changed an option, or only listening for some events inside combat).
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Ace3 OnDisable() and RefreshConfig() usage


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