Game Development Community

Going insane trying to get code to repeat

by Isaac Dutton · in General Discussion · 03/30/2006 (5:59 pm) · 1 replies

Alright, I am going crazy here guys, I feel like a complete noob, and general I know my way around torque pretty decently, but this is the first time doing something like this.

I wrote some code to rotate the player with the keyboard instead of the mouse (no I am not doing an rpg lols)
I am having trouble getting it to continue to run while holding the button.....

Please help me with this before I kill my self


This is the function which runs fine as long as I keep pressing the button

function TurnLeft(%val) //enter function with 1
{
while(%val==1) {
$mvYaw -= %val * ($cameraFov / 90) * 0.02; //adjust yaw
} else {
// Key up
}
}

This is my bind.....

moveMap.bind(keyboard, "a", turnLeft);

Thanks in advance for the help!

#1
03/31/2006 (4:05 am)
Hi Isaac

Well here's two ways you could go about it.

Method 1:

You could create your own pivot functions as follows...

$pref::Input::PivotTurnSpeed = 0.002;                  //pivot speed

function pivotLeft( %val )                             //pivot left function
{
   if(%val == 1) {                                     //while key down
      $mvYawRightSpeed = $pref::Input::PivotTurnSpeed; //yaw left at a speed of 0.002
   }
   else if (%val == 0) {                               //key released
      $mvYawRightSpeed = 0;                            //stop yaw
   }
}

function pivotRight( %val )                           //pivot right function
{
   if(%val == 1) {                                    //while key down
      $mvYawLeftSpeed = $pref::Input::PivotTurnSpeed; //yaw right at a speed of 0.002
   }
   else if (%val == 0) {                              //key released
      $mvYawLeftSpeed = 0;                            //stop yaw
   }
}

moveMap.bind( keyboard, a, pivotLeft );
moveMap.bind( keyboard, d, pivotRight );

Don't forget to delete client/config.cs for your new key bindings to take effect.

Method 2:

You could use the functions that already exist for mouse yaw and bind them to the keyboard:

//These two functions should already exist in default.bind.cs

function turnLeft( %val )
{
   $mvYawRightSpeed = %val ? $Pref::Input::KeyboardTurnSpeed : 0;
}

function turnRight( %val )
{
   $mvYawLeftSpeed = %val ? $Pref::Input::KeyboardTurnSpeed : 0;
}

//All you have to do is bind them to the keyboard

moveMap.bind( keyboard, a, turnLeft );
moveMap.bind( keyboard, d, turnRight );

To alter the speed adjust...

$pref::Input::KeyboardTurnSpeed = 0.1;

...accordingly, which can be found inside client/prefs.cs