How to make a progress bar that shows fire rate, if you shoot the bar goes down and reset?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By antoniodev

I’m new to programming and I need help. I have a timer that is the fire rate timer that was created in a yield but, how do I make a progress bar that visually shows the fire rate timer?

Thanks for the help.

:bust_in_silhouette: Reply From: exuin

It depends. You could use a Timer node and update the value of the progress bar to be the time_left on the timer each frame. Or you could use a Tween or AnimationPlayer to change the value. There’s lots of ways to do this.

:bust_in_silhouette: Reply From: Legorel

I would replace the yield with a Timer node
You can use aProgressBar node with this Timer:

  • Add a Timer node to your scene and set its wait time to the fire rate
  • Set One shot to true (because we want the timer to stop on timeout)
  • Add a ProgressBar and set its max value to the fire rate
  • You can change Visible to show/hide the percentage

I used a button for the fire event, but it can be triggered by anything you want:

var can_fire = true

onready var progress_bar = $ProgressBar
onready var button = $Button
onready var cooldown= $Cooldown

func _ready():
  button.connect("pressed", self, "_fire")
  cooldown.connect("timeout", self, "_cooldown")

# when the button is clicked
func _fire():
  # can the player fire ?
  if can_fire:
    can_fire = false
    # add the rest of the code when the player fire here

# when the cooldown is over
func _cooldown():
  # player can now shoot again
  can_fire = true

func _physics_process(delta):
  # set the ProgressBar value to the timer that has passed since the start of the cooldown
  progress_bar.value = timer.wait_time - timer.time_left