Skip to content

Observers panic when an earlier observer despawned the entity #19623

Open
@janhohenheim

Description

@janhohenheim

Bevy version

0.16.1

What you did

use bevy::prelude::*;

fn main() -> AppExit {
    App::new()
        .add_plugins(MinimalPlugins)
        .add_systems(Startup, spawn)
        .add_systems(Update, despawn_transforms)
        .add_observer(despawn)
        .add_observer(dispatch_print)
        .add_observer(print)
        .run()
}

fn spawn(mut commands: Commands) {
    commands.spawn(Transform::default());
}

fn despawn_transforms(query: Query<Entity, With<Transform>>, mut commands: Commands) {
    for entity in query.iter() {
        commands.entity(entity).trigger(Kill);
    }
}

#[derive(Event)]
struct Kill;

#[derive(Event)]
struct Print;

fn despawn(trigger: Trigger<Kill>, mut commands: Commands) {
    commands.entity(trigger.target()).despawn();
}

fn dispatch_print(trigger: Trigger<Kill>, mut commands: Commands) {
    commands.entity(trigger.target()).trigger(Print);
}

fn print(_trigger: Trigger<Print>) {
    println!("Killed something");
}

What went wrong

thread 'main' panicked at /home/hhh/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bevy_ecs-0.16.1/src/error/handler.rs:141:1:
Encountered an error in command `<bevy_ecs::system::commands::entity_command::trigger<test::Print>::{{closure}} as bevy_ecs::error::command_handling::CommandWithEntity<core::result::Result<(), bevy_ecs::world::error::EntityMutableFetchError>>>::with_entity::{{closure}}`: The entity with ID 3v1 does not exist (enable `track_location` feature for more details)

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic when applying buffers for system `test::despawn_transforms`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

Additional information

This came up organically while working on our jam submission, where the trigger is OnDamage. If the damage is high enough, the enemy is despawned. That means that other later running observers, like the ones playing a damage sound effect, panic.
I would have expected the later observers to simply not run.

Metadata

Metadata

Labels

A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

Type

No type

Projects

Status

Observer overhaul

Relationships

None yet

Development

No branches or pull requests

Issue actions