Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- # Freack100's first try at a "multitasking" manager
- # I highly recommend to NOT use this in actual programs because it might be unoptimized and only for development!
- # You ARE allowed to used it everywhere and modify the code. But you MUST give credits to me as the owner of the "core" of the code.
- ]]
- local _tasks = {}
- local _filter = {}
- local _coreTasks = {}
- local _runningTask = 0
- local stackRunning = false
- --[[
- @description
- Allows you to add a new task to the stack
- @param func
- The function you want to use as task
- @return
- Returns the ID of the task you added
- ]]
- local function addTask(func)
- if type(func) ~= "function" then error("Please only feed me with functions!",2) end
- _tasks[#_tasks+1] = coroutine.create(func)
- return #_tasks
- end
- --[[
- @description
- Allows you to remove a task from the stack
- @param ID
- The ID of the coroutine you want to remove
- @return
- Returns nothing
- ]]
- local function removeTask(ID)
- if _tasks[ID] == nil then error("I never have eaten that function, sorry.",2) end
- _tasks[ID] = nil
- end
- --[[
- @description
- The function you call to start the stack
- @param
- None
- @return
- Nothing
- ]]
- function runTaskStack()
- stackRunning = true
- local _eventData = {coroutine.yield()}
- while stackRunning do
- for k,v in pairs(_tasks) do
- if type(v) == "coroutine" then
- _runningTask = v
- if coroutine.status(_runningTask) == dead then _tasks[_runningTask] = nil end
- if _filter[_runningTask] == nil or _filter[_runningTask] == _eventData[1] or _eventData[1] == "terminate" then
- coroutine.resume(_runningTask,unpack(_eventData))
- end
- end
- end
- _eventData = {coroutine.yield()}
- end
- end
- --[[
- @description
- The function you call to stop the stack
- @param
- None
- @return
- Nothing
- ]]
- function stopTaskStack()
- stackRunning = false
- end
- --[[
- @description
- This function you call to get the ID of the current running task
- @param
- None
- @return
- The current running task ID
- ]]
- --# COMMENT: This function could be used for a program to "terminate" itself
- function getRunningTask()
- return _runningTask
- end
- addTask(function()
- while true do
- local evt = {coroutine.yield("stopTask")}
- stopTaskStack()
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement