Defining 'eventHandlers.key_up' as global works, but defining it as local does not.
local event = require "event"
local running = true
local function unknownEvent()
--
end
local eventHandlers = setmetatable({}, { __index = function () return unknownEvent end })
function eventHandlers.key_up(addr, char, code, playerName)
if (char == string.byte(" ")) then
running = false
end
end
local function handleEvent(eventID, ...)
if (eventID) then
eventHandlers[eventID](...)
end
end
while running do
handleEvent(event.pull ())
end
This is an example event handler used by the OpenComputers mod for Minecraft. 'Event' is basically a library that pulls signals from the computer (in this case, keyboard input) and tells the computer what to do with the event.
The program basically waits at the loop at the bottom, listening for an event. This example listens for a 'key_up' event. The event also gives additional information, such as the keyboard address, the character pressed, the player who pressed it etc.
In this example, lifting the spacebar after being pressed (a 'key_down' event) breaks the loop at the bottom and the program terminates.
I'm relatively new to lua coding, and I can work my way around relatively straightforward code, however here I'm stuck. I have two questions:
(1) Why is it that this works when the 'eventHandlers.key_up' function is defined as global, but not when defined as local?
(2) What is the metatable's role in this code? I've been researching metatables and metamethods but I'm finding it quite the difficult topic to grasp.
The error I get is this:
Line 11: '(' expected near '.'