lua-users home
lua-l archive

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


Sorry, I've missed you are using tolua, so it becomes

INT RUNNER::setCallback( lua_State *L)
{
   //anima is at -1 on stack
   reference = luaL_ref ( L, LUA_REGISTRYINDEX);
   return 0
}

INT RUNNER::callback( lua_State *L )
{
   lua_rawgeti(L, LUA_REGISTRYINDEX, reference);
   tolua_push( L, this); //depends of your system
   lua_pcall( L, 1, 0, 0 );
   return 0;
}

Julien Hamaide wrote:
> For me, there are two way of doing it.
> 
> If setCallback is a C function, you can try this
> 
> int callback_reference;
> 
> setCallback( lua_State *L)
> {
>     //anima is at -1 on stack
>     reference = luaL_ref ( L, LUA_REGISTRYINDEX);
> }
> 
> callback( lua_State *L )
> {
>     |lua_rawgeti(L, |REGISTRYINDEX|, |reference |);
>   lua_push( L, runner ); //depends of your system
>   lua_pcall( L, 1, 0, 0 );
> |}
> 
> don't forget to release the ref with luaL_unref()
> 
> If your object is a lua object try this
> 
> {
>     lua_getglobal( L, "runner" );
>     lua_getfield( L, -1,"callback" ); //stack : runner, function
>     lua_insert( L, -2 ) ; // stack: function, runner
>     lua_pcall( L, 1, 0, 0 );
> 
> }
> 
> Now it depends how you have implemented C binding in lua. With this
> information, I can give you the right method to do it
> 
> Julien
> 
> 
> Jose Marin wrote:
>>>> In lua, a function is a variable.
>>> You are right, Julien, but the 'anima' function will
>>> be called in C++ code;
>>>
>>> The 'setCallback' is a method of the Animation class.
>>>
>>> My doubt is how to declare the method 'setCallback' in
>>> tolua, and how to store in the C++ code the reference
>>> to the Lua funcion and how to call it later, passing
>>> the 'runner' object.
>>>
>>>
>>>
>>>
>>>> when you write
>>>>
>>>> function anima(self)
>>>> - -- Do something with self...
>>>>
>>>> end
>>>>
>>>> you actually writing
>>>>
>>>> anima = function( self )
>>>>
>>>> - -- Do something with self...
>>>>
>>>> end
>>>>
>>>> so in your callback, just do
>>>>
>>>> runner:setCallback(anima)
>>>>
>>>>      self.somefield = anima;
>>>> end
>>>>
>>>> runner:Callback()
>>>>    self.somefield( self );
>>>> end
>>>>
>>>> Julien
>>>>
>>>>
>>>
>>>        
>>> _______________________________________________________
>>> Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu
> celular. Registre seu aparelho agora!
>>> http://br.mobile.yahoo.com/mailalertas/
>>>
>>>
>>>
>>>
> 
> 
> --
> --
> Julien Hamaide
> Engineering Coach
> 10Tacle Studios Belgium / Elsewhere Entertainment

-- 
--
Julien Hamaide
Engineering Coach
10Tacle Studios Belgium / Elsewhere Entertainment