bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.34k stars 3.49k forks source link

Add `AssetPath::exists` #14427

Open alice-i-cecile opened 2 months ago

alice-i-cecile commented 2 months ago

What problem does this solve or what need does it fill?

Conditionally loading assets if a matching pattern exists is useful for data-driven behavior: you may only want to load a sound effect if a file with a matching name exists.

Checking whether the file is present Path::exists is the obvious solution, but is actually a footgun. Path is an absolute path, relative to filesystem, while AssetPath is returned relative to the assets folder that Bevy is configured to use.

What solution would you like?

Add AssetPath::exists, which pre-parses the path.

What alternative(s) have you considered?

We could also spin out something that returns a check further down the asset pipeline: whether an asset exists and whether a correct loader is found for example.

That would need to be a method on AssetServer.

viridia commented 2 months ago

See #12210

ShreyK commented 2 days ago

Hello,

@alice-i-cecile I wish to take this issue on as my first PR.

I think a simple AssetPath::exists function would be very good to have to ensure the game doesnt crash if the assets cannot be found. See below example:

I found that when I was trying to deploy the game as a binary, I did not realize that I had to copy the assets/ folder over to my target dir.

Aside from the AssetPath::exists, If we go down the route of https://github.com/bevyengine/bevy/issues/12210 ...

I think it would be beneficial to have an internal check in the asset_loader with a default fallback to not error the game on launch, in situations like these, it would be more clear to have an ERROR message being displayed for each asset not found.

I found some small, base64 encoded strings online, I think I should be able to find similar versions for all the default asset loader file types bevy supports.

Gif

data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=

Decoded here: https://base64.guru/converter/decode/image/gif image

Png:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNgYAAAAAMAASsJTYQAAAAASUVORK5CYII=

Decoded here: https://base64.guru/converter/decode/image/png image

Let me know what you think

alice-i-cecile commented 2 days ago

Hey, thanks! I think more fallback assets are reasonable, but we should open a new issue to discuss that :)

For now, feel free to add the simple AssetPath::exists and we can close this one out.