lua-users home
lua-l archive

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


I used the Lunar template for this. I will try to find it out, but for a newbie this thingie could be a little bit tricky (I guess I have no chance) :)

any hint?

Or would it be better to open a new thread, because this one has a different title and is initiallyl solved?

Best Regards
Micha

Jan de Vos schrieb:
You need the correct metatable to be set on you userdata object.

If you are not using any binding framework (such as tolua++ or luabind),
you might be able to take a look at how the Button:new() function is
implemented. Do the same in your code, and it will work :-)

If you have no idea of how the Button:new() function was implemented,
chances are that its programmer used some binding framework or other.
The answer to your question greatly depends on that, so please try to
find out.


Jan

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Michael Bauroth
Sent: maandag 10 november 2008 15:35
To: Lua list
Subject: Re: Register Lua callback functions from within Lua to C++

Unfortunately this doesn't do the trick :(

Let us reduce the complexity of the problem even more ...

Primary I create an C++ object Button in Lua:

btn = Button:new()
btn.RegisterCallback( callback )

In C++ I store the reference ...

m_CbOnMouseDown = luaL_ref( L, LUA_REGISTRYINDEX );


Later on I want to call a function from C++ in Lua like this:

callback( btn )

where btn should be of type Button again, so that I can call additonal C++ methods on it. I don't use any binding framework at the moment.


// push function
lua_rawgeti( L, LUA_REGISTRYINDEX, m_CbOnMouseDown );

//push parameter(s)
//lua_pushlightuserdata( L, this );
//lua_pushvalue(L, 1);
			
// call function
if ( lua_pcall( L, 1, LUA_MULTRET, 0 ) != 0 )
{
     // error
}

pushlightuserdata has the disadvantage to give me only type free data.
Were should I embed the call to pushvalue instead?

Best Regards
Micha



Peter Cawley schrieb:
It depends on which Lua/C++ binding framework you're using, but I'd guess that a copy of the Lua-ized 'this' pointer will be at
stack index
1, and so you could use lua_pushvalue(L, 1); to push a copy
of it to the
top of the stack.