Thread Tools Display Modes
03/01/14, 03:33 PM   #1
Son of a Sailor
Join Date: Feb 2014
Posts: 7
Help with addon and accessing code feedback

Using what I can find in the wiki, I'm trying to create an addon that puts a message in the chatbox every time a character gains XP. I'm not proficient with Lua or XML but I've programmed for four years now and can muddle my through the wiki. I think I have a proper(though crude) addon, but it doesn't work at all. I looked at some code for other addons and I think I'm doing it right but the lack of tutorials and documentation make it hard to me to find out what I'm doing wrong on my own.

I'd also like to know if there's a way to get console output for addons or access to a debugger. If I could have error returned or be able to view my addon as it runs it would be very helpful

Lua Code:
  1. local function OnXPGain(eventCode, gain, reason)
  2.     --print xp to chat
  3.     d( "XP Gained: " .. gain )
  4. end
  5.  
  6. function OnLoad()
  7.     EVENT_MANAGER:RegisterForEvent(EVENT_EXPERIENCE_GAIN, OnXPGain)
  8.     d( "loaded" )
  9. end

Code:
<GuiXml>
    <Controls>
        <TopLevelControl name="XPTracker">
            -<OnInitialized>
				OnLoad()
			</OnInitialized>
        </TopLevelControl>
    </Controls>
</GuiXml>
  Reply With Quote
03/01/14, 03:43 PM   #2
zork
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 29
I will try to post an example.

*edit*

Here you go:

lua Code:
  1. -----------------------------
  2.   -- zExperinceTest
  3.   -- zork - 2014
  4.   -----------------------------
  5.  
  6.   -----------------------------
  7.   -- VARIABLES
  8.   -----------------------------
  9.  
  10.   local cs = CHAT_SYSTEM
  11.   local em = GetEventManager()
  12.   local addon = ZO_Object:Subclass()
  13.  
  14.   -----------------------------
  15.   -- FUNCTIONS
  16.   -----------------------------
  17.  
  18.   local function p(str)
  19.     cs:AddMessage(str)
  20.   end
  21.  
  22.   --new func
  23.   function addon:New()
  24.     self.addonName = "zExperienceTest"
  25.     self:Initialize()
  26.   end
  27.  
  28.   --init func
  29.   function addon:Initialize()
  30.     --event registering
  31.     em:RegisterForEvent(self.addonName.."_OnUnitCreated",             EVENT_UNIT_CREATED,               function(...) self:OnUnitCreated(...) end)
  32.     em:RegisterForEvent(self.addonName.."_OnExperienceGain",          EVENT_EXPERIENCE_GAIN,            function(...) self:OnExperienceGain(...) end)
  33.     em:RegisterForEvent(self.addonName.."_OnExperienceGainDiscovery", EVENT_EXPERIENCE_GAIN_DISCOVERY,  function(...) self:OnExperienceGainDiscovery(...) end)
  34.     em:RegisterForEvent(self.addonName.."_OnExperienceUpdate",        EVENT_EXPERIENCE_UPDATE,          function(...) self:OnExperienceUpdate(...) end)
  35.     em:RegisterForEvent(self.addonName.."_OnPlayerActivated",         EVENT_PLAYER_ACTIVATED,           function(...) self:OnPlayerActivated(...) end)
  36.   end
  37.  
  38.   --OnPlayerActivated event trigger func
  39.   function addon:PrintExperience()
  40.     local curExp, maxExp, perExp, lvl = GetUnitXP("player"), GetUnitXPMax("player"), GetUnitXP("player")/GetUnitXPMax("player")*100, GetUnitLevel("player")
  41.     p("Level: "..lvl..", Experience: "..curExp.."/"..maxExp.." ("..perExp.."%)")
  42.   end
  43.  
  44.   --OnPlayerActivated event trigger func
  45.   function addon:OnPlayerActivated(...)
  46.     p("OnPlayerActivated")
  47.     self:PrintExperience()
  48.   end
  49.  
  50.   --OnUnitCreated event trigger func
  51.   function addon:OnUnitCreated(...)
  52.     local eventCode, unitTag = ...
  53.     if unitTag ~= "player" then return end
  54.     p("OnUnitCreated")
  55.     self:PrintExperience()
  56.   end
  57.  
  58.   --OnExperienceGain event trigger func
  59.   function addon:OnExperienceGain(...)
  60.     local eventCode, value, reason = ...
  61.     p("OnExperienceGain")
  62.     p(value.." exp gained")
  63.     self:PrintExperience()
  64.   end
  65.  
  66.   --OnExperienceGain event trigger func
  67.   function addon:OnExperienceGainDiscovery(...)
  68.     local eventCode, areaName, value = ...
  69.     p("OnExperienceGainDiscovery")
  70.     p(value.." exp gained for discovering "..areaName)
  71.     self:PrintExperience()
  72.   end
  73.  
  74.   --OnExperienceGain event trigger func
  75.   function addon:OnExperienceUpdate(...)
  76.     local eventCode, unitTag, currentExp, maxExp, reason = ...
  77.     if unitTag ~= "player" then return end
  78.     p("OnExperienceUpdate")
  79.     self:PrintExperience()
  80.   end
  81.  
  82.   -----------------------------
  83.   -- CALL
  84.   -----------------------------
  85.  
  86.   addon:New()

http://code.google.com/p/zork-esoui/...rienceTest.lua

On that note. The events are just to trigger. You get your XP via this:

lua Code:
  1. local curExp, maxExp = GetUnitXP("player"), GetUnitXPMax("player")

I will add that to the code.

*edit2*

Code is updated.

Result:

Last edited by zork : 03/01/14 at 04:32 PM.
  Reply With Quote
03/01/14, 04:52 PM   #3
jgm
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 7
So what's the benefit of going through the chat system rather than just using d() for console output?
  Reply With Quote
03/01/14, 05:07 PM   #4
Son of a Sailor
Join Date: Feb 2014
Posts: 7
Thanks! That code is beyond the scope of my Lua knowledge, so I'm doing to do my best to use it to piece together an addon that works instead of copy pasting. I will keep it bookmarked as a reference for if I ever turn this into a more professional addon. This is mostly for self education and not producing a useful addon since I imagine there's already addons that do this and more. The only thing that confuses me is using inheritance and placing "addon:" before function names. I examined a couple other addons and didn't see that. Is there any major advantage to that? If not, I'd rather not mess with it.
  Reply With Quote
03/01/14, 05:28 PM   #5
zork
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 29
@jqm
Nothing. But d() has conditions for emitting a table that one does not need here. When you call d() with a string it will call EmitMessage which does nothing but CHAT_SYSTEM:AddMessage(text). Which is what I do aswell.

@Son of a Sailor
I don't want to leak to many functions or variable to the global namespace. So first of all addon is a local object. Secondly everything I do should be encapsuled in my addon object. That is why I define functions on my addon object and not just local functions. But you could do that with local functions only aswell. There are different ways to reach the same goal.

Last edited by zork : 03/01/14 at 05:51 PM.
  Reply With Quote
03/01/14, 07:58 PM   #6
Son of a Sailor
Join Date: Feb 2014
Posts: 7
Can everything be done in Lua or is a call from the XML needed? I tried copying your Lua to have a control that I know works and it does not work either. (Tested by killing mudcrabs) Or something may be wrong with my client
  Reply With Quote
03/02/14, 05:40 AM   #7
zork
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 29
Check:
http://www.esoui.com/downloads/info3...ienceTest.html

If you are overleveled and get zero xp no event will fire.
  Reply With Quote
03/02/14, 03:04 PM   #8
Son of a Sailor
Join Date: Feb 2014
Posts: 7
If I download that as its own addon it works perfectly, but if I copy the Lua file into my own addon it doesn't work. I may have completely gooned it and it's time to start over and start from a fresh addon

Edit: Well I'm dumb. I kept working on a Lua file that wasn't in the right directory. I didn't pay attention to the file path in Notepad++ and had the right one open while continually working on the wrong one. Now I'm getting error messages! Which is normally a bad thing but I'm just happy to get feedback from an addon I made

Last edited by Son of a Sailor : 03/02/14 at 03:24 PM.
  Reply With Quote
03/02/14, 03:25 PM   #9
zork
 
zork's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 29
Lol you will not believe how often that happens.
  Reply With Quote
03/02/14, 04:21 PM   #10
Son of a Sailor
Join Date: Feb 2014
Posts: 7
Hmm, still having issues. I may just be doing something dumb again, but for now I'll just enjoy the rest of the beta and try again later. Once we get closer to release hopefully there'll be more documentation and tutorials.

Lua Code:
  1. local cs = CHAT_SYSTEM
  2. local em = GetEventManager()
  3.  
  4. local function p(str)
  5.     cs:AddMessage(str)
  6. end
  7.  
  8. local function New()
  9.     local addonName = "XPTracker"
  10.     OnLoad()
  11. end
  12.    
  13. local function OnLoad()
  14.     em:RegisterForEvent(EVENT_EXPERIENCE_GAIN, function(...) OnXPGain(...) end)
  15.    
  16. end
  17.  
  18. local function OnXPGain(...)
  19.     local eventCode, value, reason = ...
  20.     --print xp to chat
  21.     p("printing")
  22.     p( "!!!XP Gained: "..value )
  23. end
  24.  
  25. New()
  Reply With Quote
03/02/14, 04:38 PM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
Change this line
Lua Code:
  1. em:RegisterForEvent(EVENT_EXPERIENCE_GAIN, function(...) OnXPGain(...) end)

to this
Lua Code:
  1. em:RegisterForEvent("XPTracker", EVENT_EXPERIENCE_GAIN, function(...) OnXPGain(...) end)

/edit: because you're registering your event using the event manager and not on your addon object itself.
  Reply With Quote
03/03/14, 12:05 PM   #12
Son of a Sailor
Join Date: Feb 2014
Posts: 7
Well I never got it to work. I'll just try again next beta test/release. Hopefully by then there's be better documentation and a decent tutorial or two. Right now the wiki doesn't have much more than the names of functions and events.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » Help with addon and accessing code feedback


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