komadori / bevy_mod_outline

Apache License 2.0
123 stars 10 forks source link

Despawning outlines in OnEnter/OnExit schedulers crashes #41

Closed barsoosayque closed 3 months ago

barsoosayque commented 3 months ago

If there is a OnEnter(State) or OnExit(State) system, inside of which I despawn entities with OutlineBundle, it would always crash with the following message:

thread 'main' panicked at src/pipeline.rs:337:49:
called `Result::unwrap()` on an `Err` value: NoSuchEntity(Entity { index: 2, generation: 1 })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_render::batching::no_gpu_preprocessing::batch_and_prepare_sorted_render_phase<bevy_mod_outline::node::StencilOutline, bevy_mod_outline::pipeline::OutlinePipeline>`!

I tried both OnEnter and OnExit, but I'm not sure if other non-update schedulers does that ? And for the record, doing the same steps without ever inserting outlines works well.

Repro This seems to be enough to reproduce the issue, a diff for [`examples/pieces.rs`](https://github.com/komadori/bevy_mod_outline/blob/master/examples/pieces.rs): ```diff --- a/examples/pieces.rs +++ b/examples/pieces.rs @@ -10,11 +10,20 @@ fn main() { .insert_resource(Msaa::Sample4) .insert_resource(ClearColor(Color::BLACK)) .add_plugins((DefaultPlugins, OutlinePlugin)) - .add_systems(Startup, setup) - .add_systems(Update, rotates) + .add_systems(OnEnter(AppState::Scene), setup) + .add_systems(OnExit(AppState::Scene), teardown) + .add_systems(Update, (rotates, change_state)) + .init_state::() .run(); } +#[derive(States, Default, Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum AppState { + #[default] + Empty, + Scene, +} + #[derive(Component)] struct Rotates; @@ -101,8 +110,30 @@ fn setup( }); } +fn teardown(mut commands: Commands, query: Query, Without)>) { + for entity in &query { + commands.entity(entity).despawn_recursive(); + } +} + fn rotates(mut query: Query<&mut Transform, With>, timer: Res
komadori commented 3 months ago

Fixed by a51680792815bf2945f7ed608970147e87813b5b.

komadori commented 3 months ago

Fix released in 0.8.1.