Game Development Community

Adding a Sprint function

by Infinitum3D · 06/20/2007 (8:42 am) · 12 comments

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;
}

Shouldn't that multiply it by 5?

Seems simple, but once again, I'm not a programmer.

Here's my final solution;


in player.cs set your fastest movement speed

MaxForwardSpeed = 50;


in default.bind.cs I've changed movement speed from 1

$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 sprint function (just after function moveforward(%val)

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


and further down (still in default.bind.cs) I added this call just after moveMap.bind(keyboard, w, moveforward);

moveMap.bind(keyboard, "alt w", sprint); //--call the Sprint function

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

Note: Instead of "alt w" you could use a plain letter key like s without the quotations, as long as s is not bound to anything else.

I'd like to thank Amr Bekhit for inspiration and assistance, and mb for clarifying many, many things for me. These two deserve credit for this, not me.

Tony

#1
06/21/2007 (10:55 pm)
Great resource!

I have a small problem, though. When I do this, The game loads fine, but when I'm in the game, it won't let me do anything. I'm pretty sure I've done everything correctly.
#2
06/25/2007 (12:39 pm)
"it won't let me do anything"

Do you mean the mouse doesn't work? or the movement keys don't work? or more literally, the game loads and NOTHING else happens?

Can you ~ into the console to see if any errors are reported?

Let me know more.

Thanks!

Tony
#3
06/30/2007 (1:34 am)
Well, the only things that respond are the F10 and F11 keys. I can't get into the console, either.
#4
07/02/2007 (5:11 pm)
it means you effed up your default.bind.cs file and it did not compile. it had a syntax error.
#5
07/07/2007 (1:25 pm)
Thanks Infinitum3D for the great resource!

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;
   }
}
#6
08/13/2007 (10:53 pm)
Thanks for the quick reference. Now to link it to the my sprint icon and make it so you have a limited amount of energy before the key stops working. Then once the energy bar fills up again it will work.
If anyone has any ideas on this it will help out greatly.
#8
01/05/2008 (4:07 pm)
I don't know why but this completely failed for me. I sprint all the time and can't walk about.
Though I got to admit, I'm having more fun flying over hills that just walking.

I'll experiment with it for a bit.
#9
02/28/2008 (6:33 pm)
Did you change this
$movementSpeed = 1; to $movementSpeed = 0.2; in default.bind.cs?

I have the issue where the actual sprint button doesn't work, I'm going to take a look into that.
#10
05/13/2008 (5:02 pm)
i did my sprint the same way, and i even got the energy to drain, but when i try to play multiplayer the energy drain is all messed up. the machine acting as server works fine, but the clients only drain energy when they are running and the server is sprinting. heres what i have in the default.bind.cs
function sprintforward(%val)
{
$mvForwardAction = %val * $movementSpeed * 2;
if(%val)
{
playerbody.runEnergyDrain = 1;
}
if(!%val)
{
playerbody.runEnergyDrain = 0;
}
}

im fairly sure that the whole server/client thing is because i use playerbody.runEnergyDrain, but i can't seem to figure out how to fix it. any ideas?
thanks,
Jason
#11
10/07/2008 (8:13 am)
I made a slight change to your code, and I haven't tried it in multiplayer yet, so I don't know if it makes a difference, but my default.bind looks like this:

function sprint(%val)
{
$mvForwardAction = %val * $movementSpeed * 3;
if (%val)
{
playerbody.runEnergyDrain 1;
}
else playerbody.runEnergyDrain = 0.2;
}


Again, I'm not a programmer, so that may be the exact same as what you have, but it does work singleplayer (of course, so does yours)...

Ideally, I'd prefer it to be tied into the the amount of energy. For example,
$mvForwardAction = %val * $movementSpeed * ENERGY

That way, as energy decreases, so does movement speed, but I can't find where/how ENERGY is actually named or defined.

Tony
#12
10/09/2008 (5:14 pm)
I have been trying to get a sprint function for a while now.
I have tried a few methods that have been posted, but none of them accomplish
the task. The post by Zachary Seldess makes the most sense to me but I can't get it
to work. Would any of you take a look at my player.cs, config.cs, and default.bind.cs and tell
me what I'm not doing right?

Email: Ingot@kc.rr.com

Since you don't have contact info in your profile Zach, could you contact me sometime?