• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

[Lua] Wrap every called class function with another function

Status
Not open for further replies.
Level 14
Joined
Feb 7, 2020
Messages
387
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:
Level 17
Joined
Apr 27, 2008
Messages
2,455
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
 
Level 14
Joined
Feb 7, 2020
Messages
387
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.
Top