0

In Rust, I'm working on an on_update() function for a renderer. There are certain variables I would like to store outside the loop, then access and change within the function.

Example in pseudocode:

global mut lastMousePos: Vec2 = Vec2::new(0.0, 0.0); // lastMousePos can be written to and read from

pub struct Camera {
    /* ... */
}

impl Camera {
    
    /* ... */

    pub fn on_update(self, timestep: f32, input: &WinitInputHelper) {
        let Some((x, y)) = input.mouse();
        let mousePos = Vec2::new(x, y);
        let delta = (mousePos - lastMousePos) * 0.002;
        lastMousePos = mousePos;

        /* ... */

    }

    /* ... */

}

(/* ... */ = code hidden for brevity)

In this example, I'd like to be able to read lastMousePos, then write a new value to it. I need this functionality in other contexts as well.

I tried reading the Rust docs for solutions, but, as always, I just got more confused.

0 Answers0