-
|
How would I have the code pause when I want it to with a wait() function? I'm not sure how to make one. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 9 replies
-
|
There is no wait function, but you can do something like this local t=0
function TIC()
cls(12)
t=t+1
-- wait 2 seconds
if t<2*60 then return end
print("HELLO WORLD!",84,64)
end |
Beta Was this translation helpful? Give feedback.
-
|
just to be helpful, since no one mentioned this yet, instead of "burning CPU" with some kind of wait function, if you need to delay some action, if possible it's better to use the modulo, for example instead of:
use:
|
Beta Was this translation helpful? Give feedback.
-
|
what about something like... this is quick, but it has the disadvantage that can't resume the program flow where were left i learned this the hard way... in my ignorance, i did |
Beta Was this translation helpful? Give feedback.
-
|
@nesbox Can you add a function sleep() or wait()? |
Beta Was this translation helpful? Give feedback.
-
|
A simple sleep function for Lua that allows you to run other code while sleep is happening: Untitled.movlocal tasks = {}
-- https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value
function copy(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
return res
end
function sleep(duration_ms, on_finish) -- Tick is relative to TIC() function rate
local target = time() + duration_ms
table.insert(tasks, coroutine.create(function ()
while time() < target do
coroutine.yield()
end
on_finish()
end))
end
message = "Hello, (waiting...)"
function TIC()
-- Make a copy so we do not remove a index from a table that is being iterated
local runningTasks = copy(tasks)
-- NOTE: if this code isn't being called periodically, all `sleep` tasks will be delayed and/or never called. I'd recommend put this at the top of the TIC function
for idx, task in pairs(runningTasks) do
-- Will resume all submitted sleep tasks, until a `coroutine.yield` is found
coroutine.resume(task)
if (coroutine.status(task) == "dead") then
-- Finished execution, remove
table.remove(tasks, idx)
end
end
-- from example 1: (should remove if using this)
cls(0)
print(message)
-- ... other code
end
-- example 1:
sleep(1000, function ()
message = "Hello, world!"
end)
sleep(2000, function ()
message = "Hello, world!!"
end)
sleep(2500, function ()
message = "Hello, world!! (you really waited!!)"
end)How it works?Lua Coroutines are like functions, but they can be suspended at any point (coroutine.yield() is for suspending a coroutine function), and the code uses this mechanism to see if the timer is already reached it's target If the timer already reached it's target, the So what the code is really doing is iterating through all the timers that the |
Beta Was this translation helpful? Give feedback.
-
|
Thanks!
El mié, 30 jul 2025 a las 15:19, Dayvid Albuquerque (<
***@***.***>) escribió:
… No, the coroutines are not needed, i've only used them because they're so
simple and they maintain their own "space" to handle these.
—
Reply to this email directly, view it on GitHub
<#1864 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BPW6R2ATVLURLJCLX7QP5JT3LDA6BAVCNFSM6AAAAAB2ZWBUWKVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTGOJTGQ3DMMA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
There is no wait function, but you can do something like this