Game Development Community

Bind a key to more than one function at the same time

by Diego Santos Leao - GameBlox Studio · in Torque Game Builder · 03/31/2008 (10:28 am) · 2 replies

Is it possible to bind a keyboard key to more than one function at the same time?

In my specific case, I want to call the "showDialog" function when the player presses a key near an NPC. To do this I created a binding for each NPC, to the same key. Something like this:

moveMap.bindObj("keyboard", "space", "showDialog", %npc1);
moveMap.bindObj("keyboard", "space", "showDialog", %npc2);

The problem is that only one of the binding works (I'm guessing this happens because you can't bind more than one function to the same key)... Any suggestions on how to this? My current solution uses triggers to bind the keyboard key only when the player enters it (and unbinds when he leaves), but this still feels a little "hacky".

#1
03/31/2008 (11:06 am)
Your showDialog function should be where it finds the relevant NPC.
#2
03/31/2008 (11:40 am)
What many do in this case is set up an input chain, something like:

function spacePressed(%val)
{
    if (%val)
    {
      showDialog($Game::closestNPC);
    }
}

moveMap.bind("keyboard", "space", spacePressed);

Of course, during your movement, you'll want to be updating the value of the global script variable $Game::closestNPC so that the spacePressed function calls the right one.

Another reason to do this type of indirect mapping is so that you can allow multiple keys to perform the same action, such as:

function spacePressed(%val)
{
    if (%val)
    {
      showDialog($Game::closestNPC);
    }
}

function aPressed(%val)
{
    if (%val)
    {
      showDialog($Game::closestNPC);
    }
}
moveMap.bind("keyboard", "a", aPressed);
moveMap.bind("keyboard", "space", spacePressed);

It's still a little hacky I admit, but Torque does not allow you to bind 2 input events to the same exact function, so the layer of indirection is required.