lua-users home
lua-l archive

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


John Klimek said:
> Ok, take a look at the following lua script:
>
> -- Funcion to be executed later
> function SayHello()
>   myNewNpc_temp:SayRoom("Hello everybody!");
> end
>
> -- Create a new NPC and store it in "myNewNpc"
> local myNewNpc_temp := NPCManager.New("Bob");
>
> -- Create a timer to run the SayHello function in 10 seconds
> local eventId = mud:onTimer(10, SayHello);
>
> -- end of lua script
>
>
>
> In the above example, the NPC is created (myNewNpc_temp) and a timer
> is created.  (eg. the function SayHello() is stored in the registry)
> When the timer executes and the host program runs the SayHello
> function, it no longer knows about myNewNpc_temp.

That's right. So you could add the 'self' argument to onTimer:

local eventId = mud:onTimer(10, SayHello, myNewNpc_temp)

(or, more compactly:
   mud:onTimer(10, SayHello, NPCManager.New"Bob")
)

where onTimer(delta, meth, obj) would queue up a call to meth(obj) at the
time. (In fact, you could provide arbitrary arguments, by making onTimer
varargs.)

Alternatively, you could do the work in Lua:

local eventId = mud:onTimer(10,
                            function()
                              return SayHello(myNewNPC_temp)
                            end)