I need to do some calculations between every update()
, so I need to ensure that a function is processed before the next frame.
Is there any way to achieve this? Just like "WaitForEndOfFrame" in Unity?
I need to do some calculations between every update()
, so I need to ensure that a function is processed before the next frame.
Is there any way to achieve this? Just like "WaitForEndOfFrame" in Unity?
You can override the updateTree
in your FlameGame
(or in your Component
, if you only want it to happen before the update
s of a subtree) and do your calculations right before all the other components start updating. This would be right after the last frame, except for the first frame, but that one you can skip by setting a boolean.
So something like this:
class MyGame extends FlameGame {
bool isFirstTick = true;
@override
void updateTree(double dt) {
if(!isFirstTick) {
// Do your calculations
} else {
isFirstTick = false;
}
super.updateTree(dt);
}
}
But I have to ask, why do you need to do this? render
won't be called until all the update
calls are done, do can't you just put your calculations in the normal update
method?
In Flutter we don't get an update()
function unlike Unity. That is in the default API that we use, there are ways to tap into something of that effect. Normally we use a Ticker
and create an animation to get periodic updates synced with screen refresh rate.
However, if what you are trying to do is to run something in between build()
calls, WidgetsBinding.instance!.addPostFrameCallback()
may be what you are looking for.
Here is a detailed answer that may help in this regard: https://stackoverflow.com/a/51273797/679553