• 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!

Event LIB (003-event.lua)

Baxnie

Active Member
Senator
Joined
Oct 5, 2007
Messages
90
Reaction score
47
Location
Brazil
Hi, i've made this object to be easier to use events.
Main usefull thing here is the createCycleEvent function.

Code:
EVENT_TYPE = {
	SINGLE = 1,
	CYCLE  = 2,
}

-- private functions
local function parseSingleEvent(event)
	event.f(unpack(event.args))
	event.id = nil
end

local function parseCycleEvent(event)
	event.f(unpack(event.args))
	event.id = addEvent(parseCycleEvent, event.time, event)
end

-- member functions
Event = {}

function Event:start()
	if not self.id then
		if self.type == EVENT_TYPE.SINGLE then
			self.id = addEvent(parseSingleEvent, self.time, self)
		elseif self.type == EVENT_TYPE.CYCLE then
			parseCycleEvent(self)
		end
	else
		print('Trying to start an already started event.')
	end
end

function Event:restart(time)
	if self.id then
		self:stop()
	end
	self.time = time or self.time
	self:start()
end

function Event:stop()
	if self.id then
		stopEvent(self.id)
		self.id = nil
	else
		print('Trying to stop an already deleted event.')
	end
end

function Event:isFinished()
	return not self.id
end

-- public functions
function createSingleEvent(f, time, ...)
	local event = {}
	event.f = f
	event.args = {...}
	event.time = time
	event.type = EVENT_TYPE.SINGLE
	setmetatable(event, { __index = Event })

	event:start()
	return event
end

function createCycleEvent(f, time, ...)
	local event = {}
	event.f = f
	event.args = {...}
	event.time = time
	event.type = EVENT_TYPE.CYCLE
	setmetatable(event, { __index = Event })

	event:start()
	return event
end

As a simple example, it can be used to send a magic effect on certain map position every 500ms.

Code:
g_testEvent = createCycleEvent(doSendMagicEffect, 500, {x=100,y=100,z=7}, CONST_ME_FIREAREA)

-- it can be stopped when using an item..
function onUse()
  g_testEvent:stop()
end

-- it can be sent faster when using an item..
function onUse()
  g_testEvent:restart(100)
end
 
Last edited:
I like it, and nice. I want to see examples in a real event instead of just how to use the functions.
 
Back
Top