Crates.io | bevy-tokio-tasks |
lib.rs | bevy-tokio-tasks |
version | 0.14.0 |
source | src |
created_at | 2023-01-05 20:02:42.771105 |
updated_at | 2024-07-05 16:09:45.118791 |
description | Simple integration of a Tokio runtime into a Bevy app for background processing. |
homepage | https://crates.io/crates/bevy-tokio-tasks |
repository | https://github.com/EkardNT/bevy-tokio-tasks |
max_upload_size | |
id | 751774 |
size | 114,266 |
A simple Bevy plugin which integrates a Tokio runtime into a Bevy app.
To initialize the plugin, simply install the TokioTasksPlugin
when initializing your Bevy app. The simplest
way to do this is to use the TokioTasksPlugin::default()
method, which sets up a Tokio Runtime
with default
settings (the plugin calls Runtime::enable_all
to enable Tokio's IO and timing functionality).
fn main() {
bevy::App::new()
.add_plugins(bevy_tokio_tasks::TokioTasksPlugin::default())
}
If you want to customize the Tokio Runtime
setup, you may do so by specifying a make_runtime
callback on
the TokioTasksPlugin
.
fn main() {
bevy::App::new()
.add_plugins(bevy_tokio_tasks::TokioTasksPlugin {
make_runtime: Box::new(|| {
let mut runtime = tokio::runtime::Builder::new_multi_thread();
runtime.enable_all();
runtime.build().unwrap()
}),
..bevy_tokio_tasks::TokioTasksPlugin::default()
})
}
To spawn a background task from a Bevy system function, add a TokioTasksRuntime
as a resource parameter and call
the spawn_background_task
function.
fn example_system(runtime: ResMut<TokioTasksRuntime>) {
runtime.spawn_background_task(|_ctx| async move {
println!("This task is running on a background thread");
});
}
Often times, background tasks will need to synchronize with the main Bevy app at certain points. You may do this
by calling the run_on_main_thread
function on the TaskContext
that is passed to each background task.
fn example_system(runtime: ResMut<TokioTasksRuntime>) {
runtime.spawn_background_task(|mut ctx| async move {
println!("This print executes from a background Tokio runtime thread");
ctx.run_on_main_thread(move |ctx| {
// The inner context gives access to a mutable Bevy World reference.
let _world: &mut World = ctx.world;
}).await;
});
}
async fn
to spawn_background_task
.This crate's major and minor version numbers will match Bevy's. To allow this crate to publish updates between Bevy updates, the patch version is allowed to increment independent of Bevy's release cycle.
bevy-tokio-tasks version | bevy version | tokio version |
---|---|---|
0.14.0 | 0.14.0 | 1 |
0.13.0 | 0.13.0 | 1 |
0.12.0 | 0.12.0 | 1 |
0.11.0 | 0.11.0 | 1 |
0.10.2 | 0.10.1 | 1 |
0.10.1 | 0.10.0 | 1 |
0.10.0 | 0.10.0 | 1 |
0.9.5 | 0.9.1 | 1 |
0.9.4 | 0.9.1 | 1 |
0.9.3 | 0.9.1 | 1 |
0.9.2 | 0.9.1 | 1 |
0.9.1 | 0.9.1 | 1 |
0.9.0 | 0.9.1 | 1 |