Thread Tools Display Modes
04/06/14, 09:35 AM   #1
Yazilliclick
Join Date: Apr 2014
Posts: 6
Lua Error Help First Addon

Hey,

Trying my first addon and getting an error I haven't been able to solve. Probably something simple and stupid.

user:/AddOns/YazClock/YazClock.lua:13: function expected instead of nil
stack traceback:
user:/AddOns/YazClock/YazClock.lua:13: in function 'yc:OnAddOnLoaded'
user:/AddOns/YazClock/YazClock.lua:17: in function '(anonymous)'
YazClock.lua
Lua Code:
  1. local yc = ZO_Object:Subclass()
  2. local eventManager = GetEventManager()
  3.  
  4. function yc:New()
  5.     yc.AddOnName = "YazClock"
  6.     yc.AddOnEventManager()
  7. end
  8.  
  9. function yc:OnUpdate() 
  10. end
  11.  
  12. function yc:OnAddOnLoaded(eventCode, addOnName)
  13.     yc:SetHandler("OnUpdate", function(...) yc:OnUpdate(...) end)
  14. end
  15.  
  16. function yc:AddOnEventManager()
  17.     eventManager:RegisterForEvent(yc.AddOnName .. "_AddOnLoaded", EVENT_ADD_ON_LOADED, function(...) yc:OnAddOnLoaded(...) end)
  18. end
  19.  
  20. yc:New()

YazClock.xml
Code:
<GuiXml>
	<Controls>
		<TopLevelControl name="YazClock" mouseEnabled="true" movable="true">
			<Dimensions x="200" y="42" />
			<Anchor point="CENTER" />
			<Controls>				
				<Label name="$(parent)TimeDisplay" font="ZoFontGame" color="CFDCBD" wrapmode="ELLIPSIS" verticalalignment="CENTER" text="Counter: ">
					<AnchorFill />
				</Label>
			</Controls>
		</TopLevelControl>
	</Controls>
</GuiXml>
Any help appreciated. If somebody wanted to give hints or code towards moving control creation into the lua and saving position between loads as those are my next steps that'd be great but I think I can figure that out eventually
  Reply With Quote
04/06/14, 11:00 AM   #2
Iyanga
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 183
And yc:SetHandler does exist?
  Reply With Quote
04/06/14, 11:32 AM   #3
Yazilliclick
Join Date: Apr 2014
Posts: 6
Thanks Iyanga for pointing out my stupidity I knew it was something simple and silly that I was missing.

For others I added code to create a mainwindow in lua, got rid of XML and then set the handler on the mainwindow and it works.

Lua Code:
  1. local yc = ZO_Object:Subclass()
  2. local eventManager = GetEventManager()
  3. local windowManager = GetWindowManager()
  4.  
  5. function yc:New()
  6.     yc.AddOnName = "YazClock"
  7.     yc.AddOnEventManager()
  8. end
  9.  
  10. function yc:OnUpdate() 
  11. end
  12.  
  13. function yc:OnAddOnLoaded(eventCode, addOnName)
  14.  
  15.     d("test")
  16.     if (yc.MainWindow == nil) then 
  17.         yc.MainWindow = windowManager:CreateTopLevelWindow(yc.AddOnName .. "_MainWindow")
  18.         yc.MainWindow:SetDimensions(200, 42)
  19.         yc.MainWindow:SetAnchor(CENTER, GuiRoot, TOPLEFT, 0, 0)
  20.         yc.MainWindow:SetHandler("OnUpdate", OnUpdate)
  21.  
  22.         yc.MainWindow.TimeLabel = windowManager:CreateControl(yc.AddOnName .. "_TimeLabel", yc.MainWindow, CT_LABEL)
  23.         yc.MainWindow.TimeLabel:SetFont("ZoFontAnnounceMessage")
  24.         yc.MainWindow.TimeLabel:SetDimensions(200, 42)
  25.         yc.MainWindow.TimeLabel:SetText("Test")
  26.     end
  27. end
  28.  
  29. function yc:AddOnEventManager()
  30.     eventManager:RegisterForEvent(yc.AddOnName .. "_AddOnLoaded", EVENT_ADD_ON_LOADED, function(...) yc:OnAddOnLoaded(...) end)
  31. end
  32.  
  33. yc:New()
  Reply With Quote
04/06/14, 11:59 AM   #4
Iyanga
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 183
As a side note:
Is there a reason why you wrap an anonymous function around yc:OnAddOnLoaded in line 30?
  Reply With Quote
04/06/14, 03:47 PM   #5
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
To clarify why it wasn't working, in your first code example (in the first post), yc is not a control/object. Script handlers can only be set on controls.
  Reply With Quote
04/06/14, 05:33 PM   #6
Yazilliclick
Join Date: Apr 2014
Posts: 6
Originally Posted by Iyanga View Post
As a side note:
Is there a reason why you wrap an anonymous function around yc:OnAddOnLoaded in line 30?
I'm using another addon as an example right now just to try and get this working. That's where that comes from.

I've got a new problem now in that even though I'm setting my handler for OnUpdate it's not being called afterwards. Code below, have bit ugly since I've been editing it back and forth while debugging.

Lua Code:
  1. local yc = ZO_Object:Subclass()
  2. local eventManager = GetEventManager()
  3. local windowManager = GetWindowManager()
  4.  
  5. function yc:New()
  6.     yc.AddOnName = "YazClock"
  7.     yc.AddOnEventManager()
  8. end
  9.  
  10. function yc:OnUpdate() 
  11.     CHAT_SYSTEM:AddMessage("On UPdate")
  12.     yc.MainWindow.TimeLabel:SetText(GetDateStringFromTimestamp(GetTimeStamp()) .. " " .. GetTimeString())
  13. end
  14.  
  15. function yc:OnAddOnLoaded(eventCode, addOnName)
  16.  
  17.     CHAT_SYSTEM:AddMessage("OnLoaded")
  18.    
  19. end
  20.  
  21. function yc:PlayerActivated()
  22.     CHAT_SYSTEM:AddMessage("Test")
  23.  
  24.     if (yc.MainWindow == nil) then 
  25.         yc.MainWindow = windowManager:CreateTopLevelWindow(yc.AddOnName .. "_MainWindow")
  26.         yc.MainWindow:SetDimensions(200, 42)
  27.         yc.MainWindow:SetAnchor(CENTER, GuiRoot, TOPLEFT, 0, 0)
  28.         CHAT_SYSTEM:AddMessage("Setting handler")
  29.        
  30.  
  31.         yc.MainWindow.TimeLabel = windowManager:CreateControl(yc.AddOnName .. "_TimeLabel", yc.MainWindow, CT_LABEL)
  32.         yc.MainWindow.TimeLabel:SetFont("ZoFontAnnounceMessage")
  33.         yc.MainWindow.TimeLabel:SetDimensions(200, 42)
  34.         yc.MainWindow.TimeLabel:SetText("Test")
  35.         yc.MainWindow.TimeLabel:SetHandler("OnUpdate", function(self, timerun)
  36.             CHAT_SYSTEM:AddMessage("OnUpdate Fired")
  37.             end)
  38.     end
  39. end
  40.  
  41. function yc:AddOnEventManager()
  42.     CHAT_SYSTEM:AddMessage("Test2")
  43.     eventManager:RegisterForEvent(yc.AddOnName .. "_AddOnLoaded", EVENT_ADD_ON_LOADED, function(...) yc:OnAddOnLoaded(...) end)
  44.     eventManager:RegisterForEvent(yc.AddOnName .. "_PlayerActivated", EVENT_PLAYER_ACTIVATED, function(...) yc.PlayerActivated(...) end)
  45. end
  46.  
  47. yc:New()

I've had the SetHandler also set to:
lua Code:
  1. yc.MainWindow.TimeLabel:SetHandler("OnUpdate", OnUpdate)
  2.  
  3. -- and
  4.  
  5. yc.MainWindow.TimeLabel:SetHandler("OnUpdate", function(...) yc:OnUpdate(...) end)

Originally Posted by Seerah View Post
To clarify why it wasn't working, in your first code example (in the first post), yc is not a control/object. Script handlers can only be set on controls.
Yeah thanks Seerah for wording it better!

Last edited by Yazilliclick : 04/06/14 at 06:16 PM.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Lua Error Help First Addon


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