Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssetServer returns invalid handle after entity despawned (in the same frame) #12094

Closed
eidloi opened this issue Feb 24, 2024 · 2 comments
Closed
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior

Comments

@eidloi
Copy link

eidloi commented Feb 24, 2024

Bevy version

0.13

`AdapterInfo { name: "NVIDIA GeForce RTX 2070", vendor: 4318, device: 7938, device_type: DiscreteGpu, backend: Vulkan }`

What you did

I use OnExit and OnEnter to switch between UI pages. During OnExit I use commands to despawn entities recursively from the root node and then OnEnter to generate new content. My issue is with entities that have UiImage components despawned via commands and new entities with their UiImage handle set via a custom command that refer to the same image. I assume all asset types would have the same issue though.

What went wrong

I am using images for UI icons and if an icon that was used in the despawned page is fetched from with AssetServer.load(path) I can't see the icon rendered. This happens even if I add an apply_deferred before spawning the new page content.

Additional information

There doesn't seem to be an issue with loading the image if I depsawn the entity ahead of the switch of the pages. I assume it works because the frame ends with the asset cleaned up and then the loads it again correctly in a later frame.

Here is the command I use to set images:

struct SetImage {
    path: String,
}

impl EntityCommand for SetImage {
    fn apply(self, entity: Entity, world: &mut World) {
        let handle = if self.path == "" {
            Handle::default()
        } else {
            world.resource::<AssetServer>().load(self.path)
        };

        let mut q_ui_image = world.query::<&mut UiImage>();
        let Ok(mut image) = q_ui_image.get_mut(world, entity) else {
            warn!(
                "Failed to set image on entity {:?}: No UiImage component found!",
                entity
            );
            return;
        };

        if image.texture != handle {
            image.texture = handle;
        }
    }
}
@eidloi eidloi added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Feb 24, 2024
@mgmarlow
Copy link

mgmarlow commented Feb 25, 2024

I think I just ran into this same issue, for the same reasons listed. I likewise use OnExit(SomeState) to despawn entities that are re-spawned in the next state, OnEnter(OtherState). In my game, I observe that the textures fail to load (using TextureAtlasLayout in my case) but all of the normal entity logic behaves the same.

I've tried tinkering with apply_deferred before or after the state transitions (as well as splitting up my teardown despawner into a separate system, and using chain to pair them w/ auto apply_deferred in 0.13), but no luck.

As with the OP, the entities spawned/despawned refer to the same image path from the asset server.

@TrialDragon TrialDragon added A-Assets Load files from disk to use for things like images, models, and sounds and removed S-Needs-Triage This issue needs to be labelled labels Feb 28, 2024
@eidloi
Copy link
Author

eidloi commented Mar 17, 2024

I think this should have been fixed by #12459 as well. Seems to be the same issue.

@eidloi eidloi closed this as completed Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants