Blizz metamethods
Hey all,
How can I get a reference to the metatable which is used by all frames created ingame ( which contains Show, SetClampedToScreen, IsObjectType etc etc )? Only reason I ask is because I wouldn't mind adding a few of my own methods. If I can't do that, I'll just add the methods to each frame directly, but it seems daft having to replicate them each time. Cheers for any advice. |
Code:
local metatable = getmetatable(CreateFrame("Frame")) |
That said, there's really no reason to meddle with the Blizzard metatable. It's absolutely trivial to add methods to a frame, and you don't have to "replicate" anything; definte each method once, and just add a pointer to each frame as desired:
Code:
local function DoSomething(frame) |
Quote:
In the above example, the function is replicated and _G["MyButton"..i].DoSomething is not a pointer to the locally defined function 'DoSomething', it's a separate copy of the function. That's what I had meant by 'replicate'. I confirmed this with the following test... Lua Code:
if you then change the locally defined function to print("Goodbye") and call self:TestFunction() again, it'll still output "Hello". Regardless, I've added the methods to the frames directly anyway, although I see no harm what-so-ever in 'meddling' with (ie adding methods to) Blizz's table. |
Perhaps you should use a function to add functions to frames.
For example: Lua Code:
Seeing as you were playing with code here is an example you may have missed. Lua Code:
|
Quote:
It sounds to me like however you were redefining the function in your test was creating a new instance of it. |
ElvUI an Tukui define a "toolkit" using metamethods, i'm however not going to link or share any code because of license issues. LibCandybar also uses a metatable to add functions to a private frametype.
There is also a thread dated from 2014 on wowace.com about metamethods, one of them has an excellent example. |
There's another possible way to add something as default to widgets: the XML templates.
Like this: Code:
<Frame name="myTest" virtual="true"> It's not that elegant as it requires XML and code editing inside XML, but it's easy to use. :D |
Thanks everyone for your advice.
Quote:
Lua Code:
That would indicate that self.func and func both point to the same instance of the same function. But it confuses me a little. Here's my example (and what made me think that it's actually a different instance of the same function: Lua Code:
So surely frame.func doesn't simply point to t.func because if that were the case, it would return an error. Wouldn't that indicate that each frame the method is added to will hold it's own instance of that method? Just to be clear, I'm not in any way trying to insist that what I'm saying is right... to be honest, I'm stumped. I'd always thought that one of the primary benefits of a metatable is to allow numerous objects to refer to the same instance of a method, thereby conserving memory. If I'm looking at this all wrong, please let me know. |
Quote:
|
Quote:
You are assigning nil to t.func, not assigning nil to the actual function stored in memory. Both t.func and frame.func are just pointers to that function. (I could be wrong in any of my statements, someone please correct me in that case.) Using a table as an example where you can see the change in both references (because tables are reference types in Lua): lua Code:
|
Quote:
Just to confirm my understanding. When I write... t.func = nil; ... does lua check to see if the function is referenced by anything else. If it is, it keeps it, if not, it gets wiped from memory by the garbage collector? |
Pretty sure that's how it works. The garbage collector goes through objects and disposes of any that don't have any variables referencing them.
|
Quote:
|
All times are GMT -6. The time now is 01:02 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI