Is there a specific speed increase one way over the other and how does WoW/Lua handle the creation of frames using a metatable? Like, they do exactly the same thing, but does one require less memory or execution time or is one faster in completing execution?
When you're defining the metatable, there's __index = function(t, i). Is t a reference to the table that's created or is it a reference to the base object. (From brief reading of metatables in the Lua-man, I see that __index is a metamethod that is basically a catch-all) For example, say I wanted to make a series of new frames that all share some common properties.
I'd define a metatable (using setmetatable) giving it all the stuff you included in your example, letting me make a bunch of frames easily by indexing the metatable. Now, say I wanted to define methods on the created frames, such as returning a custom value contained in the frame. So I would add something like this?
Code:
local buttons = setmetatable({}, { __index = function(t, i)
local button = CreateFrame("Button", nil, UIParent)
button.text = button:CreateFontString(nil, "OVERLAY")
button.icon = button:CreateTexture(nil, "OVERLAY")
-- Set up scripts, regions, and other features
-- that are shared by all buttons here.
function button:GetIconTexture()
return self.icon:GetTexture()
end
if i > 1 then
-- Not the first button; attach to previous button
button:SetPoint("TOP", buttons[i-1], "BOTTOM")
else
-- First button; no previous button to attach to
button:SetPoint("TOP", UIParent, "CENTER", 0, 200)
end
end })
for i = 1, 10 do
-- This will create 10 buttons, even though you never explicitly call any functions to create them.
local button = buttons[i]
button.text:SetText("Button #"..i)
button.icon:SetTexture("Interface\\Icons\\INV_Misc_QuestionMark")
print(button:GetIconTexture()) -- prints "Interface\\Icons\\INV_MISC_QuestionMark"
end
So adding that would allow me to hide some commonly used implementation to clean up other sections of code, or am I wrong and misunderstanding something?