0 votes

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

in Engine by (31 points)

2 Answers

+2 votes
Best answer

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...

by (10,856 points)
selected by
+2 votes

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()
by (9,376 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.