Bevy: How to correctly modify a component?

Viewed 78

Hello today is my first day with bevy so I am trying to make a simple top down Bullet hell game. My problem is that I am trying to create a system that takes care of the player's dashing like this:

fn player_dashing_system(
    kb: Res<Input<KeyCode>>,
    query: Query<&mut Player>
) {
    if let Ok(&mut player) = query.get_single() {
        if kb.just_pressed(KeyCode::Space) {
            player.speed = BASE_PLAYER_DASH_SPEED;
        }
    }
}

but I get this error:

error[E0308]: mismatched types
--> src\player.rs:108:15
    |
108 |     if let Ok(&mut Player) = query.get_single() {
    |               ^^^^^^^^^^^    ------------------ this expression has type 
`Result<&Player, QuerySingleError>`
    |               |
    |               types differ in mutability
    |               help: you can probably remove the explicit borrow: `Player`
    |
    = note:      expected reference `&Player`
            found mutable reference `&mut _`

I have tried fiddling around with the mutability of the variables but couldn't get anything to work.

1 Answers

Use get_single_mut.

Also remove the &mut from Ok(&mut player), otherwise the pattern will dereference the player, which is likely not what you want.

Related