[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How do you implement Lua event handlers in your game?
- From: Eike Decker <eike@...>
- Date: Mon, 9 Apr 2007 14:46:59 +0200
Hi
If you have a userdata value, you can create a table of values in the metatable
of the userdata and store userdata specific values there. In the implementation
of Luxinia, userdata values behave much like tables, i.e. actornode is such a
userdata:
myactor = actornode.new("node") -- creates a node (userdata)
myactor.anyvalue = "foo"
function myactor:think () -- assign myactor.think a function
end
At the moment, we donnot do anything with these table values inside the
enginecore. However, these values could always be read out and used in C. For
your case, for example an entity object could look up a value in case of an
event, if the user assigned an event function for that type:
function entity:onActivate()
-- do something
end
and in C, the eventfunction would have to look up, if that entity has a function
named "onActivate" and call it then, with the entity as first argument. These
actions could be easily looked up with a wrapper function that looks like
that:
void Lua_entity_callfunc (lua_State *L, Entity_t *entity, const char* what)
{
// fetch now entity value and push it on the stack,
// get the metatable,
// get the luatable
// get *what field from the luatable
// check if nil, if not, pcall it
}
the luatable inside the metatable can be accessed from lua by defining the
__index and __newindex functions of the userdata metatable in a way that such
accesses are delegated to the luatable.
cya
Eike Decker
> 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/
>