Is there anyway you can get the elasped time?

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

Im trying to get the elapsed time from when you started the game, is there anyway you can do that

:bust_in_silhouette: Reply From: jgodfrey

Godot offers a number of time-oriented functions. To get an elapsed time, you really just need to get a delta between the start time and the current time. One core function you could us is OS.get_unix_time(). With two such values, you can calculate an elapsed time using something like this:

var time_start = 0
var time_now = 0

func _ready():
    time_start = OS.get_unix_time()

That’ll store an initial start time. Then, at the point where you want to get an elapsed time, you just need to call that function again, and subtract the two values for a delta-time. The result will be the elapsed seconds between the two values. For example:

func _process(delta):
    time_now = OS.get_unix_time()
    var time_elapsed = time_now - time_start
    print(time_elapsed)

Note, in the example, the elapsed time is calculated each frame in _process. That’d be useful if you want some visual, running elapsed timer as it’d constantly update. If you really just need to calculate the time and display it at some specific point in time (say, when the game ends), you don’t need to capture the current time in each frame. You can instead just grab that second value whenever you need it (at game end, for example).

Again, the result is in elapsed seconds, which you could convert to any display format you want…

:bust_in_silhouette: Reply From: Calinou

If you want to know the time elapsed in milliseconds since the project has started, use:

OS.get_ticks_msec()

Or in microseconds:

OS.get_ticks_usec()

Note: Godot 4 use Time.get_ticks_msec() and Time.get_ticks_usec()

thiagola92 | 2022-12-29 08:27

Thank you lol couldn’t find this anywhere

P0werman1 | 2023-05-10 16:59