#Camera Query Error

8 messages · Page 1 of 1 (latest)

ebon finch
#

I am trying to move a sprite and the camera in a 2D top down game. I attempted to make two separate querys for the transform of the sprite and the camera transform.

fn move_character(    
    keyboard_input: Res<Input<KeyCode>>,
    mut query: Query<(&mut Transform, &mut TextureAtlasSprite), With<Character>>,
    mut camera_query: Query<&mut Transform, With<Camera>>,
    time_step: Res<FixedTime>,){
        let (mut transform, mut sprite) = query.single_mut();
        let speed = 60.0;
            if keyboard_input.pressed(KeyCode::Left) {
                transform.translation.x += -speed*time_step.period.as_secs_f32();
                sprite.index = 1;
            }
            if keyboard_input.pressed(KeyCode::Right) {
                transform.translation.x += speed*time_step.period.as_secs_f32();
                sprite.index = 6;
            }
            if keyboard_input.pressed(KeyCode::Up) {
                transform.translation.y += speed*time_step.period.as_secs_f32();
                sprite.index = 4;
            }
            if keyboard_input.pressed(KeyCode::Down) {
                transform.translation.y += -speed*time_step.period.as_secs_f32();
                sprite.index = 5;
            }
}

Just by attempting to make the second query, I get a weird error with a message I don't understand. Should I be doing this in one query somehow?

dusty olive
#

Basically, an entity could technically be inside the query query and the camera_query query, they're not exclusive, and since they both access mutably the Transform component on this hypothetical entity, that would break Rust's borrow checker/mutability rules (even though, in this instance, the check is done by Bevy itself not the compile-time borrow checker).

#

You need to separate explicitly the two queries, for example with mut query: Query<(&mut Transform, &mut TextureAtlasSprite), (With<Character>, Without<Camera>)> (note the Without<Camera>)

queen niche
#

Bevy cannot know that there is no entity that has Transform, Camera, TextureAtlasSprite and Character, as little sense as that would make to a human

ebon finch
#

I changed it to:

    mut query: Query<(&mut Transform, &mut TextureAtlasSprite), (With<Character>, Without<Camera>)>,
    mut camera_query: Query<&mut Transform, (Without<Character>, With<Camera>)>,

and it works now!

dusty olive
#

Note that you technically don't need both Without, as With<Camera>/Without<Camera> are already exclusive (same for With<Character>/Without<Character>), but it doesn't really matter anyway

queen niche
#

I would personally go for as little withouting as possible cause it's not like it helps the individual queries make sense