lua-users home
lua-l archive

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


On Tue, Apr 10, 2007 at 11:22:25AM -0700, Jose Marin wrote:
> But justo to increase my knowledge on Lua, it's possible to save the result of lua_getglobal(L, "OnTimer"); ?

int cb;

> This way, on the beggining of the game I could do this
> 
> lua_getglobal(L, "OnTimer");
> 
> SaveFunctionOnStack(L, FUNC_ONTIMER);

cb = luaL_ref(L, LUA_REGISTRYINDEX);

> 
> FUNC_ONTIMER it's an index on an array, where the reference to Lua funcsions would be saved.
> 
> Then, the evente handler would be like this:
> 
>      case WM_TIMER:
>          PushFunctionToStack(L, FUNC_ONTIMER); // Just takes the contents of the array at position FUNC_ONTIMER and pushes it on the Lua stack

lua_rawgeti(L, LUA_REGISTRYINDEX, cb);

> 
>          lua_pushnumber(L, wParam);
> 
>          lua_call(L, 1, 0);
> 
>         break;

  OG.