Thread Tools Display Modes
08-04-08, 05:56 PM   #1
kerrang
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2006
Posts: 109
getmetatable - confuses me...

I was looking through the OmniCC addon to find out how it hooks into the Cooldown functionality of Action Buttons.

I understand the theory - but the way it does it is either VERY clever or VERY obscure - I was hoping someone might explain it

The code is roughly

local methods = getmetatable(CreateFrame('Cooldown', nil, nil, 'CooldownFrameTemplate')).__index
hooksecurefunc(methods, 'SetCooldown', function(self, start, duration)
[function content here]
end)

What on EARTH is that getmetatable function returning - apart from 'a list of all the action buttons in the entire UI' - and how - what - where - who - when etc.
  Reply With Quote
08-04-08, 06:11 PM   #2
zariel
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 7
tables can have metatables.

getmetatable will return a table, usualy containg the keys, __index, __newindex, __call, __tostring, __metatable.

What its doing is creating a button, getting the metatable and then __index key which is also a table.

Frames work by using metatables as a kind of prototype to setup shared functions.

Metatables are discussed here in the pil manual http://www.lua.org/pil/13.html
  Reply With Quote
08-04-08, 07:31 PM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Originally Posted by kerrang View Post
local methods = getmetatable(CreateFrame('Cooldown', nil, nil, 'CooldownFrameTemplate')).__index
hooksecurefunc(methods, 'SetCooldown', function(self, start, duration)
[function content here]
end)
(edit: maybe you understand this and just wanted to know what metatables are...in that case see above :P)

doing this out at greater lenght might be useful:

local f = CreateFrame('Cooldown', nil, nil, 'CooldownFrameTemplate')
local mTable = getmetatable(f)
local methods = mTable.__index

hooksecurefunc is then hooking the function SetCooldown within methods

Last edited by Akryn : 08-04-08 at 07:36 PM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » getmetatable - confuses me...


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