View Single Post
02-05-24, 04:32 PM   #1
Benalish
A Flamescale Wyrmkin
 
Benalish's Avatar
Join Date: Dec 2012
Posts: 123
Dealing with templates and OOP

This is the first time I try to apply OOP in Lua. My goal is to create a template as if it were an object, without using the mixin.

XML code:

Code:
<Frame name="TemplateListViewTemplate" virtual="true">		
	<Scripts>
		<OnLoad>
			self = LibUnitScanListView:New(self)
		</OnLoad>
	</Scripts>
</Frame>
Lua code:

Lua Code:
  1. LibListView = {}
  2.  
  3. function LibListView:New(self)
  4.        
  5.         local listviewmt = {
  6.             buttons  = {},
  7.             data = {},
  8.             --more methods and functions
  9.             SetWidgetScript = function(words) print(words) end,
  10.         }
  11.         setmetatable(self, { __index = setmetatable(listviewmt, getmetatable(self)) })
  12. end

If I write

Lua Code:
  1. self = LibListView:New(self)
inside <OnLoasd> tag the code works fine. At this point, I would like to make sure that the class is created without passing self as a parameter: in short, using the wording
Lua Code:
  1. listview = LibListView:New()
to make the code more elegant.
I tried to not put self in the signature, just leaving it without any params:
Lua Code:
  1. function LibListView:New()
At this point an error emerges:


Lua Code:
  1. listview = CreateFrame("Frame","listview",UIParent,"TemplateListViewTemplate")
  2. listview:SetSize(150, 100)
  3. listview:SetPoint("TOPLEFT",50,-75)
  4. listview:SetWidgetScript("Hello World")
returns this error:

attempt to call method 'SetWidgetScript' (a nil value)

Can you help me?

Last edited by Benalish : 02-05-24 at 05:27 PM.
  Reply With Quote