Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Pause game when backgrounded #2642

Merged
merged 5 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions packages/flame/lib/src/game/game_render_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,10 @@ class GameRenderBox extends RenderBox with WidgetsBindingObserver {

void _bindLifecycleListener() {
WidgetsBinding.instance.addObserver(this);
didChangeAppLifecycleState(
WidgetsBinding.instance.lifecycleState ?? AppLifecycleState.resumed,
);
}

void _unbindLifecycleListener() {
WidgetsBinding.instance.removeObserver(this);
didChangeAppLifecycleState(AppLifecycleState.paused);
}

@override
Expand Down
4 changes: 4 additions & 0 deletions packages/flame/lib/src/game/game_widget/game_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ class GameWidgetState<T extends Game> extends State<GameWidget<T>> {
currentGame = widget.game!;
}
currentGame.addGameStateListener(_onGameStateChange);
currentGame.lifecycleStateChange(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, it's pretty weird to call these manually since these state changes should be coming from the framework.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes more sense for the game to act identically to if the app is backgrounded when really it was just unmounted.
This makes the link between "backgrounding the app" and "backgrounding the game (widget)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plus as an added bonus, it makes the behaviour more consistent on non-mobile platforms since the game will be paused when the widget is disposed of

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean, it feels like the state machine could become inconsistent with the one on Flutter's side though, which might confuse developers and be bug prone.

WidgetsBinding.instance.lifecycleState ?? AppLifecycleState.resumed,
);
_loaderFuture = null;
}

Expand All @@ -262,6 +265,7 @@ class GameWidgetState<T extends Game> extends State<GameWidget<T>> {
/// `currentGame`'s `onDispose` method will be called; otherwise, it will not.
void disposeCurrentGame({bool callGameOnDispose = false}) {
currentGame.removeGameStateListener(_onGameStateChange);
currentGame.lifecycleStateChange(AppLifecycleState.paused);
currentGame.onRemove();
if (callGameOnDispose) {
currentGame.onDispose();
Expand Down