moonsharp icon indicating copy to clipboard operation
moonsharp copied to clipboard

Lua Script to delay

Open tommynanny opened this issue 5 years ago • 3 comments

Hi, I was wondering how I can add delay to my script, like "wait(2s)" or "sleep(2s)" before continue to execute the next line in my moonSharp Lua script.

tommynanny avatar Jul 06 '20 21:07 tommynanny

You can use standard tools .net for example Thread.Sleep

	public class Utils1
        {
            public void Sleep(int ms) =>
                Thread.Sleep(ms);
        }
        public void SleepTest()
        {
            UserData.RegisterType<Utils1>();
	    var S = new Script();
            S.Globals["ds"] = new Utils1();
            DynValue res = S.DoString("ds:Sleep(4000)");
        }

MIVerTFT avatar Jul 08 '20 08:07 MIVerTFT

I personally use this, hasn't caused me any grief. I expose it via the C# interp so it can be called as a Lua command. Very handy.

  public void Sleep(int milliseconds)
  {
      Task.Delay(milliseconds).Wait();
  }

blakepell avatar Feb 24 '21 20:02 blakepell

you can try this

sleep = function(time)
    local t = 0
    repeat
        local T = os.time()
        coroutine.yield(0)
        t = t + (os.time()-T)
    until t >= time
end

psstevenchan avatar Jul 27 '21 09:07 psstevenchan