Download

Need help with programming Stamina in strictly C++

I am trying to program stamina where 5 stamina is taken away for each second of running. When stamina hits 0, the speed of the character is supposed to to go back to normal speed. When StopSprint() is called, the stamina should start regenerating at 2 stamina per second. I was able to code the sprinting aspect without the stamina limitation completely fine where the character will get faster while the r button is pressed. However, when I programmed the stamina in, it compiled fine but when I press the r button the engine instantly freezes. I assume this is due to an overload but I am not sure what I am doing wrong.

Attached are some pictures of my code. I am using some timer functions that are described here: https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Timers/index.html

It is very likely that I am using these timer functions wrong since I do not completely understand how the iterations work. When I first tried it out I did not ahve the clear timer functions, so I added them after I figured that the program was creating infinite timers that would just cause the system to freeze, but the timer clears didn’t fix the problem.

Any help would be appreciated! I know that some of my comments might be a little off and its not very stylistic at the moment but I did not have much time to refine it and really want to fix this problem first.

Let me see if I understood this correctly, when the player presses R it triggers the sprint function and this sprint function is then called many times a second for as long as R is held and so you then create and clear a timer. This results in a timer being created and cleared 60-120 a second?.

If that is happening then you probably don’t want to clear the timer in the sprint function, instead use a repeating timer and check the timer is active in the sprint function, if not then create it. Ultimately you just want the timer to begin when R is pressed calling an update stamina function every second until R is released when you can stop the timer.

The sprint function is only called once. When the R button is pressed down the MaxWalkSpeed is changed to 1200, when the button is released, StopSprint is called which not only changes the MaxWalkSpeed back to 600 but also starts regenerating the stamina. However, there is an if statement in the sprint function that checks to see if the stamina is 0, in which case stop sprint is called. I am going to try what you suggested and will get back to you.

I took your advice with a little bit of moving around code and it ended up working. I first commented out the while loops and realized that those were what causing the over load. I then set the loop parameter so set timer to true, but made sure to put a conditional statement in both the sprint and stop sprint where the timer would be cleared once stamina was empty and full, respectively.

Thanks!