pico8-0hh1/coroutines.lua

34 lines
650 B
Lua
Raw Normal View History

2022-06-02 17:55:52 +02:00
local coroutines={}
2022-07-02 20:45:11 +02:00
-- starts a coroutine and saves it for future reference
2022-09-24 15:22:18 +02:00
function startcoroutine(co)
assert(tostr(co) == "[thread]") -- make sure that co is a coroutine, the return value of cocreate()
add(coroutines, co)
2022-06-02 17:55:52 +02:00
end
2022-07-02 20:45:11 +02:00
-- stops the the coroutine
2022-09-24 15:22:18 +02:00
function stopcoroutine(co)
del(coroutines, co)
2022-06-02 17:55:52 +02:00
end
function _coresolve()
2022-09-24 15:22:18 +02:00
for co in all(coroutines) do
if costatus(co) != 'dead' then
assert(coresume(co))
else
stopcoroutine(co)
end
end
2022-06-02 17:55:52 +02:00
end
2022-07-02 20:45:11 +02:00
-- to be used inside a coroutine
2022-06-02 17:55:52 +02:00
function wait(seconds)
2022-09-24 15:22:18 +02:00
wait_frames(seconds*30)
2022-06-02 17:55:52 +02:00
end
2022-07-02 20:45:11 +02:00
-- to be used inside a coroutine
2022-06-02 17:55:52 +02:00
function wait_frames(frames)
2022-09-24 15:22:18 +02:00
for i=1,frames do
yield()
end
2022-06-02 17:55:52 +02:00
end