lua-users home
lua-l archive

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

This is good!

But justo to increase my knowledge on Lua, it's possible to save the result of lua_getglobal(L, "OnTimer"); ?

This way, on the beggining of the game I could do this

lua_getglobal(L, "OnTimer");

SaveFunctionOnStack(L, FUNC_ONTIMER);

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_pushnumber(L, wParam);

         lua_call(L, 1, 0);


----- Mensagem original ----
De: Ashwin Hirschi <>
Para: Lua list <>
Enviadas: Terça-feira, 10 de Abril de 2007 14:52:16
Assunto: Re: How do you implement Lua event handlers in your game?

>     case WM_TIMER:
>         lua_getglobal(L, "OnTimer");
>         ... check error
>         lua_pushnumber(L, wParam);`
>         lua_call(L, 1, 0);
>        break;
> The problem here it's the performance of the lua_getglobal function, if I use it to implement event handlers to actors (will have many of them), too.

Jose, there really is no problem with performance here.

We've done this wiring of Lua handlers to Windows messages for years (and on very old hardware, too). Never had any problems whatsoever.

So, your particular scenario will work just fine. Don't hold back for fear of lack of speed, and just go for it!


P.S. Of course, if you can come up with something that'll work faster... hey, that's okay too [;-)].
no signature is a signature.

Fale com seus amigos  de graça com o novo Yahoo! Messenger