#how to load an asset relative to the executable

16 messages · Page 1 of 1 (latest)

west bluff
#

I'm currently using AssetServer to load my asset like this:

let some_texture = asset_server.load("image.png");

this is my project structure:

.
├── build.rs
...
└── src
    ├── assets
    │   └── image.png
...
    └── main.rs

build.rs is used to copy src/assets to the target/release/assets or target/debug/assets folder and does so correctly and this does work well - if I run the binary directly. but if I run the program through cargo run, cargo tries to grab the asset from the root(./assets/image.png).
How do I tell it to grab it from the correct place?

#

For reference, this is the code i'm using to copy the assets folder(build.rs):

fn main() {
    let current_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    let assets_folder = format!("{}/src/assets", current_dir);
    let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
    let target_folder = format!("{}/target/{}/assets", current_dir, profile);
    
    // some logic that copies the assets_folder to target_folder
}
dry mica
#

Do you need to place assets under src? If you can edit the build script it should work by default

#

(using DefaultPlugins.set(AssetPlugin {/* … */}))

west bluff
#

Should I just move it to the root directory, and copy as usual?

dry mica
#

Yeah I think that would be ideal (or at least what Bevy expects by default)

west bluff
#

I saw in the docs you can set some environment variable like BEVY_ROOT_DIR but had no success

west bluff
dry mica
#

Yeah I don’t really have much experience outside of using the defaults haha so hopefully this works

west bluff
dry mica
#

Awesome! And no problem haha glad it helped

warm nacelle
#

but it still expects assets/ I think, might be an overlooked detail 🤔

deft cedar