PDA

View Full Version : Questions about GetInputManager:AddBinding



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!

Seth
01-09-2007, 01:10 PM
If an EntityID is passed in, the function is called from that entities personal script namespace. In this way, a key can be bound independently by a thousand different entities and used in different ways. Doesn't need to be one giant input pump somewere.

The Beer invader's example binds the guns keys this way.

Btw, I put up some half-assed API documentation (http://www.rtsoft.com/novashell/docs/api) today, making progress though.