I can't figure out a simple stamina/sprint code

Godot Version

Godot_v4.2.1

Question

Hey, I’m pretty new to Godot and coding, and I’ve been having trouble with making a simple sprinting/stamina system. What I was going for was a simple stamina bar with a max value of 100, and when it hits 0, you get a fatigue timer for about 2 seconds before you are able to sprint again, and I’m not to sure on how to do that. That’s not the only thing though, because I’m not sure how to incorporate actually moving faster when you click shift, and how to force stop sprinting when the fatigue timer hits.

This is all the code I got so far…

@onready var stamina = $TextureProgressBar

func _ready():
stamina.value = stamina.max_value

func _process(delta):
if Input.is_action_pressed(‘run’):
stamina.value -= 2
speed = 800
else: speed = 500
stamina.value += 1

I know… its pretty sad. I had more but I deleted it because it didn’t work, and couldn’t get it to work, so I just went back to what did, and I’ve been fiddling with this for a while but never could get it, and eventually went back to that, and just couldn’t, so now I’m here.

heres my whole walking/animation coding with that if needed.

extends CharacterBody2D

var speed = 500
var friction = 0.01
var acceleration = 0.1

@onready var stamina = $TextureProgressBar

@onready var animations = $AnimationPlayer

func get_input():
velocity = Vector2()
var input = Vector2()
if Input.is_action_pressed(‘right’):
input.x += 1
if Input.is_action_pressed(‘left’):
input.x -= 1
if Input.is_action_pressed(‘down’):
input.y += 1
if Input.is_action_pressed(‘up’):
input.y -= 1
return input

func _ready():
stamina.value = stamina.max_value

func _process(delta):
if Input.is_action_pressed(‘run’):
stamina.value -= 2
speed = 800
else: speed = 500
stamina.value += 1

func updateanimation():
if velocity.length() == 0:
animations.play(“idle”)
else:
var direction = “down”
if velocity.x < 0: direction = “left”
elif velocity.x > 0: direction = “right”
elif velocity.x < 0: direction = “up”
animations.play(“run”)
if velocity.x < 0: $Sprite2D.flip_h = true
elif velocity.x > 0: $Sprite2D.flip_h = false

func _physics_process(_delta):
var direction = get_input()
if direction.length() > 0:
velocity = velocity.lerp(direction.normalized() * speed, acceleration)
else:
velocity = velocity.lerp(Vector2.ZERO, friction)
move_and_slide()
updateanimation()

Velocity.y ?

There are no checks on stamina.value if it equals zero.

I think if you want to do some sort of exhaustion, you would check if stamina value is zero and run input is still held.

I haven’t worked extensively with animations, but there is an animation tree class that operates like a state machine. I think you could leverage the animation to stall the character for 2 seconds while it plays the animation.

To stall I would add a new Boolean variable called exhausted that is set true during the animation, then when the animation ends set back to false. This will be checked in your speed code and set your speed to zero or really low.

1 Like

okay, that would make sense. alright, ill try it and see what happens, thank you!
ill let you know how it goes