lua-users home
lua-l archive

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


Well, without spamming a bunch of code around (and sense I just noticed this thread) the basic way we are handling it can be seen in the demo on our site: http://eonclash.com/ViewProduct.php?ProductID=23

It comes down to the Lua Registry and testing for global and local (table/metatable) method existence and calls.

 - Jeremy

On 4/9/07, Jose Marin < jose_marin2@yahoo.com.br> wrote:
Hi.

I see two main ways of implementing Lua event handlers in a game:

1) Use the lua_getglobal and them lua_call (or lua_pcall).

Example:

In Lua:

function OnTimer(timerID)
...
end


In C:

    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.


2) Store the reference to the function that will handle each event, and call it later, when needed.

Again, I could use the lua_getglobal to find the reference to the callback functions (there will be many of them, because actors will hare their callbacks too) at the start of the game, and store it in C variables.

The problem is how to store these references, and how to call them later.

I could use a registered C function to do that, for example:

In Lua:

function OnTimer(timerID)
...
end


RegisterEventHandler(EV_TIMER, OnTimer)


In C:
    How to get the reference on the OnTimer function? And how call it later?



I think there is already a thread about this subject, but I couldn't find it.

Thanks for any tip!


Jose

__________________________________________________
Fale com seus amigos  de graça com o novo Yahoo! Messenger
http://br.messenger.yahoo.com/



--
Jeremy

"Help I suffer from the oxymoron Corporate Security."