Game Development Community

Using the shift key to run

by Manoel Neto · in Torque Game Engine · 01/24/2005 (10:41 am) · 28 replies

Hi,

I've been trying to get the shift key to double the movement speed for hours, to no avail. I just cannot bind anything to the shift keys, the functions are simply not called. I tried creating shift* versions of the move functions and mapping those to "shift a", "shift d", etc, but it doesn't work properly (pressing shift while walking won't change the speed).

How do I get the shift key to trigger a bind function?
Page «Previous 1 2
#1
01/24/2005 (10:45 am)
This may sound like a stupid question, but did you specify which shift key in the scripts?

I seem to recall being able to bind left and right shift keys to seperate functions.

~ Always start simple. =\
#2
01/24/2005 (11:35 am)
Can we see the bind call
#3
03/17/2005 (1:51 pm)
Sorry for taking so long...

Well, today, months after skipping this problem and moving onto other things, I just found it is working now. I held shift down by accident and got running. Heh.
#4
03/21/2005 (2:47 pm)
Quote:Can we see the bind call
Quote:@Manoel: We are waiting the bind call, could you write it here?

I'm trying to bind shift to a function too. Could you post the bind call please?
#5
03/21/2005 (3:04 pm)
Could ya post it perhaps? I tried this but only got it to change when you stopped walking then started again while holding shift. And i could really use this. ;)
#6
03/21/2005 (5:39 pm)
Isnt it annoying when people dont post the solution to a problem, and just drops it.. hehe.

@Manoel... could you please post the bind call, and maybe the run function too (so we can use it)
#7
03/23/2005 (5:42 am)
Sorry, sorry! Here:

moveMap.bind( keyboard, lshift, shiftSpeed );

In my case it doesn't quite work as it should, but it's due to my shiftSpeed() function. I need to re-write it so it'll re-adjust the move variables' values with the new move speed.
#8
03/25/2005 (12:24 pm)
Well, we know how to map the key.. least i did. What i need to see is the function to make ya run though. ;)
#9
03/26/2005 (12:59 am)
Ya that'd help
#10
03/27/2005 (6:19 pm)
I've got something. It's a bit hacked, but it works:

core/client/scripts/defaults.bind.cs
function run(%val)
{
    playerChangeSpeed(%val);
}

engine/game/player.cc
ConsoleFunction(playerChangeSpeed,void,2,2,"playerChangeSpeed(speed)")
{
	Player* thePlayer = NULL;
   thePlayer = dynamic_cast<Player*>(Sim::findObject("thePlayer"));
	if(thePlayer && argc>1)
	{
		thePlayer->changeSpeed(dAtoi(argv[1]));
	}
}

. . . .

void Player::changeSpeed(S32 run)
{
	if(getPlayerPosition()==1) {
		mDataBlock->maxSideSpeed=mDataBlock->maxBackwardSpeed=mDataBlock->maxForwardSpeed= (run ? 10 : 7);
	}
}
Note, in the ConsoleFunction definition, "Sim::findObject("thePlayer")" assumes that the player object is labeled "thePlayer".

Edit: almost forgot engine/game/player.h, maked in bold
public:
   OrthoBoxConvex mConvex;
   Box3F          mWorkingQueryBox;
   [b]void changeSpeed(S32 run);[/b]

  protected:
   void setState(ActionState state, U32 ticks=0);
   void updateState();
#11
03/27/2005 (6:55 pm)
Paul,

It seems rather overkill to modify the engine just to change the player's speed (unless you are doing something else too). What about:

$movementSpeed = 0.6;
$walkSpeed = 0.6;
$runSpeed = 1.0;

function shiftSpeed(%val)
{
   if(%val)
      $movementSpeed = $runSpeed;
   else
      $movementSpeed = $walkSpeed;
}

function moveforward(%val)
{
   $mvForwardAction = %val * $movementSpeed;
}

function movebackward(%val)
{
   $mvBackwardAction = %val * $movementSpeed;
}

moveMap.bind( keyboard, w, moveforward );
moveMap.bind( keyboard, s, movebackward );
moveMap.bind( keyboard, lshift, shiftSpeed );

Thanks.
#12
03/27/2005 (7:10 pm)
With that one, you have to stop moving before your speed changes. :( I'll try the engine one in a min
#13
03/27/2005 (7:30 pm)
Hmm, Derk's post could work if you just put some boolean into the moveforward and movebackward functions. You could check that boolean in shiftSpeed, and call moveforward or movebackward as appropriate.
#14
03/27/2005 (7:42 pm)
I'm *constantly* confuzed by the way key bindings work, so please forgive me if I make a fool of myself here :) Wouldn't you be able to do something like this:

$mvIsRunning = 0;
function shiftSpeed(%val)
{
   if(%val)
      $mvIsRunning = 1;
   else
      $mvIsRunning = 0;
}

function moveforward(%val)
{
   while($mvIsRunning = 1) {
      $mvForwardAction = %val * $runSpeed;
   }
   while($mvIsRunning = 0) {
      $mvForwardAction = %val * $walkSpeed;
   }
}
Or am I missing something about how the bindings work?
#15
03/27/2005 (7:55 pm)
No, moveforward is only called when 'w' is pressed or released. Therefore, your modifications wouldn't change anyting until you press (not hold) 'w'.
#16
03/27/2005 (8:07 pm)
Yeah I thought so but I wasn't sure about how they worked... Why not reproduce the move forward function in the shift key function. Lets make a fool of myself AGAIN with ANOTHER code snippet :)


$movementSpeed = 0.6;
$walkSpeed = 0.6;
$runSpeed = 1.0;
$mvIsRunning = 0;

function shiftSpeed(%val)
{
   if(%val) {
      $mvIsRunning = 1;
      if($mvForwardAction > 0) {
         $mvForwardAction = %val * $runSpeed;
      } else if($mvBackwardAction > 0) {
         $mvBackwardAction = %val * $runSpeed;
      }
   } else {
      $mvIsRunning = 0;
      moveForward(%val);
      moveBackward(%val);
   }
}

function moveforward(%val)
{
   if($mvIsRunning = 1)
      $mvForwardAction = %val * $runSpeed;
   else
      $mvForwardAction = %val * $walkSpeed;
}

function movebackward(%val)
{
   if($mvIsRunning = 1)
      $mvBackwardAction = %val * $runSpeed;
   else
      $mvBackwardAction = %val * $walkSpeed;
}
Ok I'll stop now :)

EDIT: Thought of a better way to do this, and updated the code
#17
03/27/2005 (8:24 pm)
Greetings,

Yea, I was afraid of having to repress the keys to get it updated. Since you have a good grasp of modifying the script, here is my throttle which is constantly updated. It should have everything you need.
$movementSpeed = 0;
$movementDirection = 1; // Direction of travel: 1 forward -1 reverse

function incMove(%val) {
  if (%val) {
    speedUp();
  } else {
    speedStop();
  }
}
moveMap.bind(keyboard, "up", incMove);

function decMove(%val) {
  if (%val) {
    speedDown();
  } else {
    speedStop();
  }
}
moveMap.bind(keyboard, "down", decMove);

$speedIncrement = 0.01; // step at every update
$speedRate = 31; // how often it is updated (every tick)

function speedUp() {
  if ($movementDirection > 0) { // if moving forward
    $movementSpeed += $speedIncrement; // increase speed
    if ($movementSpeed >= 1.00) { // if past maximum
      $movementSpeed = 1.00;  // set to maximum
    }
  } else { // if moving backward
    $movementSpeed -= $speedIncrement; // decrease speed
    if (($movementSpeed < $speedIncrement) && ($movementSpeed > -$speedIncrement)) { // cover rounding errors
      $movementSpeed = 0.00;
    }
    if ($movementSpeed < 0.00) { // if speed is negative
      $movementSpeed = -1 * $movementSpeed; // switch speed sign
      $movementDirection = -1 * $movementDirection; // switch to forward
    }
  }
  $mvForwardAction = $movementDirection * $movementSpeed;
  if (isEventPending($SpeedSchedule))
    cancel($SpeedSchedule);
  $SpeedSchedule = schedule( $speedRate, 0, "speedUp");
}

function speedDown() {
  if ($movementDirection > 0) { // if moving forward
    $movementSpeed -= $speedIncrement; // decrease speed
    if (($movementSpeed < $speedIncrement) && ($movementSpeed > -$speedIncrement)) { // cover rounding errors
      $movementSpeed = 0.00;
    }
    if ($movementSpeed < 0.00) { // if speed is negative
      $movementSpeed = -1 * $movementSpeed; // switch speed sign
      $movementDirection = -1 * $movementDirection; // switch to reverse
    }
  } else { // if moving backward
    $movementSpeed += $speedIncrement; // increase speed
    if ($movementSpeed >= 1.00) { // if past maximum
      $movementSpeed = 1.00; // set to maximum
    }
  }
  $mvForwardAction = $movementDirection * $movementSpeed;
  if (isEventPending($SpeedSchedule))
    cancel($SpeedSchedule);
  $SpeedSchedule = schedule( $speedRate, 0, "speedDown");
}

function speedStop() {
  if (isEventPending($SpeedSchedule))
    cancel($SpeedSchedule);
}
#18
03/30/2005 (12:36 pm)
Uhhh Throttle? what does that have to do with the other code?
The other guys were binding shift to a key to run, there only seems to be move forward and backward being touched there?
#19
03/30/2005 (12:51 pm)
Ed,

The point was to show how to get the shift key to change the speed without having to release and repress the move key.

Everytime the speed functions are called, they can check to see if the shift key is pressed and if so, change the speed to run and then if not change it back to walk.

Thanks.
#20
03/30/2005 (1:21 pm)
Kinda like this pseudocode?

$speedIncrement = 0.01; // step at every update
[b]$runspeedIncrement = 0.05; // run speed inc?[/b]
$speedRate = 31; // how often it is updated (every tick)

function speedUp() {
  if ($movementDirection > 0) { // if moving forward
[b]// if SHIFT is being pressed.... [/b]
[b]//     $movementSpeed += $runspeedIncrement; // increase speed by run inc[/b]
[b]// ELSE [/b]
    $movementSpeed += $speedIncrement; // increase speed
[b]// end if[/b]
    if ($movementSpeed >= 1.00) { // if past maximum
      $movementSpeed = 1.00;  // set to maximum
    }
  } else { // if moving backward - no running backwards!

. . . 

  $mvForwardAction = $movementDirection * $movementSpeed;
  if (isEventPending($SpeedSchedule))
    cancel($SpeedSchedule);
  $SpeedSchedule = schedule( $speedRate, 0, "speedUp");
}
Page «Previous 1 2