[Lua] Call table keys as functions?
Posted: Sat May 10, 2014 10:12 pm
Hey LÖVE forums, first post.
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:
Lets say I have a reference to an index of the function table.
So now index refers to the table for "love.event.quit" in the "keybindings.functions" table. Is there any way I could call the function used as the key via the key itself?
Thanks in advance.
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.