lua-users home
lua-l archive

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


Hi.

On Fri, Aug 11, 2006 at 02:45:46PM +0000, 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.

You can declare the function parameter as a 'lua_Object', or
'lua_Function'. You can also take the lua_State*. For example (on your pkg
file):

class Animation {

	void setCallback(lua_State* L, lua_Object obj);
};

and use it from lua:

runner:setCallback(anima)

The lua state is included automatically. lua_Object and lua_Function are
actually ints with the index on the stack where you'll find the callback
(you might have to declare the typdef yourself). When using lua_Function,
I think tolua checks specifically for a function type (tolua++ doesn't, so
you can also pass callable objects).

Ariel.