sn3ejk
This account is inactive.
- Joined
- Nov 16, 2011
- Messages
- 2,121
- Solutions
- 1
- Reaction score
- 145
TheForgottenServer
Threading
_____
Hello, I want to sorry, because I don't speak English very well. I wrote simple class for TheForgottenServer, which support threads (commonly referred to as events).
I tested this, but If somebody find a issue - write me PM or comment here.
Of course, if you have idea for changes, write too
Threading
_____
Hello, I want to sorry, because I don't speak English very well. I wrote simple class for TheForgottenServer, which support threads (commonly referred to as events).
- /data/lib/060-thread.lua
Lua:Thread = {} Thread.__index = Thread function Thread:new() local thread = { id = 0, cid = 0, delay = 0, callback = nil } setmetatable(thread, Thread) self.__index = self return thread end function Thread:start(...) if (not self.callback) then error('[Thread:start] Could not start thread.') end self.id = addEvent(self:work(), self.delay, ...) end function Thread:stop() if (self.id <= 0) then error('[Thread:stop] Could not stop thread.') end stopEvent(self.id) end function Thread:work(...) return function(...) if (self.cid > 0 and not isCreature(self.cid)) then self:stop() else self.callback(...) end end end
- Example:
Lua:local event = Thread:new() event.delay = 5 * 1000 event.callback = function(line1, line2) print(line1, line2) end event:start("first word", "second word") -- event:stop()
I tested this, but If somebody find a issue - write me PM or comment here.
Of course, if you have idea for changes, write too
Last edited: