Skip to content

Instantly share code, notes, and snippets.

@witchica
Created September 14, 2015 22:19
Show Gist options
  • Save witchica/e26b76c2b97942429cd8 to your computer and use it in GitHub Desktop.
Save witchica/e26b76c2b97942429cd8 to your computer and use it in GitHub Desktop.
local event = require "event" -- load event table and store the pointer to it in event
local char_space = string.byte(" ") -- numerical representation of the space char
local running = true -- state variable so the loop can terminate
function unknownEvent()
-- do nothing if the event wasn't relevant
end
-- table that holds all event handlers, and in case no match can be found returns the dummy function unknownEvent
local myEventHandlers = setmetatable({}, { __index = function() return unknownEvent end })
-- Example key-handler that simply sets running to false if the user hits space
function myEventHandlers.key_up(adress, char, code, playerName)
if (char == char_space) then
running = false
end
end
-- The main event handler as function to separate eventID from the remaining arguments
function handleEvent(eventID, ...)
if (eventID) then -- can be nil if no event was pulled for some time
myEventHandlers[eventID](...) -- call the appropriate event handler with all remaining arguments
end
end
-- main event loop which processes all events, or sleeps if there is nothing to do
while running do
handleEvent(event.pull()) -- sleeps until an event is available, then process it
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment