lua-users home
lua-l archive

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


Hi John,

perhaps you may look at the game "fish-fillets" (http://fillets.sf.net)
for hints.

> Let's say I want to have a script do something like this:  
> (pseudo-code)
>
> npc = CreateNPC("Bob");
> npc:SetRoom("main");
> npc:Say("Hello everybody in the room!");
>
> SLEEP(1000);  -- Sleep for one second
> npc:Say("Hello again!!");
>
> SLEEP(1000);  -- Sleep for another second
> npc:Say("I'm being really annoying now!!");
>

Well, _I_ would implement this as:

npc = CreateNPC("Bob");
npc:SetRoom("main");
AddEvent("BobsFunction", os.time(), { npc, event=1});

where 'AddEvent' would be a function wich would take three arguments:
A function name to call, the time WHEN to call the function, and a
'private' object.

AddEvent() would then call the function at the given time (or if the time
is in the past) and REMOVE the function from the list when the fn gets
called.

Pseudocode:

BobsFunction(Object)
    if not Object.event or Object.event == 0 then
	Object.npc:Delete(); -- or whatever to free workspace
        return;
    end;
    local e, npc = Object.event, Object.npc;
    Object.event = e + 1; -- inrement private event number
    if e == 1 then
        npc:Say("Hello everybody in the room!");
        AddEvent("BobsFunction", os.time() + 5, Object); -- call in 5s
        return;
    elseif e == 2 then
        npc:Say("Hello again, foobars!!!");
        AddEvent("BobsFunction", os.time() + 10, Object); -- call in 10s
        return;
    end;
    npc:Say("I'm being really annoying now!! Bye :(");
    npc:Delete(); -- no re-adding
end;


This way you also could switch functions for 'Bob'.

HTH,
  Torsten