   Best way to pause (sleep?) inside of lua script ?    (design/implement question)
  I'm developing a MUD (multi-user dungeon game) as a side project and I'm going to use Lua for scripting.  You've probably seen my last e-mail regarding how to share objects but I have that mostly figured out.  However, I have another question...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 secondnpc:Say("Hello again!!");SLEEP(1000);  -- Sleep for another secondnpc:Say("I'm being really annoying now!!");Now I'm not even sure if SLEEP() is the correct command, but as far as my host application is concerned if I pause in a Lua script it will halt the entire program until lua_dofile() returns.What's the best way to implement the Lua scripting engine into a game?How can I get Lua to pause (eg. sleep) without stopping my entire game?Can one Lua instance be shared between shared threads?  (eg. several players in the game will be executing Lua scripts)Should I spawn a seperate thread for every single lua script being run?Any advice would be greatly appriciated!!   
  You might want to look into "co-routines" as they allow a form of cooperative multi-tasking with Lua.   ;Quote:Original post by phantomYou might want to look into "co-routines" as they allow a form of cooperative multi-tasking with Lua.So co-routines will let me have multiple threads using lua at the same time or they will let a lua script have stuff like sleep() in it?Thanks!   ;  coroutines are a teensy bit tricky, but you can basically pause a script and continue it at a later time.A coroutine has to be created as such. If the script runs into coroutine.yield Lua stops and returns to the c caller. You can now anytime later call coroutine.resume to continue the script.yield and resume let you pass parameters. I'm using this functionality for a wait command. I pass the wanted time delay, the script checks, if enough time has passed. If not it yields. The resume function gets passed the time elapsed. This continues until the time value is reached and the wait function returns normally.   ;  This is possibly the most frequently asked question on this forum. If you use the forum search/google site search, you will find some suggestions. But in short, what you generally want is (and Endurion did pretty much say this already, so apologies for the repetition):1) Implement your scripts as Lua coroutines2) Write a 'sleep' function in Lua that pauses the current coroutine and tells the host program when it needs to be resumed.3) Implement a polling loop or event queue in your host program that resumes paused coroutines when necessary.Don't forget:4) Perform some sanity checking on your objects to ensure that what was a valid object when you hit the 'sleep' function hasn't been destroyed in the time between then and resuming. eg. What do you want "npc:Say" to do if 'npc' got killed in the last second?   