Basic event system I have been using in my LÖVE games
Lua
Latest commit 8ed9dcc Jan 22, 2013 Alex Szpakowski Fixed registered objects not being cleaned up by the garbage collecto…
…r, moved example useage to readme
Permalink
Failed to load latest commit information.
README
event.lua

README

Simple event library for Lua

Example usage:


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


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.