untilLoaded method

Future<void> untilLoaded(
  1. GameObject object, {
  2. Duration timeout = const Duration(seconds: 30),
  3. int fps = 60,
})

Waits for object to load (GameObject.loaded), running a frame each 5 ms of real time meanwhile, as the app would: for a game whose onLoad awaits a routine (a fade, wait, a tween), which goes on only in a frame, so a bare await game.loaded with no frames run would wait for ever. Throws what the load threw, or a TimeoutException after timeout of real time.

final game = world.add(MyGame());
await world.untilLoaded(game);

Implementation

Future<void> untilLoaded(GameObject object, {Duration timeout = const Duration(seconds: 30), int fps = 60}) async {
  (Object, StackTrace)? failed;
  var done = false;
  unawaited(
    object.loaded.then(
      (_) => done = true,
      onError: (Object error, StackTrace stack) {
        failed = (error, stack);
        done = true;
      },
    ),
  );
  final waited = Stopwatch()..start();
  while (true) {
    await Future<void>.delayed(const Duration(milliseconds: 5));
    if (done) break;
    if (waited.elapsed > timeout) {
      throw TimeoutException('$object had not loaded after ${timeout.inMilliseconds} ms', timeout);
    }
    update(1 / fps);
  }
  if (failed case (final error, final stack)) Error.throwWithStackTrace(error, stack);
}