Hello,
I made a stamina/sprint system and it’s working pretty well but i want to add some features. When I’m holding a shift key the stamina is decreasing and the character is going faster untill the stamina goes down to 0. After that character can’t sprint untill the stamina goes to 20 and when I hit the shift key again character is sprinting but if I’m still holding the shift key the character isn’t sprinting but stamina is going down. I have to release the shift key and hit it again to get the character sprinting and decrease the stamina. How to fix it?
You have to be VERY careful when you are using TIMERS and DELAYS.
More than likely, a variable somewhere isn’t being set quickly enough, so you have to re-execute the Input Action to free it up.
To double-check this, I’d place a PRINT_STRING node between “Can_Sprint?” and BRANCH to see exactly when the “Can_Sprint?” variable is being reset.
I think when you release the SHIFT/ACTION button, it executes the “Completed” output (again), which resets the variable.
I’d also attach the output pin of “Can_Sprint?” into the input text pin of a PRINT_STRING node and attach it to your EVENT_TICK so you can see the “Can_Sprint?” value in real time.
You were right, when I release the SHIFT button, it executes the “Completed” output. I realized that as long as the SHIFT button is pressed, the “Started” output is still executing, so I added a branch at the end of “Started” blueprint. It solved the problem. Thank you
One way to fix this issue is by adjusting your code to detect when the shift key is released and pressed again while the stamina is below 20. Here’s a basic outline of how you could modify your code:
# Define your sprinting mechanics and stamina system
class Player:
def __init__(self):
self.stamina = 100
self.sprinting = False
def update(self):
if self.sprinting:
self.stamina -= sprint_depletion_rate
if self.stamina <= 0:
self.stamina = 0
self.sprinting = False
# Inside your main game loop or event handling function
player = Player()
while game_running:
# Check for keyboard input
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] and player.stamina > 0:
player.sprinting = True
else:
player.sprinting = False
# If the player presses the shift key again while stamina is below 20, start sprinting again
if keys[pygame.K_LSHIFT] and player.stamina < 20:
player.sprinting = True
# Update player
player.update()
# Other game logic and rendering...
This code checks if the player presses the shift key while stamina is below 20, and if so, sets player.sprinting to True, allowing them to sprint again without releasing and pressing the shift key again. This should fix the issue you described. Adjust the values and logic as needed for your specific game.