lua-users home
lua-l archive

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


What's the best approach to use callbacks in Lua?

For example, when the user clicks a mouse button, the Lua function 'mousebutton' must be called.

One simple way of doing this is (syntax checking is off) :

lua_pushstring(L, "mousebutton");
lua_rawget(L, -1); // I will check the correct value

if(lua_type(L, -1) == LUA_TFUNCTION )
    lua_pushinteger(L, ev.x);
    lua_pushinteger(L, ev.y);
    lua_pushinteger(L, ev.button);

   lua_call(L, 3, 0);

lua_pop(L, 0);

But would be nice if I stored the reference to the  'mousebutton' in C code to be called later, avoiding the 'lua_rawget' .

I have not used Lua for a while, so I don't know if there is a more efficient way of doing this.

How do you handle events in your game?



      Veja quais são os assuntos do momento no Yahoo! +Buscados