This is more of a theoretical question than a problem: does anyone know if I use functions as keys to a table if there is a way to call the function given that I have a reference to the key? The question arose with my key binding system, where I have a table of keys that are bound with their associated function and modifier keys, and a table of functions with their associated key and category (eg. "System", "Player"). Here's snippet of the implementation:
Code: Select all
function Keybindings:add(key, func, mods, cat)
-- Required
assert(key and type(key) == "string")
assert(func and type(func) == "function")
-- Optional
assert(not mods or type(mods) == "table")
assert(not cat or type(cat) == "string")
self.keys[key] = {
func = func,
mods = mods
}
self.functions[func] = {
key = key,
cat = cat
}
end
Code: Select all
local keybindings = Keybindings()
keybindings:add("escape", love.event.quit)
local index = keybindings.functions[keybindings.keys["escape"].function]
Thanks in advance.