[Lua] Wrap every called class function with another function

Status
Not open for further replies.
Level 15
Joined
Feb 7, 2020
Messages
399
I can't wrap my head around what metamethod to use for this, or how the code would look, after much trial and error:

I'd like to take a table, say "myClass", and wrap every method called from it with another function (that preserves the original function's arguments when used in the wrapper).

e.g.
Lua:
myObj = myClass:new()
myObj:doSomeStuff(someArg)
myObj:doSomeOtherStuff(myArg)
myObj:doSomeMoreStuff(myOtherArg)
-- do a hidden wrap of each i.e. someWrapper(doSomeStuff(someArg)) and so on, without having to WET

If that made sense, do any Lua gurus have an example of this concept available? The online tutorials I tried to follow were a bit unworldly (incrementing random integers, etc.).
 
Last edited:
Maybe i missunderstand what you want or even i'm totally wrong (my lua knowledge is basically limited to the read of the 5.3 manual)

From what i understood, user defined metatables methods are only called if there is no such method available in the parent class.
So i don't think it's possible, else i will read further and learn :p
 
Maybe i missunderstand what you want or even i'm totally wrong (my lua knowledge is basically limited to the read of the 5.3 manual)

From what i understood, user defined metatables methods are only called if there is no such method available in the parent class.
So i don't think it's possible, else i will read further and learn :p
I think I finally found this same answer on a less-opaque tutorialpoint page. I believe you are correct.

:peasant-sad:
 
Status
Not open for further replies.
Back
Top