#Despawning `Relationship` entity removes `RelationshipTarget` component from related entity

5 messages · Page 1 of 1 (latest)

austere mirage
#

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()
lost flame
#

just quickly browsed through but it sounds like an issue i ran into. If you despawn all related entities in the Slot, it removes the Slot component, and any queries that include Slot will no longer return

#

for funsies maybe try inserting a default blank Slot after you clear all the related entities and see if that fixes the issue

#

idk if there is a way to force it to not be removed, i think it would be useful though.