Lua C Wait
Funcptr is a C callback declared as: void.(.funcptr)(void.arg). Its return value is returned by th:join. The optional attrs table can have the fields: detached = true - start detached (not very useful with Lua states) priority = n - thread priority; must be between pthread.minpriority and pthread.maxpriority – in Linux these are. “lua wait” Code Answer’s. Wait function lua. Lua by Tes on Nov 07 2020 Donate. 3 how to wait in lua. Print('Hello World') If you are using the stand-alone Lua interpreter, all you have to do to run your first program is to call the interpreter (.
|
A common need is to pause (sleep) a program for a certain number of seconds, preferably without busy waiting.
This function to do this without busy waiting does not exist in ANSI C, so it does not exist in stock Lua. However, there are extension libraries and calls to external programs that can do this.
Solution: Busy Wait
Solution: C extension
There is a sleep function in ExtensionProposal. This may call Win32 Sleep or POSIX usleep. Here's a [usleep/sleep C wrapper] example.
The LuaApr binding has an [apr.sleep()] function that works on Windows & UNIX and supports sub-second resolution.
The lalarm library[1] can set an alarm on POSIX.
winapi (Windows only) has a [sleep] function. [github]
If an FFI interface (Alien or c/invoke -- BindingCodeToLua) is available, you can call whichever OS function you have.
Solution: sleep command
Windows does not have such a built-in command. However, there's a sleep in the Windows Server Resource Kit. There is also sleep in Cygwin and MinGW. Also, there is 'timeout' utility available in Windows 7
Solution: ping or other programs
This is mainly for Windows in the absence of a sleep command. Other variations exist, e.g. 'perl -e 'sleep(' .. tonumber(n) .. ')'
or 'php -r sleep(' .. tonumber(n) .. ');'
.
Solution: I/O wait
This is not a sleep but may be useful in similar cases. It waits for the user to press the Enter key.

Solution: Using WScript (Windows)
Luc Walter Amphenol
See [2].
Solution: sleep()
The POSIX sleep() call provides integer second sleeps.
Solution: socket.sleep()
The LuaSocket? module provides a sleep function.
Solution: ngx.sleep()
Nginx Lua module provides a sleep function. One can specify time resolution up to 0.001 seconds (i.e., one milliseconds). Behind the scene, this method makes use of the Nginx timers.
Solution: lsocket.select()
The select() timeout provides a fairly portable sub-second sleep, if you can tolerate the socket library dependency.
Solution: LuaJIT FFI/LuaFFI
Solution: os.time()
Lua C Wait Times
Solution: os.clock()
. .
Using the os.clock() method instead of os.time(), you can get precision down to one 100th of a second while os.time() only allows intervals based on the timestamp, which at execution can be at anything from 0.1 to 1 second. The os.time() method is great for longer periods over 2 seconds where precision isn't that much of a deal.
See Also
RecentChanges · preferencesLua C Wait Online
edit · historyLast edited October 20, 2019 1:12 pm GMT (diff)
Lua C Download
-- Consider using Thread instead! |
-- https://gist.github.com/CloneTrooper1019/538f0ab2541ef98912a2694dd8d274e7 |
local RunService = game:GetService('RunService') |
local threads = {} |
RunService.Stepped:Connect(function () |
local now =tick() |
local resumePool |
for thread, resumeTime inpairs(threads) do |
-- Resume if we're reasonably close enough. |
local diff = (resumeTime - now) |
if diff <0.005then |
ifnot resumePool then |
resumePool = {} |
end |
table.insert(resumePool, thread) |
end |
end |
if resumePool then |
for _,thread inpairs(resumePool) do |
threads[thread] =nil |
coroutine.resume(thread, now) |
end |
end |
end) |
localfunctionfastWait(t) |
local t =tonumber(t) or1/30 |
local start =tick() |
local thread =coroutine.running() |
threads[thread] = start + t |
-- Wait for the thread to resume. |
local now =coroutine.yield() |
return now - start, elapsedTime() |
end |
return fastWait |
commented Jul 19, 2020
Thanks for releasing this, I never realized how sluggish the normal wait() function is! |
commented Feb 8, 2021
Amazing script. Even if you don't think you need it, you need it! |