lua-users home
lua-l archive

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

On Jul 10, 2002 at 08:30 -0400, Tim Conkling wrote:
> I plan on writing an event system for my game whereby different a lua script
> attached to an object will register different handlers for events that are
> to be handled by the object's script file.
> For example, I might have an object of type MyObject, which could be
> associated with a file (on disk) called MyObject.lua. When this object was
> being created, I would read in MyObject.lua and call a specific function in
> that source file such as "initialize()". This function might request that a
> function "beep_loudly()" be called for the event "TOUCHED", and the function
> "become_invisible()" be called for the event "TIMER_EXPIRED". All of this
> function registration has to happen at runtime, of course. How do I handle
> calling a specific registered Lua function (in a buffered file) in C, and
> how would the function registration work in Lua?

One way to do it would be by having the file return a table.  So in
MyObject.lua, you'd do this:

---- MyObject.lua ----
return {
       initialize = function(obj)
		-- ... stuff ...

       TOUCHED = function(obj, args)
		-- ... stuff ...
       -- etc

Have your C/C++ code associate the return value of the script with the
object, then look in the table for the desired handler when an event

Thatcher Ulrich