lua-users home
lua-l archive

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

Ok, seems that the internal variable btn1 is from dedicated type: Button (002111C0), while the incoming variable has no type: userdata: 002111C0. That's why I can't call any function on it. But how can I convert (or cast) the userdata to the correct object type again?

Best Regards

Michael Bauroth schrieb:
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

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

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

Best Regards

Jerome Vuarand schrieb:
2008/11/10 Michael Bauroth <>:
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

// 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" ) );