How to read a value generated inside a long running task? #2506
-
Using // Just placeholder for this question
struct WorldGenTask(Option<Task<WorldGenResult>>);
struct WorldGenResult(u32);
fn gen_world_system(
pool: Res<AsyncComputeTaskPool>,
mut task: ResMut<WorldGenTask>,
input: Res<Input<KeyCode>>,
) {
if task.0.is_none() && input.just_pressed(KeyCode::Space) {
let t = pool.spawn(temp());
*task = WorldGenTask(Some(t));
}
}
fn gen_world() -> WorldGenResult {
// Do some CPU intensive work generating the world
WorldGenResult(42)
} This spawns the task on the task pool and runs it, but how can I now get the value generated inside a system to spawn my components? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Look here: https://github.com/bevyengine/bevy/blob/main/examples/async_tasks/async_compute.rs This example was added after 0.5, but it should still get the point across :) |
Beta Was this translation helpful? Give feedback.
Look here: https://github.com/bevyengine/bevy/blob/main/examples/async_tasks/async_compute.rs
This example was added after 0.5, but it should still get the point across :)