lua-users home
lua-l archive

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


Hi, me once more. Have found out, that the code works very well (another error confused me here). The remaining question is, how to pass a additional parameter (in my special case the sender object), when I trigger the callback?

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

// push parameter(s)
lua_rawgeti( L, LUA_REGISTRYINDEX, this );
// XXX doesn't work, needs an integer here  :(

// call function
if ( lua_pcall( L, 1, LUA_MULTRET, 0 ) != 0 )
{
    TRACE( _T( "Error calling LUA callback OnMouseDown\n" ) );
}

Best Regards
Micha

Michael Bauroth schrieb:
Hi,

normally Google is my friend, but this time I searched several hours but without luck. What I'm looking for is a way, in which I can register from Lua side a callback to C++ as a pointer like this:

Lua:

function OnMouseDown( btn )
    -- do something
end

function createButton()
    btn = Button:new()
    btn:RegisterCallback( OnMouseDown )
    return btn
end;

Button b1 = createButton()


Button is a C++ class accessible from Lua. The class has a method RegisterCallback(), which should take the Lua function pointer and store it internally for later use. What I've found was something like this (doesn't work til now):

int FButton::RegisterCallback( lua_State *L )
{
    m_CbOnMouseDown = luaL_ref( L, LUA_REGISTRYINDEX );

    return 0;
}

What I'm doing wrong?

Any help is higly appreciated.

Thanx in advance.
Micha