lua-users home
lua-l archive

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


On Sun, Mar 19, 2006 at 12:41:05PM +0100, Boguslaw Dziewierz wrote:
> Hi,

Hi.

> I've got a small problem concerning Lua 5.1 and toLua++.
> 
> I've set up my CLogicCore class to start up it's own lua state in the
> constructor and then evaluate the following LUA function passing
> itself (the CLogicCore object) as an argument:
> 
> function on_init_game (core_object)
>     new_map = CLogicMap:new()
>     core_object:addMap(new_map)
> end
> 
> I use the following C++ code to pass CLogicCore object to
> abovementioned Lua function:
> 
> [...]
> 
> void CLogicCore::init_game () {
>     lua_getglobal(this->lua_state, "on_init_game");
>     lua_pushlightuserdata(this->lua_state, (void*) this);
>     lua_call(this->lua_state, 1, 0);
> }
> 
> [...]
> 
> Of course I've created proper toLua++ bindings before and I am able to
> create and use my C++ objects in Lua (i.e. new_map = CLogicMap:new()
> works perfectly okay, also all methods of new_map do work). The
> problem is, that I can't use objects created in C++ and passed to Lua
> functions as arguments (i.e. CLogicCore object)! I get "attempt to
> index  local 'core_object' (a userdata value)" error.
> 
> What is wrong?

The object you're passing is a lightuserdata, which means it has not
'type', and no metatable in lua. You need to use tolua_pushusertype (check
the tolua generated code for usage examples) to push your object with a
type, or you can use tolua.cast inside lua to cast the light userdata to a
full userdata object with type.

> 
> Regards,
> B.

Ariel.