lua-users home
lua-l archive

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


Hi Lloyd,

Have a look at the source code for Grunt on LuaForge:

http://luaforge.net/projects/grunt/

I created a Lua class implemented in C called "Delegate_List" which
maintains a list of functions (the event handlers) and a metatable override
for the Lua call operation makes a call on objects of this class call all
the functions in the list in turn. So you write something like:

event = new.Delegate_List(function(x) print x.."1" end, function(x) print
x.."2" end)
event.add(function(x) print x.."3" end)
event("fred:")

result:

fred:1
fred:2
fred:3

In "C" I connect specific pre-defined Delegate_List objects to Windows and
Application messages so the end-user simply uses the Add method to connect
their handler functions to these fields, which I then call "Events"

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Lloyd
Sent: 01 June 2009 12:05
To: lua@bazar2.conectiva.com.br
Subject: Lua Event driven programming


Hi,

I would like to know the following kind of thing is possible with Lua. I  
am trying to implement a simple event driven system. Lets start with a  
sample

--My script
Event1(MyEventHandler1)
Event2(MyEventHandler2)

function MyEventHandler1(EventObj obj)
--some action mechanism
end

function MyEventHandler2(EventObj obj)
--some action mechanism
end

The above may not be a working Lua script, but I am trying to depict my  
idea.

"MyEventHandler1,MyEventHandler2" are two event handler functions which  
receives an event object, which is thrown by the "Event1" or "Event2".  
There will be a function "Event1" implemented in C. I would like to know  
whether there is a mechanism to throw Events from the functions like  
"Event1" and handle it using the above said method.

The above may not be a good solution, is there is any better way for doing  
an event driven program.

Thanks,
   Lloyd



______________________________________
Scanned and protected by Email scanner