View Single Post
02-10-12, 02:06 AM   #3
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
ADDON_LOADED fires every time any addon (including the Blizzard ones) finishes loading. The first argument passed with the event is the name of the addon that finished loading. When it fires for an addon, that means that:
  • all of the files in the addon's folder have been read,
  • all objects defined in any XML files have been created,
  • all code in the top-level scope of any Lua files has been executed,
  • all font and textures files have been loaded into memory, and
  • all previously stored saved variables for the addon have been loaded into memory.
Most addons listen for their own ADDON_LOADED event, and perform basic start-up tasks like initializing saved variables when it fires.

PLAYER_LOGIN fires at approximately the same time that the initial loading screen disappears and the game world becomes visible. Generally, this event means that all non-LoD addons have finished loading.

Most addons listen for this event, and perform additional start-up tasks like creating visible UI objects (eg. action buttons or unit frames) and registering for general events (eg. UNIT_HEALTH or PLAYER_REGEN_ENABLED). Since the user cannot see or interact with the UI prior to this event firing, there is no reason to perform these tasks earlier.

If you are writing an addon that modifies another addon's configuration, you will usually want to listen for the other addon's ADDON_LOADED event, and make your changes when it fires. However, depending on the other addon's design, and the nature of the changes you wish to make, you may need to listen for the PLAYER_LOGIN event instead (though I have never found this useful), or temporarily pre-hook one of the addon's functions when its ADDON_LOADED event fires and make your changes when that function is run:

Lua Code:
  1. local f = CreateFrame("Frame")
  2. f:RegisterEvent("ADDON_LOADED")
  3. f:SetScript("OnEvent", function(self, event, addonName)
  4.     if addonName ~= "TargetAddon" then
  5.         -- Not the right addon.
  6.         -- Don't do anything else just now, but keep listening
  7.         -- for future events.
  8.         return
  9.     end
  10.  
  11.     -- If we reach this point, it's the right addon.
  12.  
  13.     -- Get a reference to a function the addon runs to set itself up.
  14.     local original = TargetAddon.DoSomeSetupStuff
  15.  
  16.     -- Make the addon run a different function instead.
  17.     function TargetAddon:DoSomeSetupStuff(...)
  18.         -- Change some settings here, before the addon tries
  19.         -- to do anything with them.
  20.  
  21.         -- Let the original function run.
  22.         original(self, ...)
  23.  
  24.         -- Run any code here that needs to happen after the
  25.         -- addon sets itself up, like parenting frames to other
  26.         -- frames, etc.
  27.  
  28.         -- Put the original function back. (optional)
  29.         self.DoSomeSetupStuff = original
  30.     end
  31.  
  32.     -- We found the addon and modified it. We don't need to
  33.     -- listen for the addon's loading event anymore.
  34.     self:UnregisterEvent("ADDON_LOADED")
  35.  
  36.     -- No events are registered anymore, so we don't need an
  37.     -- event handler anymore either. Unset it, so this function
  38.     -- gets garbage-collected and removed from memory.
  39.     self:SetScript("OnEvent", nil)
  40. end)

For a more complex example that handles modifying multiple addons, see the attached file. It's what I use to apply custom borders to a number of addons. You'll notice that in some cases, I need to permanently hook one or more of the addon's functions, so that I can overwrite changes the addon makes during gameplay, long after the initial loading process.
Attached Files
File Type: lua Apply.lua (13.3 KB, 577 views)
  Reply With Quote