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

Can not find asset when Flame Game is used as a module or package #3091

Closed
champ96k opened this issue Mar 21, 2024 · 8 comments
Closed

Can not find asset when Flame Game is used as a module or package #3091

champ96k opened this issue Mar 21, 2024 · 8 comments
Labels

Comments

@champ96k
Copy link

What happened?

I'm developing a game using the Flutter Flame engine and have packaged the game to be used in another Flutter app. However, I'm encountering an issue where the game, when used as a package dependency in the host app, cannot find its image assets.

E/flutter ( 3101): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unable to load asset: "assets/images/klondike-sprites.png".
E/flutter ( 3101): The asset does not exist or has empty data.
E/flutter ( 3101): #0      PlatformAssetBundle.load.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:324:9)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #1      Images._fetchToMemory (package:flame/src/cache/images.dart:184:18)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #2      KlondikeGame.onLoad (package:klondike_package/klondike_game.dart:41:5)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #3      FlameGame.load (package:flame/src/game/flame_game.dart:105:5)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #4      GameWidgetState.loaderFuture.<anonymous closure> (package:flame/src/game/game_widget/game_widget.dart:194:9)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #5      _FutureBuilderState._subscribe.<anonymous closure> (package:flutter/src/widgets/async.dart:624:31)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): 

My Flame game has its assets located in assets/images/ and they are properly listed in the pubspec.yaml of the game package. It loads the image like this:

However, when integrating this game package into another Flutter app as a module and trying to run the app, the game fails to load its image assets. It seems like the game package is unable to locate the assets directory.

The game fails to load its image assets when used as a package dependency in another Flutter app.

What do you expect?

The game, when integrated as a package dependency, should be able to locate and load its image assets successfully.

How can we reproduce this?

  1. Create a Flutter Flame game with image assets located in assets/images/.
  2. Package the game to be used as a dependency in another Flutter app.
  3. Integrate the game package into the host app.
  4. Run the host app and observe the failure to load image assets within the game.

What steps should take to fix this?

No response

Do have an example of where the bug occurs?

NA

Relevant log output

I/flutter ( 3101): Image fileName : klondike-sprites.png, Key: null
I/flutter ( 3101): Image _assets name : {}, Key: null
I/flutter ( 3101): Image _fetchToMemory : klondike-sprites.png
E/flutter ( 3101): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Unable to load asset: "assets/images/klondike-sprites.png".
E/flutter ( 3101): The asset does not exist or has empty data.
E/flutter ( 3101): #0      PlatformAssetBundle.load.<anonymous closure> (package:flutter/src/services/asset_bundle.dart:324:9)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #1      Images._fetchToMemory (package:flame/src/cache/images.dart:184:18)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #2      KlondikeGame.onLoad (package:klondike_package/klondike_game.dart:41:5)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #3      FlameGame.load (package:flame/src/game/flame_game.dart:105:5)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #4      GameWidgetState.loaderFuture.<anonymous closure> (package:flame/src/game/game_widget/game_widget.dart:194:9)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): #5      _FutureBuilderState._subscribe.<anonymous closure> (package:flutter/src/widgets/async.dart:624:31)
E/flutter ( 3101): <asynchronous suspension>
E/flutter ( 3101): 


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

Output of: flutter doctor -v

flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel [user-branch], 3.19.3, on macOS 14.1 23B2073 darwin-arm64, locale en-IN)
! Flutter version 3.19.3 on channel [user-branch] at /Users/tushar.nikam/tool/flutter
Currently on an unknown channel. Run flutter channel to switch to an official channel.
If that doesn't fix the issue, reinstall Flutter by following instructions at https://flutter.dev/docs/get-started/install.
! Upstream repository unknown source is not a standard remote.
Set environment variable "FLUTTER_GIT_URL" to unknown source to dismiss this error.
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[!] Xcode - develop for iOS and macOS (Xcode 15.2)
! CocoaPods 1.5.2 out of date (1.13.0 is recommended).
CocoaPods is used to retrieve the iOS and macOS platform side's plugin code that responds to your plugin usage on the Dart side.
Without CocoaPods, plugins will not work on iOS or macOS.
For more info, see https://flutter.dev/platform-plugins
To upgrade see https://guides.cocoapods.org/using/getting-started.html#updating-cocoapods for instructions.
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.87.2)
[✓] Connected device (3 available)
[✓] Network resources

! Doctor found issues in 2 categories.


### Affected platforms

Android, iOS

### Other information

_No response_

### Are you interested in working on a PR for this?

- [X] I want to work on this
@champ96k champ96k added the bug label Mar 21, 2024
@champ96k
Copy link
Author

champ96k commented Mar 21, 2024

spydon sethladd angel333 ianliu domesticmouse Do you have any idea about it?

Moderated: removed tags.

@ianliu
Copy link
Contributor

ianliu commented Mar 21, 2024

@champ96k I'm sorry, I don't have anything to do with this project.

@spydon
Copy link
Member

spydon commented Mar 21, 2024

@spydon @sethladd @angel333 @ianliu @domesticmouse Do you have any idea about it?

Please don't tag all those Googlers in our issues, they are busy as it is.

If you want quick answers I suggest that you join our discord instead, it's kind of rude to ping people like that in issues.

@champ96k
Copy link
Author

Hey @spydon Thanks for response as i already drop this issue in discord, and will closely monitor this issue on discord

As i debug this issue with flame repo as i found this issue is coming from below code

Future<Image> _fetchToMemory(String name) async {
    final data = await bundle.load('$_prefix$name');
    final bytes = Uint8List.view(data.buffer);
    return decodeImageFromList(bytes);
  }

This below line throwing an error

final data = await bundle.load('$_prefix$name');

@champ96k
Copy link
Author

champ96k commented Mar 22, 2024

I just found one work around solution, When i calling our module or project then i need to create one name parameter for prefix and we need to override prefix parameter [package prefix param]

Here i am calling/Navigating my module/package code, then that time i need to override prefix param, Just for reference below is the complete code.

Navigator.of(context).push(
          MaterialPageRoute(
            builder: (ctx) => Klondike(
              prefix: 'packages/klondike_package/assets/images/',
            ),
          ),
        );
class Klondike extends GameWidget<KlondikeGame> {
  Klondike({super.key, required this.prefix})
      : super(game: KlondikeGame(prefix: prefix));
  final String prefix;
}
class KlondikeGame extends FlameGame {
  KlondikeGame({required this.prefix}) : super(camera: CameraComponent());

 @override
  FutureOr<void> onLoad() async {
    images.prefix = prefix;
 }
}

@champ96k
Copy link
Author

champ96k commented Mar 22, 2024

As of now closing this issue, feel free to re-open if you find any difficulty.

@champ96k
Copy link
Author

This is not working when we host the package on github

@champ96k champ96k reopened this Mar 25, 2024
@spydon
Copy link
Member

spydon commented Mar 25, 2024

This is not working when we host the package on github

It should not matter where the package originates, as long as the package name is correct.

@spydon spydon closed this as completed May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants