moonsharp-devs / moonsharp

An interpreter for the Lua language, written entirely in C# for the .NET, Mono, Xamarin and Unity3D platforms, including handy remote debugger facilities.
http://www.moonsharp.org
Other
1.41k stars 213 forks source link

Lua Script to delay #284

Open tommynanny opened 4 years ago

tommynanny commented 4 years ago

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.

MIVerTFT commented 4 years ago

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)");
        }
blakepell commented 3 years ago

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();
  }
psstevenchan commented 3 years ago

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