lua-users home
lua-l archive

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


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.

On Mon, Nov 10, 2008 at 1:54 PM, Michael Bauroth <Michael.Bauroth@falcom.de> wrote:
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
Micha


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