Thread Tools Display Modes
05-06-15, 05:58 AM   #1
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
ADDON_LOADED fired multiple times?

Am developing my first addon and today I noticed something strange.
I have a logging funtion that sends me messages to the default chat window to see what is happening within my addon.
The ADDON_LOADED event gets called several (a dozen, didn't count) times.
Is that default behavior or could there be a fault inside the coding of my addon?

Below is the part that sets up and registers the events.

Code:
--  Set up the frame for the event handler
Historia_frame = CreateFrame("Frame");

Historia_frame:SetScript("OnEvent", function(self,event, ...)
		Historia.EventManager(self,event,...); -- call event handler
			end);
			
 Historia_frame:RegisterEvent("ADDON_LOADED");
 Historia_frame:RegisterEvent("PLAYER_ENTERING_WORLD");
 Historia_frame:RegisterEvent("PLAYER_LEAVING_WORLD");
 Historia_frame:RegisterEvent("PLAYER_LEVEL_UP");
 Historia_frame:RegisterEvent("PLAYER_MONEY")
  Reply With Quote
05-06-15, 07:12 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
ADDON_LOADED fires for every AddOn the game loads, user developed or Blizzard in origin.
Code:
local MyAddOn, private_table = ...

private_table.eventFrame = CreateFrame("Frame")
private_table.eventFrame:RegisterEvent("ADDON_LOADED")

private_table.eventFrame:SetScript("OnEvent", function(self, event, ...)
    private_table.eventFrame[event](self, ...) -- call event handlers
end)

function private_table.eventFrame:ADDON_LOADED(AddOn)
    if not AddOn == MyAddOn then
        return -- not my AddOn
    end
    -- do stuff because this is your AddOn
    -- usually, the first thing you do here is Unregister ADDON_LOADED
    -- because you no longer need it
    -- now set up your saved variables and other "run once" stuff
end

Last edited by myrroddin : 05-06-15 at 07:15 AM. Reason: comment the code
  Reply With Quote
05-06-15, 11:56 AM   #3
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
Thank you for your quick response.
Worked like a charm.

Made one adjustment to the code you posted.

Lua Code:
  1. if not AddOn == MyAddOn then

did not work for some reason.
Replaced it by

Lua Code:
  1. if AddOn ~= addon_name then

Now works perfect.

Last edited by Yukyuk : 05-06-15 at 11:59 AM. Reason: Edited a typo
  Reply With Quote
05-06-15, 12:41 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,325
It's customary practice to unregister the event when you're done with it and don't need it to fire anymore. To do this, you just run self:UnregisterEvent("ADDON_LOADED") at the end of the function.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
05-06-15, 11:12 PM   #5
Yukyuk
A Chromatic Dragonspawn
 
Yukyuk's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2015
Posts: 179
Thx for the advice SDPhantom.
Was also advised by myrroddin.

And who I am to ignore the advice from two experienced addon developers
ADDON_LOADED is now unregistered after it has been handled.
  Reply With Quote
05-06-15, 11:30 PM   #6
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
Originally Posted by Yukyuk View Post
Thank you for your quick response.
Worked like a charm.

Made one adjustment to the code you posted.

Lua Code:
  1. if not AddOn == MyAddOn then

did not work for some reason.
Replaced it by

Lua Code:
  1. if AddOn ~= addon_name then

Now works perfect.
That's because "not AddOn" evaluates to false if AddOn is non-nil and true otherwise. So that line is essentially saying "if false/true == MyAddOn then" which isn't at all what you want for a string comparison.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » ADDON_LOADED fired multiple times?


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