Elapsed milliseconds since start of level

I need to get the time in milliseconds elapsed since the start of the level.
AGameState::ElapsedTime seems to be what I’m looking for, except it delivers a time in seconds.

Is there a way to access to this in a more simple way than managing a global-ish timer as a chronometer?

Yes, you can use the following :

float realtimeSeconds = UGameplayStatics::GetRealTimeSeconds(());

Basically, the UGameplayStatics class contains lots of neat stuff, and 2 or 3 other useful function regarding time.

If you want to get that in hours/minutes/seconds, you can use the FTimespan structure.

You can initialiaze the struct like so :

FTimespan timeSpan = FTimespan(/*Hours*/0,/*Minutes*/0, realtimeSeconds /*Seconds*/);

Then you can use that struct to extract the time the way you want.

Hope this helps!

2 Likes

Did my answer help you out?

Also you can use

FPlatformTime::Seconds();

It returns at least milliseconds at last microseconds

Either way it helped me so thanks :smiley:

Yeah, even 3 years later :smiley:

you can do it cleaner with FTimespan::FromSeconds(realtimeSeconds);

Is there a way to do this in Blueprint?

Sure Get Real Time Seconds | Unreal Engine Documentation

Also there is another way to do the same ()->GetRealTimeSeconds();. Note, that returning value is equal to such of Time node in Materials! (but check Ignore Pause in Time node!) Can be used to control animations without triggering parameters every single tick.

1 Like

This is in seconds, not in miliseconds.

3 Likes

it returns a float, so you just x1000 to get milliseconds

That would advance the time by the thousands and not in-between.

1000->2000->3000->etc

While what OP wants is to have the values in-between too

1 Like

getRealTimeSeconds return a value in float:
1,40654 seconde
so if you multiply by 1000, you get 1406 milliseconds, not 1000.

1 Like

Thank you, this was super helpful. I also used the FTimespan::ToString(format) method like this:

float GameTime = UGameplayStatics::GetRealTimeSeconds(GetWorld());
FTimespan TimeSpan = FTimespan(0, 0, GameTime);
return TimeSpan.ToString(TEXT("%m:%s"));

Just leaving it here as an additional note because it took me a while to figure out how to use the formatting options. Basically what I was missing is that the expected TCHAR* is generated by the TEXT macro.