Game Development Community

Sprinting - changing $movementSpeed

by Infinitum3D · in Torque Game Engine · 06/15/2007 (8:40 am) · 10 replies

I'm trying to add a sprint feature.

Here's what I have so far-

in player.cs

MaxForwardSpeed = 50;

in default.bind.cs I've changed

$movementSpeed = 0.2;

This works fine. My player now jogs at a leisurely 10 m/s.

Then (still in default.bind.cs) I added this

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

and further down (still in default.bind.cs) I added

moveMap.bind(keyboard, shift w, sprint);

however, when I'm in-game, w still jogs forward at 10m/s and shift w does the same 10 m/s meaning my sprint function isn't working or my bind isn't correct.

Any suggestions?

Tony
not a programmer

#1
06/15/2007 (9:58 am)
If I'm not mistaken (not 100% positive) the %val variable is basically a true or false variable for when the key is being pressed down and up so you shouldn't be including it in your calculation.
#2
06/16/2007 (12:50 am)
I would suggest just binding a function to shift, rather than shift w, that increases the movement speed. I would also suggest not uing a global variable to change the speed - when the Player sprints, everyone else will move more quickly, too.
#3
06/16/2007 (12:53 am)
It's a client side variable - so everyone won't be affected.
#4
06/16/2007 (8:54 am)
Ah. In that case, carry on. :P
#5
06/16/2007 (11:18 am)
If nothing else works, a scripter's approach would be to just make two ArmorData's, at two different speeds. Then when the client requests, the server puts them into the faster armor, until the client takes their hands off the keys.
#6
06/18/2007 (9:54 am)
@ Maxwell - thanks. However, I'm a newbie at scripting. Can you give an example in code?

@ Steve - the reason I use %val in the equation is because that's how Torque is set up for forward motion bound to the W key.

@ Daniel - I tried using the same code bound directly to the SHIFT key as well as bound to Y and even commented out the line for W and tried binding my own code to w. None of it worked.

Any other help?

Thanks!

Tony
#7
06/18/2007 (10:36 am)
Sprint using script: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=12607

Sprint using engine: www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2697

edit: btw are you deleting the ~/client/config.cs file? You need to delete that.
#8
06/18/2007 (10:52 am)
Thanks mb. I've seen Amr Bekhit's Dynamically change player speed in script but I was trying to streamline it into just a function and bind.key without a server call, etc.

If pressing W moves you forward at 10 m/s, why can't multiplying that by five ($movementSpeed *5) move you forward at 50 m/s.

function moveforward(%val)
{
$mvForwardAction = %val * $movementSpeed ; //--- doesn't this determine rate of movement?
}


This should work:

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

but it doesn't. Is my *5 incorrect? Shouldn't that multiply it by 5?

Seems simple, but once again, I'm not a programmer. Maybe my syntax is off?

Thanks!

Tony
edit: There IS a syntax error. There has to be a space between * and 5.
edit2: Deleting config.cs DID IT!!!! Thanks mb!!

Here's my final solution;


in player.cs

MaxForwardSpeed = 50;


in default.bind.cs I've changed

$movementSpeed = 0.2;


This works fine. My player now jogs at a leisurely 10 m/s.


Then (still in default.bind.cs) I added this

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


and further down (still in default.bind.cs) I added

moveMap.bind(keyboard, "alt w", sprint);

Then, I deleted the config.cs file, deleted all my .dso's, and ran the game. It works! I jog at 10 m/s using the W key, and I run full speed using ALT+W to sprint.

No server calls. Just change two values and add a sprint function to default.bind.cs

Tony


BTW: I just added this as a resource www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=13082
#9
07/07/2007 (1:30 pm)
I just added this to Infinitum3D's great resource page. Thought I would add it here as well. Let me know if there are any flaws:

I've also been messing around lately with adding a scripted sprint function. Below I've included additional script for more intuitive keyboard manipulation. Every time a key is pressed or released, the script checks to see if other keys are currently pressed. So if you press 'lshift' (my sprint key) and 'w' and 'a', $mvForwardAction and $mvLeftAction will be set to their "sprinting" values. Then if you release 'lshift' while still holding 'w' and 'a', $mvForwardAction and $mvLeftAction will be set to their "non-sprinting" values. It works like this for keys bound to movement forward, backward, left, and right in my script. This is possible through the use of a few extra global variables associated with each key that hold a 1 or 0 depending on whether it is pressed or not. The code is below.

I set the following fields in my PlayerData datablock (in ./server/player.cs):

maxForwardSpeed = 50;
maxBackwardSpeed = 50;
maxSideSpeed = 50;

bind():
moveMap.bind( keyboard, lshift, sprint);
moveMap.bind( keyboard, a, moveleft );
moveMap.bind( keyboard, d, moveright );
moveMap.bind( keyboard, w, moveforward );
moveMap.bind( keyboard, s, movebackward );
moveMap.bind( keyboard, space, jump );

global variables and functions:
function sprint(%val)
{
   if(%val)
   {
      $sprinting = 1;
      if($movingLeft == 1)
         $mvLeftAction = %val * $movementSpeed * 3;
      if($movingRight == 1)
         $mvRightAction = %val * $movementSpeed * 3;
      if($movingForward == 1)
         $mvForwardAction = %val * $movementSpeed * 3;
      if($movingBackward == 1)
         $mvBackwardAction = %val * $movementSpeed * 3;
   }
   else
   {
      $sprinting = 0;
      if($movingLeft == 1)
         $mvLeftAction = 1 * $movementSpeed;
      else
         $mvLeftAction = 0 * $movementSpeed;
      if($movingRight == 1)
         $mvRightAction = 1 * $movementSpeed;
      else
         $mvRightAction = 0 * $movementSpeed;
      if($movingForward == 1)
         $mvForwardAction = 1 * $movementSpeed;
      else
         $mvForwardAction = 0 * $movementSpeed;
      if($movingBackward == 1)
         $mvBackwardAction = 1 * $movementSpeed;
      else
         $mvBackwardAction = 0 * $movementSpeed;
   }
}

function moveleft(%val)
{
   if(%val)
   {
      $movingLeft = 1;
      if($sprinting == 1)
         $mvLeftAction = %val * $movementSpeed * 3;
      else
         $mvLeftAction = %val * $movementSpeed;
   }
   else
   {
      $movingLeft = 0;
      $mvLeftAction = %val * $movementSpeed;
   }
}

function moveright(%val)
{
   if(%val)
   {
      $movingRight = 1;
      if($sprinting == 1)
         $mvRightAction = %val * $movementSpeed * 3;
      else
         $mvRightAction = %val * $movementSpeed;
   }
   else
   {
      $movingRight = 0;
      $mvRightAction = %val * $movementSpeed;
   }
}

function moveforward(%val)
{
   if(%val)
   {
      $movingForward = 1;
      if($sprinting == 1)
         $mvForwardAction = %val * $movementSpeed * 3;
      else
         $mvForwardAction = %val * $movementSpeed;
   }
   else
   {
      $movingForward = 0;
      $mvForwardAction = %val * $movementSpeed;
   }
}

function movebackward(%val)
{
   if(%val)
   {
      $movingBackward = 1;
      if($sprinting == 1)
         $mvBackwardAction = %val * $movementSpeed * 3;
      else
         $mvBackwardAction = %val * $movementSpeed;
   }
   else
   {
      $movingBackward = 0;
      $mvBackwardAction = %val * $movementSpeed;
   }
}
#10
07/08/2007 (1:12 pm)
Nice.