Event library thing

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
slime
Solid Snayke
Posts: 3131
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Event library thing

Post by slime »

I made this for my games and thought I'd share. It's a library designed to help with events! It's very similar to what Vendetta Online has because that's what I'm used to.


GitHub page with download link


Usage isn't too complicated. It's nearly identical to the above link to what Vendetta does, with a couple things added or removed. Note that anything triggered gets processed immediately, there is no queue.
You can register (and unregister) an object to multiple events at once either via an array or just additional arguments to the Event.[Un]Register function.

Not everything has been tested extensively so I'd appreciate any bug reports. :P

I wrote this comment in the lua file:

Example usage with imaginary Cat and Mouse classes:

Code: Select all


-- create generic event handling function for a Cat class
function Cat:OnEvent(eventname, ...)
	if eventname == "MOUSE_SPAWNED" and not self.chasing then
		local mouse = ...
		self:ChaseMouse(mouse)
	end
end

-- use a regular function to process the event as well
function PrintWhenMouseSpawns(eventname, mouse)
	print("MOUSE SPAWNED! "..tostring(mouse))
end

function Mouse:initialize(x, y)
	self:Spawn(x, y)
	Event.Trigger("MOUSE_SPAWNED", self) -- trigger the event and pass any arguments you want
end

-- register the Cat class and function we created with the MOUSE_SPAWNED event
Event.Register(Cat, "MOUSE_SPAWNED")
Event.Register(PrintWhenMouseSpawns, "MOUSE_SPAWNED")

NOTE: an 'object' (the thing you register) can be either a function or a table.
If it is a table, then when the event it's associated with is triggered, it will
first look for a function of the same name as the event in the table, and if it
doesn't find one it will fall back to the table's "OnEvent" method, if it exists.
Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests