[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Calling a specific function
- From: Tim Conkling <Timothy_Conkling@...>
- Date: Wed, 10 Jul 2002 22:07:35 -0400
on 7/10/02 8:36 PM, Thatcher Ulrich at tu@tulrich.com reportedly stated:
> 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 ...
> end
>
> TOUCHED = function(obj, args)
> -- ... stuff ...
> end
>
> -- 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
> happens.
Would this work if, for example, the TOUCHED function called another utility
function in the same file?
--
Tim Conkling
Vaporware Software
<http://members.mint.net/conkling>