Last active
April 27, 2025 06:17
-
-
Save evanrelf/e47ef926662ae2e3d413acc818688b88 to your computer and use it in GitHub Desktop.
park bevy app main thread until there's work to do. full version: https://github.com/evanrelf/indigo/blob/59a733f173c0e0ce754a0da7d0b1a590aa05621b/crates/indigo-bevy/src/park.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// defining the plugin: | |
use bevy::{prelude::*, tasks::IoTaskPool}; | |
pub struct Parker(parking::Parker); | |
#[derive(Resource)] | |
pub struct Unparker(pub parking::Unparker); | |
fn park_system(parker: NonSend<Parker>) { | |
parker.0.park(); | |
} | |
pub struct ParkPlugin; | |
impl Plugin for ParkPlugin { | |
fn build(&self, app: &mut App) { | |
let (parker, unparker) = parking::pair(); | |
app.insert_non_send_resource(Parker(parker)) | |
.insert_resource(Unparker(unparker)) | |
.add_systems(Last, park_system); | |
} | |
} | |
// using the plugin: | |
fn some_other_system(unparker: Res<Unparker>) { | |
let task_pool = IoTaskPool::get(); | |
let unparker = unparker.0.clone(); | |
let task = task_pool.spawn(async move { | |
// feed some input to the bevy app here | |
todo!(); | |
// wake up the main thread to let bevy loose on the new inputs | |
unparker.unpark(); | |
}); | |
task.detach(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment