Metatables. Add functionality into metamethod without overriding
6 replies, posted
Hello.
Is there way to extend Panel's metatable methods like:
PANEL:OnCursorEntered
PANEL:OnCursorExited
Without loosing initial functionality. (Kinda like adding event listeners in JS)
For example I have a panel that highlights on CursorEntered.
But I want to create custom tooltip meta method like:
[CODE]
local meta = FindMetaTable("Panel")
function meta:CustomTip(str)
self.OnCursorEntered = function() -- Here we override the method and it loses it's functionality and panel no more highlights itself :(
-- Show custom tooltip
end
self.OnCursorExited = function()
-- Hife custom tooltip
end
end
[/CODE]
Detours.
[code]
mt._Function = mt.Function
function mt:Function()
-- code here
mt:_function()
end
[/code]
[QUOTE=StonedPenguin;51094837]Detours.
mt._Function = mt.Functionfunction mt:Function() -- code here mt:_function()end[/QUOTE]
Yes, but it's a little bit hacky way.
[QUOTE=StonedPenguin;51094837]Detours.
[code]
mt._Function = mt.Function
function mt:Function()
-- code here
mt:_function()
end
[/code][/QUOTE]
Popping out of the blue here a little bit, but what is the nature of detours? I've never used them and have no idea what that code does, it seems really useful.
[QUOTE=VIoxtar;51094955]Popping out of the blue here a little bit, but what are detours? I've never used them and have no idea what that code does, it seems really useful.[/QUOTE]
The code does exactly what it looks like it does, it saves the current function and then overwrites it.
Just like if I want to know whenever something printed shit
[code]
local old_print = print
function print(...)
-- Print was called, do our stuff here
old_print(...) -- Call old print to retain functionality
end
[/code]
ninja'd
[QUOTE=Koji6ac9H;51094845]Yes, but it's a little bit hacky way.[/QUOTE]
vanilla gmod code does this in quite a few spots, i wouldn't worry too much about it being hacky. if you really must have a cleaner syntax, you'd have to implement a hooking system for your panel yourself.
Sorry, you need to Log In to post a reply to this thread.