lua-users home
lua-l archive

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

On 12/10/05, John Klimek <> wrote:

  > Zigmar, can you explain what you mean when you say "register a
  > function itself, not its name"?
  Functions are first-class objects in lua, and therefore function can
get another function as parameter, functions can be stored in
variables, etc. So what I mean, as the function that registers event,
can receive not the name of the function (which therefor must be
global) but a function object itself. It allows you to pass local
function, function stored in some tables or even anonymous function
(as I wrote in example)

  > For example, in your lua code you have:
  > mud.onTimer(10, function() npc:say("Hello") end )
  > It looks like mud is a table filled with cfunctions.  Am I right?
  Yep. It probably some library filled with function that gives you a
connectivity to your host application (mud server). For example
queries about different statuses, registering/removing events,
spawning monsters, etc.

  > Furthermore it looks like you are calling this cfuntion (wherever
  > mud.onTimer is mapped) and passing it a lua function parameter.  Am I
  > right on this as well?
  Exactly. onTimer is a function that registers user event and calls
it with specific interval.

  >  If so, how would you declare the cfunction
  > onTimer that would handle this input?
  Just like any other cfunction - you just handle the parameters on
stack in appropriate way. The tricky thing is that you can non pop and
store function object in C-app as you can do with strings. The
function has to be stored inside lua state itself. To do this, you can
use "registry".

  >  Delphi examples are preferred
  > but C/C++ will work too =)
  Here is an excelent explanation, along with code examples

 BTW, I  thought that it might be a good idea, that event registration
function will return some kind of event id (integral or whatever), so
it can be used later to remove that event notification.

 For example:

 local eventId = mud.onTimer(10, someFunc);
 mud.removeEvent(eventId); -- or maybe "removeTimerEvent"?
 --from now on someFunc will not be called anymore

 Also there should be some functionality to allow to reset all event

  > Thanks!!
  You are welcome :)

  > On 12/9/05, Pavel Antokolsky aka Zigmar <> wrote:
  > > Just an idea - I think it will be much more convinient and flexible to
  > > register a function itself, not its name. Then, you can write
  > > something like:
  > >
  > > -- Every ten seconds
  > > mud.onTimer(10, function() npc:say("Hello") end )
  > >
  > > This system can be also extended to handle different events. For example:
  > > mud:onRoomEnter("main", function(who,where) npc:say("Hello, " .. end )
  > > it can be, for example, extended to handle rooms with masks
  > > mud:onRoomEnter("dungeon_*",
  > >    function(who, where)
  > >        if where:name != "dungen_saferoom" then
  > >            local monster = mud:spawn("monster",where)
  > >            monter:attack(who)
  > >        end
  > >   end
  > > )
  > >
  > > And so on... I can't even think of all possibilites with such system :)

  Best regards,