In which format GetTimerElapsed() returns its value?

Namely, I do have a time handler bound to a function I’m calling each tenth of a second, like so:

FTimerHandle myTimer; // this is a global variable
GetWorldTimerManager().SetTimer(myTimer, this, &AMyClass::countTime, .1, true);

Inside the function I’m monitoring the lifetime of my timer, logging the results:

 void MyClass::countTime()
    {
        float timeElapsed = GetWorldTimerManager().GetTimerElapsed(myTimer);
        UE_LOG(LogTemp, Log, TEXT("Time Elapsed: %f"), timeElapsed);
    }

This is what I get from the logging:

> LogTemp: time elapsed: 0.100002
> LogTemp: time elapsed: 0.100004
> LogTemp: time elapsed: 0.100005
> LogTemp: time elapsed: 0.100007
> LogTemp: time elapsed: 0.100009
> LogTemp: time elapsed: 0.100011
> LogTemp: time elapsed: 0.100013
> LogTemp: time elapsed: 0.100014
> LogTemp: time elapsed: 0.100016
> LogTemp: time elapsed: 0.100017
> LogTemp: time elapsed: 0.100019
> LogTemp: time elapsed: 0.100021
> LogTemp: time elapsed: 0.100022
> LogTemp: time elapsed: 0.100023
> LogTemp: time elapsed: 0.100024
> LogTemp: time elapsed: 0.100026
> LogTemp: time elapsed: 0.100028
> LogTemp: time elapsed: 0.100029
> LogTemp: time elapsed: 0.100030
> LogTemp: time elapsed: 0.100032
> LogTemp: time elapsed: 0.100034
> LogTemp: time elapsed: 0.100035
> LogTemp: time elapsed: 0.100037
> LogTemp: time elapsed: 0.100038
> LogTemp: time elapsed: 0.100039
> LogTemp: time elapsed: 0.100041
> LogTemp: time elapsed: 0.100042
> LogTemp: time elapsed: 0.100044
> LogTemp: time elapsed: 0.106723
> LogTemp: time elapsed: 0.104173
> LogTemp: time elapsed: 0.104175
> LogTemp: time elapsed: 0.104176
> LogTemp: time elapsed: 0.104179
> LogTemp: time elapsed: 0.104180
> LogTemp: time elapsed: 0.104222
> LogTemp: time elapsed: 0.104224
> LogTemp: time elapsed: 0.104226
> LogTemp: time elapsed: 0.104227
> LogTemp: time elapsed: 0.104228
> LogTemp: time elapsed: 0.104229
> LogTemp: time elapsed: 0.104230
> LogTemp: time elapsed: 0.106897
> LogTemp: time elapsed: 0.106899
> LogTemp: time elapsed: 0.106901
> LogTemp: time elapsed: 0.106977
> LogTemp: time elapsed: 0.106979
> LogTemp: time elapsed: 0.106980
> LogTemp: time elapsed: 0.106982
> LogTemp: time elapsed: 0.106984
> LogTemp: time elapsed: 0.106986
> LogTemp: time elapsed: 0.106987
> LogTemp: time elapsed: 0.106989
> LogTemp: time elapsed: 0.106991
> LogTemp: time elapsed: 0.106992
> LogTemp: time elapsed: 0.106993
> LogTemp: time elapsed: 0.106995
> LogTemp: time elapsed: 0.106996
> LogTemp: time elapsed: 0.110875

It looks like the float I’m getting has an arbitrary starting value of 0.1; then its sixth decimal tells me time elapsed in tenth of a seconds; then things just go crazy, as in the jump from 0.100044 to 0.106723:

> LogTemp: time elapsed: 0.100044
> LogTemp: time elapsed: 0.106723

If anybody could shed some light on how time gets actually counted that’d be rad!

Cheers,
f

I don’t completely understand the question but this is what I see:

You start a timer that fires every 0.1 seconds. You get the time that has elapsed since the last execution (in seconds) of that timer every time it is executed, and that time is about 0.1 seconds, which was to be expected. So it works exactly as you planned, doesn’t it?

It will never be exactly 0.1 seconds due to the way timers are executed and rounding errors but it is nearly accurate.

If you wanted to have the complete elapsed time since the start you could do something like:

// at the beginning
FDateTime TimerStartTime = FDateTime::Now();

// in the timer
float ElapsedSeconds = (FDateTime::Now() - TimerStartTime).GetTotalSeconds();

also the FTimer should probably be a FTimerHandle.

Hi ,

my issue is that

GetWorldTimerManager().GetTimerElapsed(myTimer);

doesn’t return the value I would expect:

  • I fire my Timer every 0.1 seconds, which means my countTime() function is called every 0.1 seconds (and yes, my bad in the OP I corrected that, the FTimer is actually a FTimerHandle);

  • Since I’m logging and calling GetWorldTimerManager().GetTimerElapsed(myTimer) from within that function, I would expect this output:

    0.10000

    0.20000

    0.30000

    0.40000

    0.50000

    0.60000
    […]

  • however that’s not what I get (see my first post). Giving the results I’m actually getting, I started thinking the GetWorldTimerManager().GetTimerElapsed(myTimer) wasn’t returning time in seconds, but in some other format. But no format would explain the sudden jump from 0.100044 to 0.106723; or other sudden, apparently completely arbitrary result I would get out of it.

  • Hence my question. What does GetWorldTimerManager().GetTimerElapsed(myTimer) actually returns?

At this point that’s mostly a curiosity I got, as I solved the problem in a similar way to the one you recommended. But I’d love to be able to use GetWorldTimerManager().GetTimerElapsed(myTimer) as it’d make for a more elegant solution, and save the conondrum or logging time manually and allocating two more floats.

Cheers

f