C++ Stamina Drain Not Working

Hello,

I’ve built a basic stamina system for my survival project, but have encountered a small yet significant issue. I have my sprint function bound to a L Shift action binding and when I press it, my speed increases and my stamina decreases. I also have a check for velocity to make sure that the player is moving before the stamina is drained. This also works.

The problem arises when I start the game, press shift, but don’t immediately start moving forward. No stamina is drained, which is correct, but if I continue to hold it down and then press W to move forward, it doesn’t drain stamina. I have my ReduceStamina function called in the velocity check. I believe the problem has something to do with the fact that the if statement is only being called once, then the velocity is read as zero and it doesn’t recheck it. I tried a while loop and a for loop and my computer crashed.

The forum won’t let me share images of my code because “I’m a new member,” so hopefully you guys can understand what I’m describing. Any advice is welcome.

One way of doing that could be, when you start sprint then SetMaxWalkSpeed and call your DecreaseStamina function with a timer that loops per some time and check if velocity is not 0 then decrease stamina. And when stop Shifting just clear or stop the timer. This way you have to reduce the stamina by small ammount in your DecreaseStamina function.
I’m not sure how timers look like in c++ though, but maybe you find this way helpful.

Another way could be, when your Event that starts the run movement fires, check if isSprinting (maybe based on current Max Walk Speed) and fire DecreaseStamina function again.