Amethyst's Loader can't find an asset file

676 Views Asked by At

I am using the Rust Amethyst game engine to load a texture named ground.png, but the Loader does not seem to find the file:

//...
let assets_dir = format!("{}", env!("CARGO_MANIFEST_DIR"));
let mut game = Application::build(assets_dir, Example)?.build(game_data)?;

My assets_dir is the root folder of the project and when loading my file, I append textures/ground.png:

let texture_handle = {
    let loader = world.read_resource::<Loader>();
    let texture_storage = world.read_resource::<AssetStorage<Texture>>();
    loader.load(
        "textures/ground.png",
        PngFormat,
        Default::default(),
        (),
        &texture_storage,
    )
};

My file directory looks like this:

├── src
│   └── main.rs
├── Cargo.toml
└── textures
    └── ground.png

The error I am getting is a None value when fetching the texture:

assert!(
    world
        .read_resource::<AssetStorage<Texture>>()
        .get(&texture_handle) != None
); //panics

I am using amethyst 0.8.

1

There are 1 best solutions below

1
On

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.