pico8-0hh1/coroutines.lua

35 lines
681 B
Lua
Raw Normal View History

2023-10-07 19:12:53 +02:00
local coroutines = {}
2022-06-02 17:55:52 +02:00
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)
2023-10-07 19:12:53 +02:00
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)
2023-10-07 19:12:53 +02:00
del(coroutines, co)
2022-06-02 17:55:52 +02:00
end
function _coresolve()
2023-10-07 19:12:53 +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)
2023-10-07 19:12:53 +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)
2023-10-07 19:12:53 +02:00
for i = 1, frames do
yield()
end
2022-06-02 17:55:52 +02:00
end