Timer that goes forward

Godot Version

4.2.2

Question

how can I make a timer that goes forward like 1s to 5s and also 1s to untill I stop it.
Does Godot has any like that?

I’ve used similar logic like this before:

var timer
var is_timer_on : bool = false

func process(delta):
       if is_timer_on:
             timer += 1 * delta
func start():
       is_timer_on = true
func pause:
       is_timer_on = false
func stop():
      is timer_on = false
      timer = 0
3 Likes

I just dawned on what you mean. You guys want to make a timer to build on. No. Because a timer is a built in function. That’s a rotary function in the CPU. That’s part of the engine. It’s not even part of delta. I get what you want. But, the documentation clearly states you don’t want that. Because then all the functions, ranges, syntaxes and processes fall out of use. You can’t apply range: to timer. For example.

Ok, but what if I need to count time. Like i am moving a object A to B and i want to see how much time it takes to reach the B point.

1 Like

You could just do : time += delta only while a variable like is-moving is true, once it’s set to false you now have the time it took to move to position b.

If you don’t want it in process, you could try something like:

var timer : float = 0.0
var step : float = 1.0
func run_timer:
      Await get_tree().create_timer(step).timeout
      timer += step
      if is_moving:
           run_timer()

The logic might need some editing, but it follows closely to a new method I’m about to use for spawning enemies in my own project, and the nice thing is that it stays outside of the process you could even lower the step for a more precise time.

Edit: just tried it the next day and it does work, I noticed that if you make the step 0.01 the decimals get ready large, so all you have to do is run the timer through a string that will round it to the nearest 2 decimals.

1 Like

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