Begginner - need to change value of bool inside function. (macroquad game engine)

I'm using the macroquad game engine to make a game, and I'm running into a problem where I'm trying to change the value of a bool inside a function and it's not working and my players movement system which revolves around whether gameover (bool) is false or true.

use macroquad::prelude::*;

#[macroquad::main("InputKeys")]
async fn main() {
    let mut x: f32 = screen_width() / 2.0_f32;
    let mut y: f32 = screen_width() / 2.0_f32;
    let mut gravity_speed: f32 = 1.0_f32;
    let speed: f32 = 3.5_f32;
    let gameover: bool = false;

    loop {
        clear_background(GRAY);

        if is_key_down(KeyCode::W) {
            if gameover == true {
                y -= speed;
            }
        }
        if is_key_down(KeyCode::A) {
            if gameover == true {
                x -= speed;
            }
        }
        if is_key_down(KeyCode::D) {
            if gameover == true {
                x += speed;
            }
        }
        gravity(&mut y, &mut gravity_speed/*, &mut gameover */);
        draw_circle(x, y, 5.0, YELLOW);
        next_frame().await;
    }

}

fn gravity(n1: &mut f32, n2: &mut f32/*,thingy: &mut bool*/) {
    *n1 += *n2;
    //println!("{n1}");
    if n1 >= &mut 800.0_f32 {
        gameover = true;
    }
    if n1 <= &mut -1.5_f32 {
        gameover = true;
    }
}

the terminal just returns this with no errors:

warning: unused variable: `gameover`
  --> src\main.rs:42:13
   |
42 |         let gameover = true;
   |             ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_gameover`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `gameover`
  --> src\main.rs:45:13
   |
45 |         let gameover = true;
   |             ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_gameover`

the code you showed actually doesn't compile because gameover isn't defined in the gravity fn. maybe you shared the wrong snippet

but from it, I see two problems:

1- you want to declare gameover as mut, so that you can later borrow it mutably with &mut
2- when you're going to mutate a reference like that, you need to explicitly dereference the l-value, so it would be *gameover = false/true

I did try including gameover as a parameter but it didn't work, I guess I'll try the dereferencing.

(6 minutes later) It worked, thanks for your response.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.