Event Systems in Lua
Build a lightweight event dispatcher with callbacks stored in tables.
Event Systems in Lua is a free Lua Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.
Why Event Systems?
An event system decouples producers (things that fire events) from consumers (things that react). In Lua, it is easily built with tables of callback functions.
Simple Event Dispatcher
A dispatcher stores lists of listeners keyed by event name.
local EventEmitter = {}
EventEmitter.__index = EventEmitter
function EventEmitter.new()
return setmetatable({_listeners = {}}, EventEmitter)
end
function EventEmitter:on(event, fn)
self._listeners[event] = self._listeners[event] or {}
table.insert(self._listeners[event], fn)
end
function EventEmitter:emit(event, ...)
for _, fn in ipairs(self._listeners[event] or {}) do
fn(...)
end
endUsing the Dispatcher
Subscribe to events with :on and fire them with :emit.
local bus = EventEmitter.new()
bus:on("playerHit", function(player, damage)
player.hp = player.hp - damage
print(player.name, "took", damage, "damage")
end)
bus:emit("playerHit", {name="Alice", hp=100}, 15)One-Time Listeners
A :once method wraps the callback to deregister itself after the first call.
function EventEmitter:once(event, fn)
local wrapper
wrapper = function(...)
self:off(event, wrapper)
fn(...)
end
self:on(event, wrapper)
endRemoving Listeners
An :off method removes a specific listener by reference.
function EventEmitter:off(event, fn)
local list = self._listeners[event]
if not list then return end
for i, f in ipairs(list) do
if f == fn then table.remove(list, i); return end
end
endPriority Queues for Events
Add a priority field to each listener. Sort the list on registration so high-priority handlers run first.
Async Event Queue
Instead of calling listeners immediately, push events into a queue and process them each frame/tick.
local queue = {}
local function postEvent(event, data)
queue[#queue+1] = {event=event, data=data}
end
local function processEvents()
local current = queue
queue = {}
for _, e in ipairs(current) do
bus:emit(e.event, e.data)
end
endPreventing Modification During Emit
When emitting, iterate over a copy of the listener list so that adding/removing during emission does not cause bugs.
Namespaced Events
Use dotted event names ("player.damaged", "player.healed") to organize events by system.
Weak Listener Tables
Use weak-value listener tables so that if a subscriber object is GC'd, its listener is automatically removed — preventing leaks in long-running games.
Game Loop Integration
Integrate the event system with the game loop: input events → logic events → render events, processed in order each frame.
Event System Question
What is the main benefit of an event system?
Recap: Event Systems
Lua event systems use a table of named listener lists. Implement on, emit, off, and once for a complete dispatcher. Use async queues and weak tables in production games.
Learn Lua with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 40
- Lessons
- 159
Frequently Asked Questions
Is the “Event Systems in Lua” lesson free?
Yes — any 3 lessons of the Lua Academy learning path, including “Event Systems in Lua”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.
What will I learn in “Event Systems in Lua”?
Build a lightweight event dispatcher with callbacks stored in tables. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Event Systems in Lua” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Event Systems in Lua
- Component-Based Scripting
- AI Behavior with State Machines
- Data-Driven Config and Hot-Reload