Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function multitasker(...)
- local a = {...}
- local m = {t = {}}
- for k, v in pairs(a) do
- m.t[k] = assert(coroutine.create(v))
- end
- m.status = "paused"
- m.start = (function(mode)
- local mode = mode or "all"
- if (m.status == "done") then
- error("Cannot start a completed multitasker",2)
- end
- m.status = "running"
- while (m.status == "running") do
- if (#m.t == 0) then
- m.status = "done"
- end
- for i,t in pairs(m.t) do
- if (coroutine.status(t) == "dead") then
- table.remove(m.t,i)
- if (string.lower(mode)=="any")then
- m.status = "done"
- end
- i = i-1
- else
- coroutine.resume(t)
- end
- end
- sleep(0)
- end
- if (m.status ~= "paused") then m.status = "done"
- end
- end)
- m.pause = (function()
- if (m.status == "done") then
- error("Cannot pause a completed multitasker",2)
- end
- m.status = "paused"
- end)
- return m
- end
- --while solution
- function wh(bool,fn)
- if (bool==true or bool()) then
- fn()
- coroutine.yield()
- wh(bool,fn)
- return
- else
- return
- end
- end
- --sleep solution/"timer" like thing-- may or may not work in cclua as millisecond time isn't used often
- function sl(ms)
- ms = ms/1000*.94
- local start = os.clock()
- local function wait()
- if(os.clock()-start>ms) then
- return
- end
- coroutine.yield()
- wait()
- end
- wait()
- end
- --[[
- --# Multitasking API
- --# Created by Convo_bomber34
- --# Example:
- function a()
- wh(true,function()
- print("a",(os.date("*t").sec))
- sl(1000)
- end)
- end
- function b()
- local i = 1
- wh(function()return i<=4 end,function()
- sl(250)
- print("b",i)
- i = i+1
- end)
- end
- m = multitasker(a,b)
- m.start("any")
- --]]--
Advertisement
Add Comment
Please, Sign In to add comment