• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Developer Blog

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).


  • /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:
Refesh.
If somebody have idea for next blog entry, please write. I can explain anything, LUA, C++, and other languages.
 
I wanted to share some idea. In this code I wanted to make function queue that is executed one by one, I can handle if its paused, terminated and so on.
Maybe you find it use full to implement it to your thread class. Code I post is untested but I hope that main idea will be visible there. Regards.

Lua:
function Queue:new()
	local obj = {
		--callbacks = {func, {arguments}},
		callbacks = {}
		currentCallback
		currentId = 1
		currentEvent = -1
		continue = true
	}
	
	self.__index = self
	return setmetatable(obj,self)
end
function Queue:put(func,delay,...)
	 if type(func) ~= "function" then
	  return error(" Argument 1 has to be function reference")
	 end
	 
	 local tmp = {}
	 tmp.func = func
	 tmp.delay = delay
	 tmp.arg = arg
	 table.insert(self.callbacks,tmp)
end	
function Queue:execute()
	  return function(id)
	    if id == nil then
		 id = 1
		end
		if id > table.maxv(self.callbacks) then
		  continue = false
		  return false
		end
			self.currentId = id
			self.currentEvent = addEvent(self.callbacks[i].func,self.callbacks[i].delay, unpack(self.callbacks[i].arg))
			if continue then
				addEvent(self.execute(),self.callbacks[i].delay, id+ 1)
			end
	  end	  
end

function Queue:pause()

end
function Queue:continue()

end
function Queue:terminate()

end

It could be used this way

We have somefunction we want to execute.
we define new queue, and put functions we defined earlier:

Lua:
  local event = Queue:new()
  event:put(shufflePlayers,1000,some arguments)
  event:put(kickLosers,2000,some arguments)

 etc..
 
Last edited:
Back
Top