CleanCut / rusty_engine

2D game engine for learning Rust

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question regarding compiler error because of borrowing of: engine

etnt opened this issue · comments

Hi,

I'm following your Ultimate Rust 2 course and have completed the first basic Road Race task.
(great fun btw :-)

Now I've started to experiment with shooting. So in my game_logic code I want to set the
rotation of the Shot to be the same as the Player at the time the shot fires, then I think I have
figured out how to use the rotation to figure out the direction (y = tan(v) * x).

Anyway, I get hold of the "player" and want to create a shot like in:

let player = engine.sprites.get_mut("player").unwrap();                                                                                                                                      
game_state.shot_counter += 1;                                                                                                                                                                
let sprite = engine.add_sprite(                                                                                                                                                              
      format!("shot{}", game_state.shot_counter),                                                                                                                                              
      SpritePreset::RollingBallRed,);
 sprite.rotation = player.rotation;
 ...etc...                                                                                                                                                              

But I get a second mutable borrow occurs here error at the second reference to engine,
and now I'm stuck...still struggling with the basics of Rust I guess...

Ahh...after some experiments I solved it by doing like this:

let player = engine.sprites.get_mut("player").unwrap();
let player_rotation = player.rotation;
....
   sprite.rotation = player_rotation;
....

Still not sure why the compiler complained though, I thought the player.rotation was a f32
and thus should "copy" the float?
Anyway, sorry for the noice :-)

That's a great way to work around it!

The issue you encountered is that engine.sprites is a hash map, and the hash map itself is mutably borrowed when you do

let player = engine.sprites.get_mut("player").unwrap();   

and remains borrowed as long as player needs to be used. Your change made it so the compiler invalidate the mutable reference player earlier than it normally would when you wanted to borrow it again with engine.add_sprite, because it knows player is never used again in the same scope.

Sidenote: This is exactly the sort of issue that using an ECS will let you avoid, but then you have to learn what an ECS is and how to use it. Rusty Engine wraps Bevy's ECS system with a monsterous Engine struct that holds everything, which makes it simple to use in small programs, but awkward and low performance as things scale. Rusty Engine is specifically for folks to practice learning Rust, but folks will need to migrate to a more suitable tool if they actually want to make Real™ games.