Hi all,
I’m writing some lua script for some game software which supplies me a userdata object (or lightuserdata -> I’m not responsible for the code, am not really proficient in C++ and only hazily aware of the difference between lightuserdata and userdata, should this be relevant). I wish to write a wrapper for this object in lua where the wrapper object inherits from the game object. I want to be able to override certain functions on the game object with the wrapper but let other calls that I don’t wish to override pass through:-
game = game_class:new()
game_wrapper = {}
game_wrapper_mt = {__index = game}
function game_wrapper:new()
local retval = {}
setmetatable(retval, game_wrapper_mt)
return retval
end
my_game = game_wrapper:new()
my_game:out("This text should appear in the debug console like as if I’d called game:out()!")
However the above script snippet doesn’t work, throwing the following error:-
LUA script error:[string "Test_Libs_Start.lua"]:44: calling 'out' on bad self (game_class expected, got table)
The problem seems to happen when lua resolves the syntactic sugar of my_game:out(string) into my_game.out(my_game, string). Once it’s worked everything out Lua seems to be seeing the last line as:-
game:out(my_game, "This text should appear in the debug console like as if I’d called game:out()!")
If I use the line below it works, but the call is ugly:-
my_game.out(game, "This text should appear in the debug console like as if I’d called game:out()!")
I suspect that what I’m trying to do is either impossible or something to do with the setup of the object supplied by the game code, which I can get changed, but I’m out of my depth with C++ and would appreciate any insights.
Regards,
Stephen