lua-users home
lua-l archive

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


Hi Jerome,

thank you for your response. Your solution was also my first attempt. Unfortunately I receive an error while execution:

\phone.lua:34: attempt to index local 'btn' (a userdata value)

The pointer is correct, I checked this (C++ object created within Lua).
Here is a small piece of Lua code:

function createButton( parent, x, y, callback )
    btn = Button:new()
    btn:SetParent( parent )
    btn:SetPosition( x, y )
    btn:RegisterCallback( callback )

    return btn
end;

-- callback function
function OnMouseDown( btn )
    btn:SetPosition( 123, 456 )   <== FAILS !!!
end

btn1 = createButton( nil, 0, 0, OnMouseDown )

Best Regards
Micha


Jerome Vuarand schrieb:
2008/11/10 Michael Bauroth <Michael.Bauroth@falcom.de>:
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" ) );
}

Use lua_pushlightuserdata or lua_newuserdata to pass a pointer to Lua.
For example:

lua_rawgeti(L, LUA_REGISTRYINDEX, m_CbOnMouseDown);
lua_pushlightuserdata(L, this);
if ( lua_pcall( L, 1, LUA_MULTRET, 0 ) != 0 )
{
    TRACE( _T( "Error calling LUA callback OnMouseDown\n" ) );
}