View Single Post
03-04-23, 09:11 PM   #12
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
The problem of templates is the template "class" you create doesn't exist as an object accessible by Lua. You need to create an OnLoad handler and register it for your template to call when instantiated. At that time, your handler is passed a reference to the instanced frame to act on.

What a mixin does is take an existing table and copy its contents into the instantiated frame's table, making every function defined by the mixin available immediately as a method of the frame.

Lua Code:
  1. MyTemplateMixin={};
  2.  
  3. function MyTemplateMixin:Init()
  4.     print(self:GetName());
  5. end

Code:
<Frame name="MyTemplate" mixin="MyTemplateMixin" virtual="true">
	<Scripts>
		<OnLoad method="Init"/>
	</Scripts>
</Frame>

<Frame name="MyTemplateFrame1" inherits="MyTemplate"/>
In this example, MyTemplate is defined as a template that loads MyTemplateMixin when instantiated. This function just prints the frame's name in chat as a test. A frame is then created using the template and is named MyTemplateFrame1. :Init() is then called as defined in the template with a reference to the newly created frame. This causes :Init() to print "MyTemplateFrame1" as that is this new frame's name.



Using metatables for templates has never been a thing. Historically, people have defined global functions and had frames call them. Even that was short-lived once the ability to create frames from Lua was implemented. XML has always been a pain for debugging and is rarely used anymore.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote