lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2012/6/15 Stephen Virgo <stephen@creative-assembly.com>:

>
> 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).

A full userdata can have its own metatable, but there's only one
metatable for all light userdata.  Lua manages the storage for a full
userdata, the app does it for a light userdata.

> 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:-

That means a full userdata unless there is only one kind of userdata.

> 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)

> 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()!")
>

The immediate problem can be solved thus:

game_wrapper.out = function(self,...) return game:out(...) end

But using object-oriented calls is rather pointless if you are going to
throw away the implied `self`.  So I'd rather do it this way:

game_wrapper.out = function(...) return game:out(...) end

and call it as

my_game.out("This text should appear in the debug console like as if
> I’d called game:out()!")