Thread Tools Display Modes
03/11/15, 08:09 PM   #1
Deome
 
Deome's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 29
Exclamation Important, Simple, Easily Overlooked Load Error

I've been meaning to remember to start a discussion about this since at least November. Other things intrude, as they so often do.

I made a discovery when I was playing around with a new addon that is very counter-intuitive and probably should go into the wiki tutorial. Here goes:

It does not matter what ADDON_NAME is passed to your OnAddonLoaded function from the ON_ADDON_LOADED event register. The only ADDON_NAME the event will pass to your OnAddonLoaded function is the Name of the Addon Folder

It's so very basic, but it'll raise hell for anyone who doesn't know about it; I know I've seen the KillCounter addon displaying all loaded addon names (I comment that line out in my copy) like I did to figure this out. Simply put, if these names don't match, your addon won't load if you have a line like this in your OnAddonLoad function:
Code:
 if ADDON_NAME == "TheAddonNamePassedtothisFunction" then
I'm serious--I've passed "Timbuktu" as the name of the addon in the RegisterForEvent function; doesn't matter, the addon folder name is what's passed to the function you register. Just add a simple display to your OnAddonLoaded function if you don't believe me:
Code:
d(addonName)
Obviously, "addonName" is whatever you named the variable your function recieves after whatever you name "eventId".

Stupid, I know, but I've spoken to a few developers who had to change their pants after I told them.
  Reply With Quote
03/11/15, 09:01 PM   #2
circonian
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 613
Originally Posted by Deome View Post
It does not matter what ADDON_NAME is passed to your OnAddonLoaded function from the ON_ADDON_LOADED event register. The only ADDON_NAME the event will pass to your OnAddonLoaded function is the Name of the Addon Folder
From the rest of your post I think you have the right Idea, but it will pass the addon folder names for every addon loaded, not just your addon folders name.
Also the manifest (txt file) must match the folder name or it wont run any of your code.


I'm guessing what you meant was that it does not matter what name you pass to "register" the event:
Lua Code:
  1. EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED, OnAddOnLoaded)
It is true that this: ADDON_NAME does not matter, it will still only pass your OnAddOnLoaded function the folder names of the addons that are loaded.


But the addonName passed to your OnAddonLoaded function does matter. That is how you know when your addon or others are loaded.
If your addon is not fully loaded & you start trying to call functions your going to have problems. Same if your trying to use/access something from another addon. The OnAddonLoaded function tells us when the code in that addon is available.
Lua Code:
  1. local function OnAddOnLoaded(event, addonName)
  2.     if addonName == myAddon.name then
  3.         myAddon:Initialize()
  4.     end
  5. end
  6. EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED, OnAddOnLoaded)

For example what if only the first code file of your addon was loaded but in it you started calling functions defined in another code file, before it got loaded?
The OnAddonLoaded event/function is how we know when the addon is loaded, all of it.

Last edited by circonian : 03/11/15 at 09:06 PM.
  Reply With Quote
03/11/15, 10:32 PM   #3
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
There's nothing stupid about it. Surprising for the unwary, perhaps, but it totally makes sense.

I was going to blame the wiki for your confusion, but after glancing at a few pages, no. For example nowhere on AddOn Quick Questions or EVENT_ADD_ON_LOADED it states that the two arguments you're talking about are interconnected. They aren't, they're completely different things.

EVENT_MANAGER:RegisterForEvent(tag, event, handler)

The first argument (after self) should be a string uniquely identifying the listener. I call that argument tag, others call it namespace. The wiki pages use variants of "YourAddonName" as its value, because it's a natural namespace.

You can't say it doesn't matter. If we all start using "Timbuktu", each event will only be delivered to one add-on -- the one that registers it first. You absolutely have to keep that unique.

EVENT_ADD_ON_LOADED(eventCode, addonName)

The second argument is an actual add-on name. It has nothing to do with the identity of the listener. Everyone who registered for the event receives the same list of add-on names.
  Reply With Quote
03/12/15, 08:23 PM   #4
Deome
 
Deome's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 29
Originally Posted by circonian View Post
From the rest of your post I think you have the right Idea, but it will pass the addon folder names for every addon loaded, not just your addon folders name.
Also the manifest (txt file) must match the folder name or it wont run any of your code.


I'm guessing what you meant was that it does not matter what name you pass to "register" the event:
Lua Code:
  1. EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED, OnAddOnLoaded)
It is true that this: ADDON_NAME does not matter, it will still only pass your OnAddOnLoaded function the folder names of the addons that are loaded.
Yep, that's what I mean. And yeah, KillCounter's d(addonName) does just that. That's how I found out that my addon wasn't being loaded at all because myAddon.name needs to match the folder name.


Originally Posted by circonian View Post
But the addonName passed to your OnAddonLoaded function does matter. That is how you know when your addon or others are loaded.
If your addon is not fully loaded & you start trying to call functions your going to have problems. Same if your trying to use/access something from another addon. The OnAddonLoaded function tells us when the code in that addon is available.
Lua Code:
  1. local function OnAddOnLoaded(event, addonName)
  2.     if addonName == myAddon.name then
  3.         myAddon:Initialize()
  4.     end
  5. end
  6. EVENT_MANAGER:RegisterForEvent(ADDON_NAME, EVENT_ADD_ON_LOADED, OnAddOnLoaded)
Yep, that's what I mean: myAddon.name must match the folder and manifest text name. I was using an addon folder name like ddTemp\ and ddTemp.txt, while myAddon.name was "ddMyNewAddon" or something like that. So addonName == "ddTemp", while myAddon.name was "ddMyNewAddon", and ergo the OnAddonLoaded function never made it past the name check.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Important, Simple, Easily Overlooked Load Error


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