Lua AFAIK doesn't have a way to sleep for real. Like it kinda has corutines, but I think within Lua you'd only have access to bussy loops. Which has a whole assortment of problems.
Are you asking about this Retro Gadgets game? They might have added extra features on top of Lua to make a more real wait/sleep system. Here's a link to their documentation. You can probably get more directed help on their discord server.
If they give you an event loop, I'd implement a wait by registering an event that just checks the time, then if the time is right, call the real function and unregister the event. This is still basically a bussy loop, but you are giveing control to event loop, so your bussy loop won't block other scripts.
Edit: check out the update function. All you need is an if block inside the update function to see if the time is right, then do something, then update the time to when you want it to run next.
well technically you could use the os libray and outsource the sleep to the system your on, which is a means lua has to do the sleep; just not doing it without outside involvement.
I didn't even consider asking the OS to do the sleep. That's a fun idea. OP's environment probably doesn't allow for that, but I'll need to remember that for my own uses.
i know of that trick because when i search for "sleep in lua" a stackoverflow answer suggesting to use OS Commands, even something like Pinging your self or a websocket timeout against an adress that doesnt exist as ways to do this. Really you just need to think of an action you can do in a given environment that lets you control the time it takes directly or indirectly, even a set timeout of 1 second for example would mean you could sleep in steps of 1 second and do something like a for i loop to timeout as many times as you want to sleep in seconds.
i dont know how that environment your in works but when in the past i neededa while loop i used luas os.execute to tell the operating system to sleep my application for me but that may not be good for you if you dont want to block your whole setup any time you sleep.
os.execute("sleep 5") Blocking: This command blocks the entire Lua thread (and therefore the whole program) for 5 seconds. If your game or gadget needs to handle other tasks (like user input, display, or other scripts), everything will be frozen
os.execute("sleep 5") Blocking: This command blocks the entire Lua thread (and therefore the whole program) for 5 seconds. If your game or gadget needs to handle other tasks (like user input, display, or other scripts), everything will be frozen. Portability: sleep is a Unix command (Linux/macOS). On Windows, you would need to use timeout or an alternative, which makes the code non-portable. Security: Using os.execute to run system commands can be risky (code injection, permission issues, etc.). WindowsSleep(5) This function does not exist natively in Lua. You would need to implement it via an external library (like lua-winapi or luasocket), which adds a dependency and complicates the code. 100% CPU with a while loop A while true do end loop (without pause) indeed consumes 100% of the CPU, because it runs in a loop without yielding the processor. It is also a bad practice, okay. Maybe! use the coroutine which does not block the whole process by itself, combined with start = os.clock(), or( for example in love2d), a function love.timer.sleep()
3
u/Emerald_Pick May 29 '26 edited May 29 '26
Lua AFAIK doesn't have a way to sleep for real. Like it kinda has corutines, but I think within Lua you'd only have access to bussy loops. Which has a whole assortment of problems.
Are you asking about this Retro Gadgets game? They might have added extra features on top of Lua to make a more real wait/sleep system. Here's a link to their documentation. You can probably get more directed help on their discord server.
If they give you an event loop, I'd implement a wait by registering an event that just checks the time, then if the time is right, call the real function and unregister the event. This is still basically a bussy loop, but you are giveing control to event loop, so your bussy loop won't block other scripts.
Edit: check out the update function. All you need is an if block inside the update function to see if the time is right, then do something, then update the time to when you want it to run next.
1
u/Average_Pangolin May 29 '26
knife.timer https://github.com/airstruck/knife
1
u/Spacedestructor May 29 '26
i dont know how that environment your in works but when in the past i neededa while loop i used luas os.execute to tell the operating system to sleep my application for me but that may not be good for you if you dont want to block your whole setup any time you sleep.
1
u/Sckip974 May 30 '26
After research I found a forum that offered a library: require >LuaSocket: Allows using socket.sleep(5)
1
u/Intelligent_Yak71 May 31 '26
see https://github.com/BlueLua/timeutil
0
u/Sckip974 May 29 '26
whith a while loop ?