Thread Tools Display Modes
07/17/14, 03:00 PM   #1
FadedJeans
 
FadedJeans's Avatar
Join Date: Apr 2014
Posts: 6
Hello World

I tried following the directions here:

http://wiki.esoui.com/Writing_your_first_addon

...just up to the point where I expect to see messages in the chat tab, but I don't. i don't get any errors, I see the "add on" listed, and it is checked off to be used, but it doesn't seem to do anything at all.

What's the best way to troubleshoot this little "Hello World" project? I don't see any logs produced. Are there dev tools to see what is going on?

Thanks! Looking forward to getting up to speed and joining the community. I've never written Lua before, but I've been writing ColdFusion applications for over a decade. Not the same thing, I know, but hopefully I can make the jump

I love the game and plan on staying for a long time.

Lua Code:
  1. -- First, we create a namespace for our addon by declaring a top-level table that will hold everything else.
  2. FooAddon = {}
  3.  
  4. -- This isn't strictly necessary, but we'll use this string later when registering events.
  5. -- Better to define it in a single place rather than retyping the same string.
  6. FooAddon.name = "FooAddon"
  7.  
  8. -- Next we create a function that will initialize our addon
  9. function FooAddon:Initialize()
  10.   self.inCombat = IsUnitInCombat("player")
  11.  
  12.   EVENT_MANAGER:RegisterForEvent(self.name, EVENT_PLAYER_COMBAT_STATE, self.OnPlayerCombatState)
  13. end
  14.  
  15. -- Then we create an event handler function which will be called when the "addon loaded" event
  16. -- occurs. We'll use this to initialize our addon after all of its resources are fully loaded.
  17. function FooAddon.OnAddOnLoaded(event, addonName)
  18.   -- The event fires each time *any* addon loads - but we only care about when our own addon loads.
  19.   if addonName == FooAddon.name then
  20.     FooAddon:Initialize()
  21.   end
  22. end
  23.  
  24. function FooAddon.OnPlayerCombatState(event, inCombat)
  25.   -- The ~= operator is "not equal to" in Lua.
  26.   if inCombat ~= FooAddon.inCombat then
  27.     -- The player's state has changed. Update the stored state...
  28.     FooAddon.inCombat = inCombat
  29.  
  30.     -- ...and then announce the change.
  31.     if inCombat then
  32.       d("Entering combat.")
  33.     else
  34.       d("Exiting combat.")
  35.     end
  36.  
  37.   end
  38. end
  39.  
  40. -- Finally, we'll register our event handler function to be called when the proper event occurs.
  41. EVENT_MANAGER:RegisterForEvent(FooAddon.name, EVENT_ADD_ON_LOADED, FooAddon.OnAddOnLoaded)

EDITED: I went ahead and added the syntax highlight, thanks for the advice! ; I feel extra lost I suppose, I don't see any syntax tools, just smilies.

Last edited by FadedJeans : 07/17/14 at 03:34 PM.
  Reply With Quote
07/17/14, 03:24 PM   #2
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
Please use highlight="Lua" for code blocks, there's a blue Lua icon that wraps it for you. Couldn't find anything wrong with the code, other that absent formatting
  Reply With Quote
07/17/14, 03:35 PM   #3
FadedJeans
 
FadedJeans's Avatar
Join Date: Apr 2014
Posts: 6
Perhaps my expectations are off? I log in, make sure that the addon is checked off, then run off to get in combat and don't see any messages.
  Reply With Quote
07/17/14, 03:44 PM   #4
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Just a simple question: What is the name of your addon?
EVENT_ADD_ON_LOADED uses what AddOnManager calls "addOnFileName", it means that you have to make sure that variable FooAddon.name contains name of your addon manifest (.txt file) without extension.

Last edited by Garkin : 07/17/14 at 07:11 PM.
  Reply With Quote
07/17/14, 03:48 PM   #5
zgrssd
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 280
Avoid using global variables or functions as far as humanly possible. Global in lua means shared between all addons and Zeniamx code. You never know who else is using just that name and wheter they load before you addon or after.
If you are writing a dedicated library, use LibStub to expose the functions. When I need unique strings (for Event registering or timers) I prefer to generate a unique ID - the string reprensentation of the MD5 Hash of the Addons name.

In fact you should propably just take out the whole table/namespace syntax for the time being. Work with local functions and variables for your first tests.
No point making your own live hard. Try this code for comparision:

Lua Code:
  1. -- This isn't strictly necessary, but we'll use this string later when registering events.
  2.     -- Better to define it in a single place rather than retyping the same string.
  3.     local name = "FooAddon"
  4.  
  5.     local inCombat
  6.      
  7.     -- Next we create a function that will initialize our addon
  8.     local function Initialize()
  9.       inCombat = IsUnitInCombat("player")
  10.      
  11.       EVENT_MANAGER:RegisterForEvent(name, EVENT_PLAYER_COMBAT_STATE, OnPlayerCombatState)
  12.     end
  13.      
  14.     -- Then we create an event handler function which will be called when the "addon loaded" event
  15.     -- occurs. We'll use this to initialize our addon after all of its resources are fully loaded.
  16.     local function OnAddOnLoaded(event, addonName)
  17.       -- The event fires each time *any* addon loads - but we only care about when our own addon loads.
  18.       if addonName == FooAddon.name then
  19.         Initialize()
  20.       end
  21.     end
  22.      
  23.     local function OnPlayerCombatState(event, eventInCombat)
  24.       -- The ~= operator is "not equal to" in Lua.
  25.       if eventInCombat ~= inCombat then
  26.         -- The player's state has changed. Update the stored state...
  27.         inCombat = eventInCombat
  28.      
  29.         -- ...and then announce the change.
  30.         if inCombat then
  31.           d("Entering combat.")
  32.         else
  33.           d("Exiting combat.")
  34.         end
  35.      
  36.       end
  37.     end
  38.      
  39.     -- Finally, we'll register our event handler function to be called when the proper event occurs.
  40.     EVENT_MANAGER:RegisterForEvent(name .. "_LoadedEvent", EVENT_ADD_ON_LOADED, OnAddOnLoaded)
Are you certain you ever even enter the if block in the Loaded Event?
If you need messages from that early, isntall BugEater and enabelked Pre-Init debug. Some d() messages in teh code and onLoaded can really help nail the issues down. But you need BugEaters Pre Init debug (plus an entry as optional dependency) to see those.
  Reply With Quote
07/17/14, 03:50 PM   #6
FadedJeans
 
FadedJeans's Avatar
Join Date: Apr 2014
Posts: 6
...and that was it (garkin). Thanks!

Also, thanks for the advice zgrssd!

Now to start beating my head against this until I get a feel for it. Much to learn and unlearn.

Thanks again!
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Hello World


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