pico8-0hh1/coroutines.lua
2022-07-02 20:45:11 +02:00

34 lines
667 B
Lua

local coroutines={}
-- starts a coroutine and saves it for future reference
function startcoroutine(fn)
assert(tostr(fn) == "[thread]") -- make sure that fn is a coroutine, the return value of cocreate()
add(coroutines, fn)
end
-- stops the the coroutine
function stopcoroutine(fn)
del(coroutines, fn)
end
function _coresolve()
for co in all(coroutines) do
if costatus(co)!='dead' then
assert(coresume(co))
else
stopcoroutine(co)
end
end
end
-- to be used inside a coroutine
function wait(seconds)
wait_frames(seconds*60)
end
-- to be used inside a coroutine
function wait_frames(frames)
for i=1,frames do
yield()
end
end