Ian
01-09-2007, 02:39 AM
First, what is the third argument? It looks like it requires an entity ID, but I have yet to figure out why. :confused:
Secondly, I wondered why it used the name of a function rather than the function itself as a first-class value. I decided to make a version that did this because I'm insane. This was a lot more difficult (fun) then I first guessed.
I need some goofy helper functions first
-- Executes a string of code and returns the result if there is one.
function Exec(s) return assert(loadstring(s))() end
-- Returns a unique number for a function.
function ID(object) return string.sub(tostring(object), -8) end
Now for AddBinding:
function AddBinding(key, function_)
-- I need to store this argument in a global for Exec(), which only works
-- with global names.
_lastBoundFunction = function_
-- Create a new, unique function name.
local functionName = "_BindingHandler_" .. key .. "_" .. ID(function_)
-- Dynamically create a new, global function.
Exec(functionName .. " = _lastBoundFunction")
GetInputManager:AddBinding(key, functionName, C_ENTITY_NONE)
end
I guess I should include RemoveBinding in case someone was dumb enough to use a function I made and needs to unexplode their computer:
function RemoveBinding(key, function_)
local functionName = "_BindingHandler_" .. key .. "_" .. ID(function_)
Exec(functionName .. " = nil")
GetInputManager:RemoveBinding(key, functionName, C_ENTITY_NONE)
end
Hurray, now I can do this:
AddBinding("escape", function (keydown)
return keydown and DefaultEscapeHandler()
end)
Why is this useful? It's not!
Secondly, I wondered why it used the name of a function rather than the function itself as a first-class value. I decided to make a version that did this because I'm insane. This was a lot more difficult (fun) then I first guessed.
I need some goofy helper functions first
-- Executes a string of code and returns the result if there is one.
function Exec(s) return assert(loadstring(s))() end
-- Returns a unique number for a function.
function ID(object) return string.sub(tostring(object), -8) end
Now for AddBinding:
function AddBinding(key, function_)
-- I need to store this argument in a global for Exec(), which only works
-- with global names.
_lastBoundFunction = function_
-- Create a new, unique function name.
local functionName = "_BindingHandler_" .. key .. "_" .. ID(function_)
-- Dynamically create a new, global function.
Exec(functionName .. " = _lastBoundFunction")
GetInputManager:AddBinding(key, functionName, C_ENTITY_NONE)
end
I guess I should include RemoveBinding in case someone was dumb enough to use a function I made and needs to unexplode their computer:
function RemoveBinding(key, function_)
local functionName = "_BindingHandler_" .. key .. "_" .. ID(function_)
Exec(functionName .. " = nil")
GetInputManager:RemoveBinding(key, functionName, C_ENTITY_NONE)
end
Hurray, now I can do this:
AddBinding("escape", function (keydown)
return keydown and DefaultEscapeHandler()
end)
Why is this useful? It's not!