Need help implementing a stamina system for my player!

Godot Version

4.1.3.stable

Question

Hi IndieQuantum here,

I need a little help with properly making a stamina system for my player.

The player can hold onto a wall, but will lose stamina and fall or slide down the wall. I tried to implement this in my player script but I does not work.

Here is the script (sorry for the messy script):
var JumpHeight = -320
var VariableJumpHeight = -60
var DoubleJumpHeight = -300
var Speed = 100
var Gravity = 250
var Acceleration = 90
var DoubleJump = 1
var BufferedJump = false
var CoyoteJump = false
var WallSlideFriction = 300
var IsWallSliding = false
var StaminaDepleted = false

func _physics_process(_delta):
var Friction = false

#Gravity------------------------------------------------------------------------
velocity.y = min(velocity.y + 22.5, Gravity)

#Movement-----------------------------------------------------------------------
if Input.is_action_pressed(“move_right”):
velocity.x = min(velocity.x + Acceleration, Speed)
$AnimatedSprite2D.play(“run”)
$AnimatedSprite2D.flip_h = false
elif Input.is_action_pressed(“move_left”):
velocity.x = max(velocity.x - Acceleration, -Speed)
$AnimatedSprite2D.play(“run”)
$AnimatedSprite2D.flip_h = true
else:
Friction = true
$AnimatedSprite2D.play(“idle”)

#Jump, Double Jump, Jump Buffer-------------------------------------------------
if Input.is_action_just_released(“jump”) and velocity.y < VariableJumpHeight:
velocity.y = VariableJumpHeight
if is_on_floor() or CoyoteJump:
DoubleJump = 1
if Input.is_action_just_pressed(“jump”) or BufferedJump:
velocity.y = JumpHeight
BufferedJump = false
if Friction == true:
velocity.x = lerp(float(velocity.x), 0.0, 0.4)
else:
if Input.is_action_just_pressed(“jump”) and DoubleJump > 0:
velocity.y = DoubleJumpHeight
DoubleJump -= 1
if Input.is_action_just_pressed(“jump”):
BufferedJump = true
$JumpBufferTimer.start()
if Friction == true:
velocity.x = lerp(float(velocity.x), 0.0, 0.07)
if velocity.y > 0 and not is_on_floor():
$AnimatedSprite2D.play(“fall”)
$AnimatedSprite2D.visible = true
$AnimatedSprite2D2.visible = false
elif velocity.y < 0:
if DoubleJump > 0:
$AnimatedSprite2D.play(“jump”)
else:
$AnimatedSprite2D2.play(“double_jump”)
$AnimatedSprite2D2.visible = true
$AnimatedSprite2D.visible = false

#Wall Hold----------------------------------------------------------------------
if Input.is_action_pressed(“wall_grab”) and is_on_wall():
if $LeftWallDetector.is_colliding() and StaminaDepleted:
position += Vector2.LEFT
$AnimatedSprite2D.play(“wall_jump”)
$AnimatedSprite2D.flip_h = true
DoubleJump = 1
velocity.y = min(velocity.y + 0, 0)
DoubleJumpHeight = 0
JumpHeight = 0
velocity.x = 0
$StaminaTimer.start()
elif $RightWallDetector.is_colliding() and StaminaDepleted:
position += Vector2.RIGHT
$AnimatedSprite2D.play(“wall_jump”)
$AnimatedSprite2D.flip_h = false
DoubleJump = 1
velocity.y = min(velocity.y + 0, 0)
DoubleJumpHeight = 0
JumpHeight = 0
velocity.x = 0
$StaminaTimer.start()
else:
Gravity = 250
DoubleJumpHeight = -300
JumpHeight = -320

#Wall Slide---------------------------------------------------------------------
if is_on_wall() and not is_on_floor():
if Input.is_action_pressed(“move_left”) or Input.is_action_pressed(“move_right”):
IsWallSliding = true
$AnimatedSprite2D.play(“wall_jump”)
Gravity = 50
DoubleJumpHeight = 0
else:
IsWallSliding = false

var was_on_floor = is_on_floor()

#Overall Movement---------------------------------------------------------------
move_and_slide()

#Coyote Timer-------------------------------------------------------------------
var just_left_ground = not is_on_floor() and was_on_floor
if just_left_ground and velocity.y >= 0:
CoyoteJump = true
$CoyoteJumpTimer.start()

func _on_jump_buffer_timer_timeout():
BufferedJump = false

func _on_coyote_jump_timer_timeout():
CoyoteJump = false

func _on_stamina_timer_timeout():
StaminaDepleted = false

Please write the codes properly, I means for a code format you can write ` for triple times so in the body, paste your codes like:

#Hello World
#I used ``` codes ``` for it
1 Like

Write your code like this:

```gdscript

Code here

```

1 Like

sorry, now I know for next time:

var JumpHeight = -320
var VariableJumpHeight = -60
var DoubleJumpHeight = -300
var Speed = 100
var Gravity = 250
var Acceleration = 90
var DoubleJump = 1
var BufferedJump = false
var CoyoteJump = false
var WallSlideFriction = 300
var IsWallSliding = false
var StaminaDepleted = false

func _physics_process(_delta):
var Friction = false

#Gravity------------------------------------------------------------------------
velocity.y = min(velocity.y + 22.5, Gravity)

#Movement-----------------------------------------------------------------------
if Input.is_action_pressed(“move_right”):
velocity.x = min(velocity.x + Acceleration, Speed)
$AnimatedSprite2D.play(“run”)
$AnimatedSprite2D.flip_h = false
elif Input.is_action_pressed(“move_left”):
velocity.x = max(velocity.x - Acceleration, -Speed)
$AnimatedSprite2D.play(“run”)
$AnimatedSprite2D.flip_h = true
else:
Friction = true
$AnimatedSprite2D.play(“idle”)

#Jump, Double Jump, Jump Buffer-------------------------------------------------
if Input.is_action_just_released(“jump”) and velocity.y < VariableJumpHeight:
velocity.y = VariableJumpHeight
if is_on_floor() or CoyoteJump:
DoubleJump = 1
if Input.is_action_just_pressed(“jump”) or BufferedJump:
velocity.y = JumpHeight
BufferedJump = false
if Friction == true:
velocity.x = lerp(float(velocity.x), 0.0, 0.4)
else:
if Input.is_action_just_pressed(“jump”) and DoubleJump > 0:
velocity.y = DoubleJumpHeight
DoubleJump -= 1
if Input.is_action_just_pressed(“jump”):
BufferedJump = true
$JumpBufferTimer.start()
if Friction == true:
velocity.x = lerp(float(velocity.x), 0.0, 0.07)
if velocity.y > 0 and not is_on_floor():
$AnimatedSprite2D.play(“fall”)
$AnimatedSprite2D.visible = true
$AnimatedSprite2D2.visible = false
elif velocity.y < 0:
if DoubleJump > 0:
$AnimatedSprite2D.play(“jump”)
else:
$AnimatedSprite2D2.play(“double_jump”)
$AnimatedSprite2D2.visible = true
$AnimatedSprite2D.visible = false

#Wall Hold----------------------------------------------------------------------
if Input.is_action_pressed(“wall_grab”) and is_on_wall():
if $LeftWallDetector.is_colliding() and StaminaDepleted:
position += Vector2.LEFT
$AnimatedSprite2D.play(“wall_jump”)
$AnimatedSprite2D.flip_h = true
DoubleJump = 1
velocity.y = min(velocity.y + 0, 0)
DoubleJumpHeight = 0
JumpHeight = 0
velocity.x = 0
$StaminaTimer.start()
elif $RightWallDetector.is_colliding() and StaminaDepleted:
position += Vector2.RIGHT
$AnimatedSprite2D.play(“wall_jump”)
$AnimatedSprite2D.flip_h = false
DoubleJump = 1
velocity.y = min(velocity.y + 0, 0)
DoubleJumpHeight = 0
JumpHeight = 0
velocity.x = 0
$StaminaTimer.start()
else:
Gravity = 250
DoubleJumpHeight = -300
JumpHeight = -320

#Wall Slide---------------------------------------------------------------------
if is_on_wall() and not is_on_floor():
if Input.is_action_pressed(“move_left”) or Input.is_action_pressed(“move_right”):
IsWallSliding = true
$AnimatedSprite2D.play(“wall_jump”)
Gravity = 50
DoubleJumpHeight = 0
else:
IsWallSliding = false

var was_on_floor = is_on_floor()
#Overall Movement---------------------------------------------------------------
move_and_slide()

#Coyote Timer-------------------------------------------------------------------
var just_left_ground = not is_on_floor() and was_on_floor
if just_left_ground and velocity.y >= 0:
CoyoteJump = true
$CoyoteJumpTimer.start()

func _on_jump_buffer_timer_timeout():
BufferedJump = false

func _on_coyote_jump_timer_timeout():
CoyoteJump = false

func _on_stamina_timer_timeout():
StaminaDepleted = false

Edit your original post instead by clicking this:
image

  • Make sure your code is properly indented, so that it is readable.
  • Explain your code more. A statement like “but it doesn’t work” is far from helpful.
  • A final tip is to delete this post and create a new, proper one.
    It’s likely that most are going to ignore this when they see that it already has several answers.
2 Likes

Nothing in the code you have posted indicates that Stamina is ever reduced. The only occurence of “Stamina” is in the concept of StaminaTimer and StaminaDepleted which according to the context of the code you have shared, seems to only ever be false.

1 Like

For creating stamina, do like this:

var stamina = 100

func _physics_process(delta):
  if is_on_wall():
    stamina -= 20 * delta
  elif stamina < 100 and is_on_floor():
    stamina += 20 * delta

  if stamina <= 0:
    #do whatever you want... if you need help you can tell me
  stamina = clamp(stamina, 0, 100)

Otherwise what do you want to do with StaminaDepleted?

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.