0 votes

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.

Godot version 3.2.3
in Engine by (56 points)

2 Answers

+1 vote
Best answer

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:
    print("Fire!")
    can_fire = false
    timer.start()
    # 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
by (272 points)
selected by
+1 vote

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.

by (7,148 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.