View Single Post
  #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