#Check if an entity has a component

1 messages · Page 1 of 1 (latest)

viscid ore
#

I want to destroy entities when they collide, and this is the code i use to do that.

fn do_collision(
    query: Query<(Entity, &Transform, &Collider)>,
    mut commands: Commands,
) {
    for [(entity, transform, collider), (other_entity, other_transform, other_collider)] in
        query.iter_combinations()
    {
        let collision = collide(
            transform.translation,
            collider.0,
            other_transform.translation,
            other_collider.0,
        );
        if let Some(_) = collision {
            commands.entity(entity).despawn();
            commands.entity(other_entity).despawn();
        }
    }
}

This works! But I have a slight problem, I don't want to delete the player. Instead I want to restart the game/change the state of the game. So how do I check if the entity i'm deleting has a certain component?

outer ocean
#

You can query for Option<Player> to get the component if it exists, or Has<Player> to get a simple bool

#

alternatively, write one system using the Without<Player> filter, and another using With<Player> to handle the player entirely separately

#

the with/without option is nicer if the logic is very different

#

in your case it would make more sense to exclude Player from your collision system by using a Without<Player> condition, and handle player conditions separately, as the logic is quite different (despawn vs change game state)