lotrointerface.com
Search Downloads


Go Back   LoTROInterface > LotRO > Developer Discussions > Lua Programming Help (L)

Reply
Thread Tools Display Modes
  #1  
Unread 11-17-2013, 01:06 PM
Carentil's Avatar
Carentil Carentil is offline
The Undefeated
Interface Author - Click to view interfaces
 
Join Date: Nov 2013
Posts: 5
Binding events to classes

Hiya guys, sorry if this is a stupid question but I've yet to figure it out and thought perhaps someone might be able to assist.

I'm using Turbine's Class.lua to create window classes and I'd like to add functions to the Constructor so that when a window is instantiated, it already has functions bound to events.

Here's a short example (edited down to weed out some unimportant bits):

Code:
local myWindowClass=class(Turbine.UI.Lotro.Window());
	function myWindowClass:Constructor()
		Turbine.UI.Lotro.Window:Constructor(self);
		self:SetSize(300,400);
		self:SetPosition(30,Turbine.UI.Display:GetHeight()/2-100);
		self:SetText("My Window");
		-- A Label
		self.myLabel = Turbine.UI.Label();
		self.myLabel:SetParent(self);
		self.myLabel:SetText("The Button Has Not Been Clicked");
		-- A Button
		self.myButton = Turbine.UI.Lotro.Button();
		self.myButton:SetParent(self);
		self.myButton:SetText("Do Something");

		-- Define MouseUp handler for myButton
		function self.myButton:MouseUp(sender,args)
			self.myLabel:SetText("The button was clicked");
		end
	end

	local myWindowInstance = myWindowClass();
The window gets created and displayed but when I click the button I get an error saying "attempt to index field 'myLabel' (a nil value)".

I have a feeling this has something to do with a lack of knowledge of scoping, but I'm a bit stymied. Specifically, how do I address the instance of "myLabel" from within the function? I was assuming that self.myButton:MouseUp() was passing in self as the first argument automatically, as I'd read.

Could someone please point me in the right direction? The "real" purpose is to create windows that are constructed along with their own resizing handler functions.

Thanks in advance!

Last edited by Carentil : 11-17-2013 at 01:52 PM.
Reply With Quote
  #2  
Unread 11-17-2013, 02:42 PM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Oct 2010
Posts: 340
OK. I actually missed the biggest problem for several minutes. You put parentheses after the base class name when using the class statement but that winds up calling the base class constructor and passing the result of that call, an instance of the class, instead of the class itself.
Code:
myWindowClass=class(Turbine.UI.Lotro.Window());
should be
Code:
myWindowClass=class(Turbine.UI.Lotro.Window);
Next, you need to use the period notation when calling the base class constructor
Code:
Turbine.UI.Lotro.Window:Constructor(self);
should be
Code:
Turbine.UI.Lotro.Window.Constructor(self);
That will eliminate the error you are getting. You probably don't want either the class or the instance declared as local but that is a separate issue and would depend on how they are being used.

If you haven't already checked it out, the thread, https://www.lotro.com/forums/showthr...gins-for-Noobs has two posts, "Function calling" and "Event Handling" which cover handling events as well as firing and handling custom events.

Last edited by Garan : 11-17-2013 at 02:48 PM.
Reply With Quote
  #3  
Unread 11-17-2013, 04:27 PM
Carentil's Avatar
Carentil Carentil is offline
The Undefeated
Interface Author - Click to view interfaces
 
Join Date: Nov 2013
Posts: 5
Thank you so much Garan! I got a bit farther and also realized I had another issue in my actual code, which this helped me to solve.

I was doing this:

Code:
self.myCheckbox:CheckedChanged = function(sender,args)
	-- toggle enabled flag of a field based upon a checkbox
	self.myTextBox:SetEnabled(self.myCheckBox:IsChecked())
end
...and getting an error still, but changed that to

Code:
self.myCheckbox.CheckedChanged = function(sender,args)
...and everything works as desired.

I've been relying on your Plugins for noobs post heavily, thanks. It's what's gotten me as far as I have. I'm an old hand programmer but Lua is entirely new to me, so I think a lot of the problems I have had and will have with syntax are just a matter of experience. Thank you again for the help!
Reply With Quote
  #4  
Unread 11-17-2013, 06:00 PM
Garan's Avatar
Garan Garan is offline
The Undying
Interface Author - Click to view interfaces
 
Join Date: Oct 2010
Posts: 340
You're welcome. Lua is a fun language but it's syntactic flexibility (often several ways to write the same expressions) can take a bit of getting used to. I depended heavily on the Lua 5.1 Reference Manual from lua.org when learning and I still have to go back there sometimes when proper syntax eludes me:
http://www.lua.org/manual/5.1/
Reply With Quote
  #5  
Unread 11-18-2013, 08:19 AM
Carentil's Avatar
Carentil Carentil is offline
The Undefeated
Interface Author - Click to view interfaces
 
Join Date: Nov 2013
Posts: 5
Thanks! I have the PRM in both ebook form and using the online version. Can't do without them!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Classes issue SanDBoX Lua Programming Help (L) 7 10-27-2011 10:30 AM
TextBox FocusGained() / FocusLost() events not implemented? Betsiel LotRO Wish List (L) 3 10-06-2010 09:44 PM


All times are GMT -5. The time now is 02:26 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui