How can I create a loop in a thread with gtk-rs to move a player character?(GTK-RS)

Hello, I want to move my player character downward constantly with a thread and a while loop. However, GTK-RS is not thread safe. I want to know how I can do this, or if there’s a different approach that I could take. Code:

pub fn create_player(layout: &Fixed){
 let player = Image::from_file("/home/dequog/dev/Game/src/bird.png");

 let mut y = 20;

 layout.put(&player, 20, y);  

 thread::spawn(move || {
     while true {
         y += 1;
         layout.move_(&player, 20, y);
         thread::sleep(Duration::from_millis(1));
     }
 });
}

You could use a channel to communicate from your thread with the main thread, or any other futures-enabled channel.

Alternatively you could also run your game loop on the main thread as an async task if it’s not too CPU intensive.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.