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

fix(NA): black frame when activating overlays #1093

Merged
merged 10 commits into from
Nov 17, 2021
Merged

Conversation

renancaraujo
Copy link
Member

@renancaraujo renancaraujo commented Nov 12, 2021

Description

There is a nasty black frame every time GameWidget rebuilds. Fixing that by memoizing the onLoad future that loads the game only once. Since changing the game instance between builds is already an anti-pattern, there is no problem caching the load state.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors and are passing (See Contributor Guide).
  • My PR does not decrease the code coverage, or I have a very special case and explained on the PR description why this PR decreases the coverage.
  • I updated/added relevant documentation (doc comments with ///) and updated/added examples in doc/examples.
  • I have formatted my code with flutter format and the flutter analyze does not report any problems.
  • I read and followed the Flame Style Guide.
  • I have added a description of the change under [next] in CHANGELOG.md.
  • I removed the Draft status, by clicking on the Ready for review button in this PR.
  • I am willing to follow up on review comments in a timely manner.

Breaking Change

Does your PR require Flame users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md).
  • No, this is not a breaking change.

Related Issues

Replace this paragraph with a list of issues related to this PR from the issue database. Indicate, which of these issues are resolved or fixed by this PR.

@@ -39,6 +39,7 @@ mixin Loadable {
/// implementing class, it is cached so that it can be reused when the parent
/// component/game/widget changes.
@internal
@nonVirtual
Copy link
Member Author

Choose a reason for hiding this comment

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

Made this to make it non-overridable even inside flame.

Copy link
Member

Choose a reason for hiding this comment

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

so like a final! cool, TIL

@@ -138,11 +138,11 @@ class _GameWidgetState<T extends Game> extends State<GameWidget<T>> {

MouseCursor? _mouseCursor;

Future<void> get _loaderFuture {
late final Future<void> _loaderFuture = (() {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this will work, now it will only run onMount once, at the same time as onLoad?

Copy link
Member Author

@renancaraujo renancaraujo Nov 13, 2021

Choose a reason for hiding this comment

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

Actually it is off because it will not run onmount at all (if onload returns a non null future). I am not sure why we should run onmount every time the widget tree rebuilds?

Copy link
Member Author

Choose a reason for hiding this comment

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

Whatever is the logic between onload and unmount, we should not generate a new future every time the build method is called, since a widget rebuild may happen even because of things not related to Flame. In the previous implementation, we cached the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Bottom line:: since Game is now a component, this seems more like a component lifecycle and should be treated as such: inside Flame. Hooks to the widget lifecycle may, be more specialized (on widget mount, on widget rebuild etc).

Copy link
Member

Choose a reason for hiding this comment

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

I agree, it shouldn't trigger on each widget rebuild. But, we need onMount to trigger if the GameWidget is moved from one place in the tree to another (but is already loaded), or if it is removed and then later added back to the tree, is this even possible to differentiate from a normal widget rebuild?

Copy link
Member Author

@renancaraujo renancaraujo Nov 13, 2021

Choose a reason for hiding this comment

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

There are some possibilities here:

  • The package user removes GameWidget from the tree temporarily: this unmounts the widget and kills the state (unless there is a global key keeping it), de-caching the future.
  • The package user moves GameWidget in the widget tree: Same as above.
  • The package user changes the game in which it passes to the GameWidget: The future is de-cached here.

If the user keeps the state in a global key, we should assume it wants to keep things as it is.

Copy link
Member

Choose a reason for hiding this comment

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

Alright, seems like this is the way to go then. Are the lifecycle docs in line with this?

Copy link
Member

@luanpotter luanpotter left a comment

Choose a reason for hiding this comment

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

LGTM, honestly I am just trusting you tested and it worked as I don't immediately see the connection but I see how it could have some impact

@spydon spydon enabled auto-merge (squash) November 17, 2021 10:13
@spydon spydon merged commit 85caf46 into main Nov 17, 2021
@spydon spydon deleted the renan/fix-gamewidget branch November 17, 2021 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants