#RaylibHandle borrow checker woes

7 messages · Page 1 of 1 (latest)

flat beacon
#

Hey! I'm fairly new to the language so I decided to work on a simple game project. I understand some of the basics of the borrow checker but I can't seem to figure out what I'm doing wrong with my current method, whether I made a simple mistake or if I just can't figure out how to design around the language rules.

use raylib::prelude::*;

mod engine;

fn main() {
    let (mut rl, thread) = raylib::init()
        .size(640, 480)
        .title("Game Window")
        .resizable()
        .build();
    rl.toggle_fullscreen();

    let level1_texture = rl.load_texture(
        &thread,
        format!("{}/level1.png", engine::ASSETS_DIRECTORY).as_str(),
    );
    assert!(level1_texture.is_ok());
    let level1 = engine::level::load_level(level1_texture.unwrap().load_image().unwrap());

    let mut player = engine::player::init_player();

    while !rl.window_should_close() {
        let mut d = rl.begin_drawing(&thread);

        // Processing

        player.update(&mut rl); // cannot borrow `rl` as mutable more than once at a time

        // Rendering

        for o in level1.objects.iter() {
            o.draw_hitbox(&mut d);
        }
        player.object.draw_hitbox(&mut d);

        d.clear_background(Color::BLACK);
    }
}

rotund pollen
#

I don't know raylib, but the “obvious” solution is that you should call begin_drawing when you start drawing, not before update()

flat beacon
#

I don't think so, in raylib examples for all bindings this is how it's done. My issue is with re-using rl multiple times since it has to be mutable.

#

I'm not entirely sure how to design around that rule just yet and I can't figure out the best way to solve this.

#

Oh nevermind I severely misunderstood your response, I moved that line down to the rendering code and it worked 😅

rotund pollen
#

Yes. And another thing is that if you want to do something with rl after you're done with d, then you'll need to either put the drawing code in a separate function, put it in a block, or drop(d) when you're done

#

to release the borrow of rl.