Files
Basalt/oldVersions/lastVersion/source/project/objects/Thread.lua
Robert Jelic c00d466b10 changed the projects filesystem to use require() instead of loadfile()
currently preparing for a better package manager/installer
2022-07-03 21:19:01 +02:00

69 lines
1.8 KiB
Lua

local function Thread(name)
local object
local objectType = "Thread"
local func
local cRoutine
local isActive = false
object = {
name = name,
getType = function(self)
return objectType
end;
getZIndex = function(self)
return 1
end;
getName = function(self)
return self.name
end;
start = function(self, f)
if (f == nil) then
error("Function provided to thread is nil")
end
func = f
cRoutine = coroutine.create(func)
isActive = true
local ok, result = coroutine.resume(cRoutine)
if not (ok) then
if (result ~= "Terminated") then
error("Thread Error Occurred - " .. result)
end
end
return self
end;
getStatus = function(self, f)
if (cRoutine ~= nil) then
return coroutine.status(cRoutine)
end
return nil
end;
stop = function(self, f)
isActive = false
return self
end;
eventHandler = function(self, event, p1, p2, p3)
if (isActive) then
if (coroutine.status(cRoutine) ~= "dead") then
local ok, result = coroutine.resume(cRoutine, event, p1, p2, p3)
if not (ok) then
if (result ~= "Terminated") then
error("Thread Error Occurred - " .. result)
end
end
else
isActive = false
end
end
end;
}
object.__index = object
return object
end