Loading assets from a file

(Martin) #1

I posted the following on Reddit, and I was advised to post here…

I am trying to understand how to load data from files into Amethyst. Specifically, I need to load a PNG file to create a texture.

I am getting a message:

[DEBUG][amethyst_assets::loader] "renderer::Texture": Loading asset "texture/texture.png" with format "PNG" from source "[default source]" (handle id: Handle { id: 7 })
thread 'main' panicked at 'Tried to fetch a resource, but the resource does not exist.

I am basing my code on the Pong tutorial and other examples in Amethyst on GitHub.

Please tell me

  • what I should use for the asset directory in the call to Application::build and
  • where I should put my asset file assuming the file is called texture/texture.png in the call to loader.load. The error message just tells me “[default source]” — from where? If I had the file path that it is trying to read, I could move the file there.

I am in debug mode at the moment, so the executable will be $WKSP/target/debug/$PROG (i.e. the program is in a workspace — does this affect things?)

I have looked at the book, the API reference and the examples on GitHub, and I cannot work out what to do. I have tried putting the PNG in every location that I can think of, but the app keeps telling me that it cannot find the file (and it does not tell me where it is looking). It seems that I need to set the location of the assets directory, but even setting that explicitly does not help.

My app is part of a workspace, but I have tried creating an assets directory at (a) the workspace level and (b) the app level, and neither of those works.

I have added my specific details to an existing Stack question at https://stackoverflow.com/questions/52019501/amethysts-loader-cant-find-an-asset-file

Also, a related question: is there some way that I can supply the data as a byte array instead of a file? Then I could incorporate the data into my app’s executable using a crate like rust_embed.

The original Reddit post is at https://www.reddit.com/r/rust_gamedev/comments/bjr5s8/how_do_i_load_a_png_into_my_amethyst_game/ [this is a cross-post].

1 Like

(Azriel Hoh) #2

**Read the next reply before this one. This is possibly relevant, but the next post is more likely the issue.**

Hopefully this table helps you, because there are many possible answers.

All rows assume you are loading a texture using:

loader.load("path/to/texture.png", ..)

Paths listed are relative to the repository directory.

Amethyst version What the code uses for assets dir How you run the executable Where the texture should be
0.10.0 "assets" cargo run $repo/target/$profile/assets/path/to/texture.png
0.10.0 format!("{}/assets", env!("CARGO_MANIFEST_DIR")) cargo run $repo/assets/path/to/texture.png
0.10.0 "assets" ./target/$profile/app $repo/assets/path/to/texture.png
0.10.0 env!("CARGO_MANIFEST_DIR") ./target/$profile/app $repo/assets/path/to/texture.png
0.10.0 option_env!("CARGO_MANIFEST_DIR").map(|d| format!("{}/assets", d)).unwrap_or("assets") cargo run $repo/assets/path/to/texture.png
0.10.0 option_env!("CARGO_MANIFEST_DIR").map(|d| format!("{}/assets", d)).unwrap_or("assets") ./target/$profile/app $repo/target/$profile/assets/path/to/texture.png
master application_root_dir() cargo run $repo/assets/path/to/texture.png
master application_root_dir() ./target/$profile/app $repo/target/$profile/assets/path/to/texture.png

The first 4 are not good solutions (either dev path or player path is wrong). The 5th and 6th ways to tolerate it, which is done for you on master using the application_root_dir() function.

That’s the (unfortunately mysterious) alias for the assets directory path used in Application::new(assets_dir, ..)

2 Likes

(Azriel Hoh) #3

ah, just realized this is actually the real error. The message before it is a [DEBUG] message :man_facepalming:.

I suspect you:

  • have a DrawFlat pass, and
  • are using a SpriteRender component on an entity

SpriteRenders are drawn using the DrawFlat2D pass in main.rs.

More info: Each pass is like a System, and each System has automatic resource registration. The DrawFlat pass does not register the SpriteRender storage, whereas the DrawFlat2D does.

To diagnose this error message:

thread 'main' panicked at 'Tried to fetch a resource, but the resource does not exist.

you’d have to:

  • use a nightly compiler (rustup default nightly), and

  • enable the amethyst "nightly" feature in Cargo.toml

    amethyst = { git = "https://github.com/amethyst/amethyst.git", features = ["nightly"] }
    
2 Likes

(Martin) #4

I think I have it working now using Rust and Amethyst nightly mode. Thanks for your help.

0 Likes