[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: How do you deal with event functions?
- From: Shmuel Zeigerman <shmuz@...>
- Date: Wed, 06 Dec 2006 01:37:26 +0200
Jose Marin wrote:
With Lua 5.1, is there some "proper" way of store
references to user Lua functions, to be called later?
[...]
I see 2 ways of do this:
1) provide a "SetOnKeyPressed" to Lua, and in the code
it would be called like this:
SetOnKeyPressed(OnKeyPressed)
[...]
2)provide the same SetOnKeyPressed function, but the
user would pass a string, like this:
SetOnKeyPressed("OnKeyPressed")
[...]
In Lua 5.1, what's the pro's and con's of these 2
methods?
Is there a better way of do this?
Don't know if it's better but here's a way that seem
to combine the pro's of the 2 above 2 ways.
-- start of Lua example
t_events = {
KeyPressed = OnKeyPressed,
LeftClick = OnLeftClick,
DoubleClick = function (x,y) print (x,y) end,
-- etc.
}
function OnEvent (event, ...)
local f = t_events [event]
if f then return f (...) end
end
SetEvent (OnEvent)
-- end of Lua example
C provides SetEvent function, and Lua registers only
*one* event handler. C calls that handler passing
event name (a string) and any number of parameters.
--
Shmuel