Why does withFrameNanos return inconsistent values...
# coroutines
m
Why does withFrameNanos return inconsistent values (details in thread)
I have this game loop:
Copy code
suspend fun loop(
    speed: GameSpeed,
    action: (motion: Float) -> Unit
) {
    withFrameNanos { time ->
        val sinceLast = time - lastFrame
        val motion = speed.value / (sinceLast)
        lastFrame = time
        action(motion)
    }
}
whenever i restart this loop, the value produced by ‘motion’ is very different from the previous ones
i cannot figure out why
k
Is this a compose thing?
withFrameNanos
is also a function on monotonic frame clocks.
m
not compose. I’m building a game loop with GraphicsSurface
k
What is your definition of
withFrameNanos
then? Or a link to documentation if you didn’t write it?
m
i cannot get the function to return relatively stable inputs
yes, its compose. sorry
k
Which platform are you running on?
m
android
k
Time based things really aren’t guaranteed to be very precise
just in general
There might be more specific, better time synchronization with compose, but I am unsure.
m
ya. i might need to tune this impl to a specific fps, so i get constant output
k
For example, here’s an impl of monotonic frame clock that I wrote for mac os. You’re dependent on when the platform says there should be another frame rendered. https://github.com/cashapp/molecule/blob/trunk/molecule-runtime/src/quartzCoreMain/kotlin/app/cash/molecule/DisplayLinkClock.kt
I am unsure of the android frame clock.
z
There are no guarantees about the clock values on compose’s side. We just pass through whatever we get from Choreographer. E.g, If you’re on a device that dynamically adjusts refresh rate, they could change.
☝️ 2
☝🏾 1