SOLVED Working Sprint into Existing Movement Script

ERK

Member
Hello, I'm attempting to add a sprint & stamina system in an existing movement script.

Original:
GML:
hSpeed = lengthdir_x(inputMagnitude * speedWalk, inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedWalk, inputDirection);

PlayerCollision();

var _oldSprite = sprite_index;
if (inputMagnitude != 0)
{
    direction = inputDirection;
    sprite_index = spriteWalk;
}  else sprite_index = spriteIdle;
if (_oldSprite != sprite_index) localFrame = 0;

PlayerAnimateSprite();
What I've tried and can't seem to get working:
GML:
hSpeed = lengthdir_x(inputMagnitude * speedWalk, inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedWalk, inputDirection);

PlayerCollision();

var _oldSprite = sprite_index;
if (keySprint) && (inputMagnitude != 0) && (global.stamina > 0)
{
    direction = inputDirection;
    sprite_index = spriteSprint;
    global.stamina -= 1;
}
else if (inputMagnitude != 0)
{
    direction = inputDirection;
    sprite_index = spriteWalk;
}
else
{
    sprite_index = spriteIdle;
}

if (_oldSprite != sprite_index) localFrame = 0;

PlayerAnimateSprite();
Most of it is working, however; when the player gets to 0 stamina, the sprite doesn't update to the spriteWalk, it stays as spriteSprint.

I'm also trying to figure out how to make the player move *2 when sprinting and then back to regular speed when stamina reaches 0.

Thank you for any help!
 
Last edited:

Nidoking

Member
Where and how often are you increasing global.stamina? It's possible that you're dropping it to zero and then immediately increasing it again before you get back to this check.
 

ERK

Member
That seems to be the problem. I turned my global.stamina regen off to test and it works.

It's currently right under the rest of the code above, which is in the player's step event.
GML:
global.stamina += 0.5;
global.stamina = clamp(global.stamina, 0, 100);

Any ideas where it should be placed? Also, I'm not sure how to change the players movement speed in each.
 
Last edited:

Nidoking

Member
Movement speed is a number. Looks like you call it speedWalk. You'd make it a different number to give it a different value.

What you really should be doing is using some sort of state tracking rather than just monitoring inputs alone. You need to set a minimum amount of stamina to enter the sprint state. The way you have it, the half point of stamina the player recovers every step allows them to keep sprinting.
 

ERK

Member
Thank you for your help! I got it working!

GML:
hSpeed = lengthdir_x(inputMagnitude * speedWalk, inputDirection);
vSpeed = lengthdir_y(inputMagnitude * speedWalk, inputDirection);

PlayerCollision();

var _oldSprite = sprite_index;
if (inputMagnitude != 0)
{
    if (keySprint) && (global.stamina > 0)
    {
    direction = inputDirection;
    sprite_index = spriteSprint;
    global.stamina -= 1;
    speedWalk = 1.5;
    }
    else
    {
    direction = inputDirection;
    sprite_index = spriteWalk;
    speedWalk = 1;
    }
}
else
{
    sprite_index = spriteIdle;
}

if (_oldSprite != sprite_index) localFrame = 0;

PlayerAnimateSprite();

if !(keySprint)
{
    global.stamina += 0.5;
}

global.stamina = clamp(global.stamina, 0, 100);
Really just moved the stamina regen to only happen when not holding the sprint key and also adjust speedWalk to different values.
 
Top