Whenever I despawn an entity with a Relationship component, it removes the RelationshipTarget component from the related entity. Can someone tell me what's going on and what I'm doing wrong?
Here are my component definitions:
#[derive(Component, Reflect, Debug)]
#[relationship(relationship_target = Slot)]
pub struct Slotted(pub Entity);
#[derive(Component, Reflect, Debug, Default)]
#[relationship_target(relationship = Slotted)]
#[require(Coordinate)]
pub struct Slot {
#[relationship]
occupants: Vec<Entity>,
}
and the system that despawns looks like this:
fn on_clear(_: On<Clear>, mut commands: Commands, query: Query<Entity, With<Slotted>>) {
for entity in query.iter() {
commands.entity(entity).despawn();
}
}
After that system runs, this system's query no longer returns the entity that the despawned Slotted used to point to
fn on_spawn(
_: On<Spawn>,
mut commands: Commands,
slots: Query<(Entity, &Slot)>,
) {
for (entity, slot) in slots.iter() {
info!(?entity, ?slot, "latest slot");
}
// --snip--
}
For context, both of those observers are triggered from another observer, triggered by a system behind a run condition:
fn on_restart(_: On<Restart>, mut commands: Commands) {
commands.trigger(Clear);
commands.trigger(Spawn);
}
fn restart(mut commands: Commands) {
commands.trigger(Restart);
}
#[derive(Event)]
struct Clear;
#[derive(Event)]
struct Spawn;
#[derive(Event)]
struct Restart;
App::new()
// --snip--
.add_systems(
Update,
restart.run_if(input_just_pressed(KeyCode::R))
)
.add_observer(on_clear)
.add_observer(on_spawn)
.add_observer(on_restart)
.run()