How do I prevent the character from sprinting when the stamina value is 0?

I’m currently using Unreal Engine 4.6, but I’m having a few problems with blueprints. I’ve set up so that if you press left shift and move character the character will sprint. I want to implement where if you sprint it will deplete your stamina progress bar, but because the stamina bar is empty you won’t be able to sprint in till it recharges. That’s the issue I’m having…the character is still able to sprint even with an empty stamina bar, and I would like to prevent the character from sprinting in till the stamina bar has recharged. Here’s a picture of the blueprints, and if its not too much trouble could you please help with this issue. Kind regards,

Whats up with all those branches? Also why dont you try using timers + bool that controls whatever you can sprint or not

I am new to blueprints. I’ll attempt the timers + bool right now. :slight_smile:

I’m actually struggling with this, I’ve only started using blue prints a few days ago, and I literally have no idea on how to make my character stop sprinting when the stamina value reaches 0.

I’ve attached an image that might help you understand how to use Blueprints to accomplish something like this. Right click to view the image in full resolution.

I’ll explain it here in text, and if you have any questions please ask.

First I use a boolean variable “IsSprinting” to keep track of whether or not the sprinting key is currently being pressed. I use a “FlipFlop” node here. The “FlipFlop” node will alternate between A and B each time it receives input. So when shift is pressed, it fires off A, which sets “IsSprinting” variable to true. Then when shift is released, it fires B, which sets “IsSprinting” to false. Now we can check the variable “IsSprinting” to see the state of the sprint key anywhere in our blueprint.

Next, I use the “Event Tick” node to check every frame if “IsSprinting” is true (shift is pressed), and also if “Stamina” is greater than zero. They both must be true in order for the “AND” node to output true. If these conditions are true (checked with a branch node), then we set “Speed” variable equal to our “Sprint Speed” variable. If they’re false, we set “Speed” to “Walk Speed” variable. These variables have default values set in the details panel below the “My Blueprint” section.

Please keep in mind that this is not a complete or optimized solution.

Some things wrong with this example:

  • Speed is being set every frame,
    when really it only needs to happen
    once per key press/release.
  • Stamina isn’t being reduced/recovered
  • Speed isn’t being applied to the character

Hopefully this can get you going on the right track. There are many different ways to do this, and I encourage you to check out all the fantastic Blueprint tutorials on the Unreal Engine Youtube channel.

Won’t this raise a condition where player is in constant state between being unable to sprint and being able to sprint? This is what I am going through rn haven’t come across anything to fix it atm. If any suggestion please let me know.

I, personally, don’t understand what you mean? (I mean, I do, but I don’t understand why it’s an issue for you.)

Could you elaborate?

lol didn’t knew I would get reply in few hours on this thread. Anyways so I was talking about how would one go about setting a delay/ cooldown period in such case. If I setup a function with 0> condition wouldn’t the stamina bar deplete to 0 and as soon as it recovers by 0.1 with sprint key held, the whole flow would execute over again. In other words, there has to be some kind of in-between logic which is stopping the execution from happening simultaneously, like generally in game you wouldn’t be able to sprint for a bit right after sprinting. If you got anything on mind let me know ^^ I was just curious how would people handle that, couldn’t find much material on this specific execution.

1 Like

Ah! Give me 5 minutes…

Okay, of course Unreal decides to take FOREVER to load when I need it to open quickly.

In a rush, I forgot to tick a few boxes. Does this clarify for you?

So, this would disable sprinting for the delayed amount? Cuz I do have similar logic let me share, if you could see where the problem might be let me know. Cuz in theory I am going for the same thing lol.



1 Like

Okay, so where is the recharge mechanic that temporarily disables sprinting until its requirement is fulfilled?

(See: STAMINA_DELAY_EVENT)

Wont these branches be enough? if a function is necessary what would be the flow of it? I did try making a function for delay but it didn’t work so I removed it.

Do you have a timer somewhere that manages the cooldown?

I don’t see the cooldown code.

ah no I did use a timer for recovery but i dont think i used it for any thing other then that. So i believe i need to use a time by function node?

I create my own timers, so, I’m not 100% sure how those premade work in the Engine.

But, assuming you use the timer correctly, the timer will act as a switch that is:

  1. Only activated when STAMINA DEPLETED
  2. Overrides any stamina usage until its condition has been met. (i.e. The timer event has completed)
1 Like

Ok, it would be helpful if you would whip out a abstract demo if possible even for future people if anyone stumbles upon this same situation lol (I know I am being greedy). other then that, I will try and follow what you said hopefully it will be easy enough ^^ Thanks again my friend!

1 Like

Please, if “greedy” saves time in programming, be greedy.

A rundown:

“Can_Only_Attack_When_Stamina_Not_Depleted”
-I created a variable that tests the condition of whether stamina is depleted. By DEFAULT it is not. So, the condition (BRANCH) will come back FALSE, and the character will attack. If the STAMINA_DEPLETED comes back TRUE, then the character can not attack.

-But we have to create a condition for this to occur.

“STAMINA_DEPLETED”
-This line of code ALWAYS RUN (at least in combat mode). It constantly checks to see if the character has stamina. It’s a loop function. If the character’s stamina is set to “0” (the Stamina FLOAT), It will stop looping, by following the FALSE scenario and head to the TRUE scenario, where the EXECUTION PULSE will set STAMINA_DEPLETED to TRUE. Now the player can no longer attack. The pulse will now make its way to the STAMINA_DEPLETION_DELAY event.

“STAMINA_DELAY_EVENT”
-This is the “timer” I created that adds +1 to the Stamina FLOAT value every .1 seconds. That means, the player has to wait 10 seconds before they can attack (.1 x 100 = 10). Once Stamina FLOAT equals 100, the function will stop looping, finally taking the TRUE path, unsetting the STAMINA_DEPLETED Boolean. The player can attack again.

Further, the “STAMINA_DEPLETED” line of code will reinitiate, checking, nonstop, to see if the player has FULLY depleted their stamina. If you have a recharging system, this condition very well may never be met.

1 Like

ahh so thats how you are doing it, i guess then i will have to do it in the component since recovery is inside there its only logical to put it in there or maybe in character itself. Thanks again for explaining so well! I will post the result here once I am done with the design ^^

1 Like

how to avoid decreasing stamina while pressing the key?