-
Notifications
You must be signed in to change notification settings - Fork 6
/
simple.rs
35 lines (32 loc) · 978 Bytes
/
simple.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use async_std::task::sleep;
use bevy::{app::PanicHandlerPlugin, log::LogPlugin, prelude::*};
use bevy_async_task::{AsyncTaskRunner, AsyncTaskStatus};
use std::time::Duration;
/// An async task that takes time to compute!
async fn long_task() -> u32 {
sleep(Duration::from_millis(1000)).await;
5
}
fn my_system(mut task_executor: AsyncTaskRunner<u32>) {
match task_executor.poll() {
AsyncTaskStatus::Idle => {
// Start an async task!
task_executor.start(long_task());
// Closures also work:
// task_executor.start(async { 5 });
info!("Started!");
}
AsyncTaskStatus::Pending => {
// Waiting...
}
AsyncTaskStatus::Finished(v) => {
info!("Received {v}");
}
}
}
pub fn main() {
App::new()
.add_plugins((MinimalPlugins, LogPlugin::default(), PanicHandlerPlugin))
.add_systems(Update, my_system)
.run();
}