0

I have the component below added to a FlameGame class, whenever I am changing this component I have to hot restart the app. Is there a workaround for this?

class SoldierSprite extends SpriteAnimationComponent {
  @override
  Future<void>? onLoad() async {

    var miniSprites =
        await TexturePackerLoader.fromJSONAtlas('mini.png', 'mini.json');

    var miniSpriteComponent = SpriteComponent()
      ..sprite = miniSprites[4]
      ..size = Vector2(100, 200)
      ..position += Vector2(200, 200)
      ..angle = -95;

    add(miniSpriteComponent);

    return super.onLoad();
  }
}
spydon
  • 9,372
  • 6
  • 33
  • 63
Davoud
  • 2,576
  • 1
  • 32
  • 53

2 Answers2

1

Hot reload only works for changes in the widget tree.

Since your game lives within the GameWidget, Flutter can't know that it has changed if you don't update for example the key in the GameWidget when you do a hot reload.

spydon
  • 9,372
  • 6
  • 33
  • 63
-1

I also encountered this problem and noticed that hot reloading does work when you wrap your GameWidget inside a non GameWidget, e.g.:

class MyPage extends StatelessWidget {
  const MyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('New Game'),
      ),
      body: GameWidget(
        game: MyGame(),
      ),
    );
  }
}

where MyGame is

class MyGame extends FlameGame {
  ...
}
Levissie
  • 181
  • 1
  • 3
  • 12