3

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.

KuSpa
  • 325
  • 4
  • 14
  • I presume you've solved this by now, but I would guess it has something to do with how you're running it. You need to make sure the textures folder is in the working directory of the executable. I'd also check that your file is actually named correctly. – AdmiralJonB Nov 11 '18 at 17:56
  • I am getting the same problem with 0.10.0. Please tell me (a) what I should use for the asset directory in the call to `Application::build` and (b) where I should put my asset file assuming the file is called `texture/texture.png` in the call to `loader.load`. – Martin Ellison May 02 '19 at 03:16
  • The executable will be $WKSP/target/debug/$PROG. – Martin Ellison May 02 '19 at 03:23

1 Answers1

1

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.

Azriel
  • 386
  • 2
  • 13