Game Development Community

Keyboard states Torque Script...

by Matt Sanders · in Torque Game Engine · 12/31/2004 (6:15 am) · 11 replies

I was wondering if anyone found a good way in torque script to detect if a key is being held down or not. What I am trying to do is make the vehicle controls more like that of unreal Tournament. When you let go of the turning button (left or right) the vehicle will straighten the tires up automatically.

I was trying to think of different ways of doing this but I am not sure how efficient they will be so I wanted to see if anyone out there has a good Idea on how to accomplish this.

#1
12/31/2004 (6:47 am)
You may want to check out the function calls in client/scripts/default.binds.cs

For example:


function toggleZoom( %val )
{
   if ( %val )
   {
      $ZoomOn = true;
      setFov( $Pref::player::CurrentFOV );
   }
   else
   {
      $ZoomOn = false;
      setFov( $Pref::player::DefaultFov );
   }
}

moveMap.bind(keyboard, e, toggleZoom);

In this code block, the function toggleZoom is bound to the 'e' key. So hitting 'e' will call this function. Whether the key is down or up is passed into the function by the variable %val. %val is 1 if the key is pressed, 0 is if is not pressed.

So for toggleZoom, when the key is down (%val == 1), zoom is turned on and the FOV is set to CurrentFOV.
When the key is released, the zoom is turned off and the FOV is set to DefaultFov.

That's why a lot of functions in default.binds.cs look like this:
function toggleFirstPerson(%val)
{
   if (%val)
   {
      $firstPerson = !$firstPerson;
   }
}

It's because they only want to be called on a key down event, not a key up event, so they make sure the key is only being pressed down by checking with if (%val)

Hope that helps!
#2
12/31/2004 (7:04 am)
WOW that is exactly the help I needed!! Thank you soooo much Drew you rule.
#3
12/31/2004 (7:20 am)
One question though.... when the key is up does that mean torque is still calling the function every cycle just passing %val as 0... I was wondering because I do not want it to make calculations every cycle.

EX: if I were to do something like

function turnLeft(%val)
{
if(%val)
{
$mvLeftAction = %val;
}
else
{
Calculate eye vector and set tires to be forward
}
}

Will torque constantly be calculating the eye vector and setting the tires forward
and if so would I have to add something like



......

if(%val)
{
needtocalculate = true;
$mvLeftAction = %val;

}
else
{

if(needtocalculate)
{
calculate eye vector and set tires to be forward
needtocalculate = false;
}


}
#4
12/31/2004 (7:28 am)
I think the commands are only called when the state of the button changes (up to down or down to up), but I can't guarantee that. Logically however, performance would absolutely suck if all of your keybinds were called each and every tick for the entire keyboard, so I think this is a valid, if unproven, assumption.
#5
12/31/2004 (7:54 am)
I set up traces and they are only called once. that was a stupid question because you are right it would logically bog.
#6
12/31/2004 (8:12 am)
Now I just need to find out what is controlling the angle of the tires and how to make the person go straight after the key is up....
#8
12/31/2004 (9:16 am)
No I did not see that.... I really wanted to try and accomplish it with Torque script but I think I will try that out to see how it works. I know that it would be way more efficeint in C++. Thank you Drew.
#9
12/31/2004 (9:47 am)
Ok now I get to uncove even more fun questions.... When I turn alot it will now go back to center (used The C++ tutorial) but the physics makes the vehicle spin out still. it is almost like it is sliding. I tried increasing the kinetic friction but this doesn't seem to help. what settings in the datablock could cause this and are their some general rules on how much you should set these settings.

also when I am just going straight the vehicle all of a sudden will turn. Thank you very much for your responses.
#10
12/31/2004 (10:28 am)
Matt,
I included code for keyboard steering that includes straightening out when the key is released in the recent update to the BraveTree Car Pack
#11
12/31/2004 (10:40 am)
Thanks Matthew. but the C++ code seems to be working fine for now.