[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Res: How do you implement Lua event handlers in your game?
- From: Olivier Galibert <galibert@...>
- Date: Tue, 10 Apr 2007 20:35:26 +0200
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.