2

I am trying to execute the below two queries in a bevy system function.

fn move_player(
    mut player_query: Query<(&mut Velocity, &mut Transform, &SpriteSize, &Player), With<PlayerId>>,
    wall_query: Query<(&Transform, &SpriteSize), With<Barrier>>,
) {
    for (mut player_velocity, mut player_tf, player_size, player) in player_query.iter_mut() {
        for (wall_tf, wall_size) in wall_query.iter() {
        }
    }
}

I inserted the PlayerId component to the Player entity and the Barrier component to the Wall entities when spawning them. The PlayerId is not inserted in Wall entities and the Barrier component is not inserted in the Player entity.

When I run the above function I get the error below;

thread 'main' panicked at 'error[B0001]: Query<(&mut bevy_transform::components::transform::Transform, &bevy_fantasy::Sprite Size), bevy_ecs::query::filter::With<bevy_fantasy::Barrier>> in system bevy_fantasy::player::move_player accesses component( s) bevy_transform::components::transform::Transform in a way that conflicts with a previous system parameter. Consider using Without<T> to create disjoint Queries or merging conflicting Queries into a ParamSet.

Why are the 2 queries conflicting when I filter them using unique components?

simplelenz
  • 877
  • 1
  • 9
  • 22

2 Answers2

2

You're using

mut player_query: Query<(&mut Velocity, &mut Transform, &SpriteSize, &Player), With<PlayerId>>,
wall_query: Query<(&Transform, &SpriteSize), With<Barrier>>,

All entities that have Transform, Velocity, SpriteSize, Player, PlayerId, and Barrier components are in both queries. There is no way for Rust or Bevy to tell that there aren't any such Entities.

Getting mutable references to Transform would therefore be undefined behaviour

To fix it just follow one of the suggestions.

cafce25
  • 15,907
  • 4
  • 25
  • 31
1

I have had this problem myself. As @cafce25 you need to follow the suggestion of the error message. If you add a Without to one of the queries it can exclude the results of the other query.

Here I've modified your snippet to use Without<Barrier> in the player_query.

fn move_player(
    mut player_query: Query<(&mut Velocity, &mut Transform, &SpriteSize, &Player), With<PlayerId>, Without<Barrier>,
    wall_query: Query<(&Transform, &SpriteSize), With<Barrier>>,
) {
    for (mut player_velocity, mut player_tf, player_size, player) in player_query.iter_mut() {
        for (wall_tf, wall_size) in wall_query.iter() {
        }
    }
}
cengen
  • 61
  • 1
  • 3