Panic despawning non-existent entity
I have setup using `For::each`, `Cond`, and a `Mutable` which can cause quill to get into trouble trying to despawn an entity.
```
2024-08-17T12:18:19.750435Z WARN bevy_ecs::world: error[B0003]: Could not despawn entity Entity { index: 20, generation: 1 } because it doesn't exist in this World. See: https://bevyengine.org/learn/errors/#b0003
```
I've tried to create a reduced example but could probably be simplified more. Removing either the Cond or the update of the mutable stops the panic from happening. I was able to bisect the issue to be starting from 479fc2aedfd5646abf88aa1f1dec7ee018829813
- Run the example
- click the button
- panic
```
//! Example of a simple UI layout
#![feature(impl_trait_in_assoc_type)]
#[path = "./common/lib.rs"]
mod common;
use bevy::prelude::*;
use bevy_mod_picking::prelude::*;
use bevy_quill::{Cond, Cx, Element, For, Mutable, QuillPlugin, View, ViewTemplate};
use common::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
QuillPlugin,
DefaultPickingPlugins,
))
.init_resource::<Birds>()
.observe(Birds::on_add)
.observe(Birds::on_remove)
.add_systems(Startup, (setup, setup_view_root, setup_birds))
.add_systems(Update, (close_on_esc, rotate))
.run();
}
fn setup_view_root(mut commands: Commands) {
commands.spawn(Reproduce.to_root());
}
fn setup_birds(mut commands: Commands) {
commands.spawn(Bird);
}
#[derive(Component)]
struct Bird;
#[derive(Resource, Default)]
struct Birds(Vec<Entity>);
impl Birds {
pub fn on_add(trigger: Trigger<OnAdd, Bird>, mut birds: ResMut<Self>) {
birds.0.push(trigger.entity());
}
pub fn on_remove(trigger: Trigger<OnRemove, Bird>, mut birds: ResMut<Self>) {
birds.0.retain(|&e| e != trigger.entity());
}
}
#[derive(Clone, PartialEq)]
struct Reproduce;
impl ViewTemplate for Reproduce {
type View = impl View;
fn create(&self, cx: &mut Cx) -> Self::View {
let selected = cx.create_mutable::<Option<Entity>>(None);
let birds = cx.use_resource::<Birds>().0.clone();
selected.update(cx, |mut selected: Mut<Option<Entity>>| {
let new_selected = selected
.filter(|s| birds.contains(s))
.or(birds.first().copied());
if new_selected != *selected {
info!("Updating selected {:?}", new_selected);
*selected = new_selected;
}
});
Element::<NodeBundle>::new().children((
For::each(birds, move |&item| SubComponent { item, selected }),
Element::<ButtonBundle>::new()
.insert_dyn(
move |_| {
On::<Pointer<Click>>::run(
move |mut commands: Commands, birds: Query<Entity, With<Bird>>| {
info!("Clicked");
for bird in &birds {
commands.entity(bird).despawn();
}
},
)
},
(),
)
.children("Click me"),
))
}
}
#[derive(Clone, PartialEq)]
struct SubComponent {
pub item: Entity,
pub selected: Mutable<Option<Entity>>,
}
impl ViewTemplate for SubComponent {
type View = impl View;
fn create(&self, cx: &mut Cx) -> Self::View {
let selected = self.selected.get(cx);
Cond::new(
selected.is_some(),
Element::<NodeBundle>::new().children("Bird"),
(),
)
}
}
```
关闭于 2024-08-28 4 条评论