flame-engine / flame

A Flutter based game engine.
https://flame-engine.org
MIT License
9.3k stars 914 forks source link

My flame game got lagging when i load many CameraComponent #3359

Closed tobioxd closed 3 weeks ago

tobioxd commented 3 weeks ago

What happened?

This is my project : https://github.com/tobioxd/pixel_adventure In pixel_adventure.dart class, when it load to next nevel, i create new Level And CameraComponent , if it is 3 first Level , it's okay but when got to 4 one, the fps is decrease, i try to limit dt is 1/60 but the game is very slow

What do you expect?

Someone help me to optimize my game and it can load smooth between each level thank you guys !

How can we reproduce this?

No response

What steps should take to fix this?

No response

Do have an example of where the bug occurs?

No response

Relevant log output

No response

Execute in a terminal and put output into the code block below

Output of: flutter doctor -v

Affected platforms

Android

Other information

No response

Are you interested in working on a PR for this?

spydon commented 3 weeks ago

How come you are using your own camera variable instead of the one that is built-in to FlameGame? https://github.com/tobioxd/pixel_adventure/blob/main/lib/game/screens/pixel_adventure.dart#L32

Can you have a look in the Flutter DevTools, you will have a Flame tab in there and where you can see the component tree, my bet is that you're having several cameras running in there rendering the old levels.

All this would be solved if you instead of using your own variables for world and camera use the built-in ones, you most likely don't even need to create new cameras, just reassign the world and the existing camera will start to render that one.

class MyGame extends FlameGame {
  MyGame() : super(
    camera: CameraComponent
      .withFixedResolution(width: 640, height: 480),
  );

  void _loadLevel() {
    ...
    // No need to add it to the tree manually or to add a new camera
    // everything is already handled under the hood in the setter.
    world = Level();
   }   
}
spydon commented 3 weeks ago

We should really update our documentation about this to make it clearer, I'll create an issue about that. Many seem to have followed Spellthorn's YouTube tutorial which was written before the camera and world was built-in, and then end up in this state.

tobioxd commented 3 weeks ago

Thank you !

spydon commented 3 weeks ago

@tobioxd does it work like it should now? If so we'll close this issue, since the documentation update will be handled in #3360.