bevyengine / bevy

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

`Err` value: NoEntities even though entity id exists #8262

Closed stanley355 closed 1 year ago

stanley355 commented 1 year ago

Bevy version

Bevy 0.10

[Optional] Relevant system information

If you cannot get Bevy to build or run on your machine, please include:

If your bug is rendering-related, copy the adapter info that appears when you run Bevy.

thread 'Compute Task Pool (0)' panicked at 'called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<bevy_ecs::entity::Entity, bevy_ecs::query::filter::With<bevymon::chat::components::ChatBoxText>>")', src/chat/controller.rs:43:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'Compute Task Pool (0)' panicked at 'A system has panicked so the executor cannot continue.: RecvError', /home/stanley/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.10.0/src/schedule/executor/multi_threaded.rs:194:60
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/stanley/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_tasks-0.10.0/src/task_pool.rs:376:49

What you did

I'm trying to despawn two entities, I made sure that if I debug it, the entity already exists e.g. showing the ID on log and it renders on Egui. The below code works:

use bevy::prelude::*;

use super::components::{SplashBackground, SplashText};
use crate::startup::state::StartupState;

#[derive(Resource, Debug, Deref, DerefMut)]
pub struct SplashTimer(Timer);

#[derive(Component, Debug)]
pub struct SplashScreen;

impl SplashScreen {
    pub fn new(mut commands: Commands, asset_server: Res<AssetServer>, query: Query<&Window>) {
        let window = query.single();

        let splash_bg = SplashBackground::new(&asset_server, window);
        let splash_text = SplashText::new(&asset_server, window);

        commands
            .spawn((splash_bg, SplashBackground))
            .with_children(|par| {
                par.spawn((splash_text, SplashText));
            });
        commands.insert_resource(SplashTimer(Timer::from_seconds(2., TimerMode::Once)));
    }

    pub fn despawn(mut commands: Commands, splash_query: Query<Entity, With<SplashBackground>>) {
        let splash_screen = splash_query.single();
        commands.entity(splash_screen).despawn_descendants();
        commands.entity(splash_screen).despawn();
    }
}

What went wrong

However when I tried with the below code it doesn't working:

use bevy::prelude::*;

use super::components::*;
use super::resource::ChatResource;

#[derive(Debug)]
pub struct Chat;

impl Chat {
    pub fn new(
        chat_res: Res<ChatResource>,
        mut commands: Commands,
        asset_server: Res<AssetServer>,
        texture_atlas_res: ResMut<Assets<TextureAtlas>>,
        window_query: Query<&Window>,
    ) {
        let window = window_query.single();

        let chatbox_name = Name::new("Chatbox");
        let chatbox = ChatBoxSprite::new(&asset_server, texture_atlas_res, window);

        let chattext_name = Name::new("ChatText");
        let chattext = ChatBoxText::new(
            &asset_server,
            window,
            &chat_res.dialgoues[chat_res.dialogue_index],
        );

        commands
            .spawn((chatbox_name, chatbox))
            .insert(ChatBoxSprite);
        commands
            .spawn((chattext_name, chattext))
            .insert(ChatBoxText);
    }

    pub fn next_dialogue(
        keyboard: Res<Input<KeyCode>>,
        mut commands: Commands,
        query: Query<Entity, With<ChatBoxText>>,
    ) {
        if keyboard.pressed(KeyCode::Z) {
            let entity = query.single();
            commands.entity(entity).despawn();
        }
    }
}

It throws the following error:

thread 'Compute Task Pool (0)' panicked at 'called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<bevy_ecs::entity::Entity, bevy_ecs::query::filter::With<bevymon::chat::components::ChatBoxText>>")', src/chat/controller.rs:43:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'Compute Task Pool (0)' panicked at 'A system has panicked so the executor cannot continue.: RecvError', /home/stanley/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.10.0/src/schedule/executor/multi_threaded.rs:194:60
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/stanley/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_tasks-0.10.0/src/task_pool.rs:376:49

As the error message says, it got err value with no entities but when I debug it on log the entity is showing its ID, I'm not sure which part is my fault on despawning it, does anybody have similar issue?

stanley355 commented 1 year ago

Nevermind, I found the issue was on the keyboard.pressed() it should be keyboard.released()