Crates.io | bevy_coroutine |
lib.rs | bevy_coroutine |
version | |
source | src |
created_at | 2024-07-02 13:20:07.196428 |
updated_at | 2024-12-08 12:45:01.125439 |
description | A simple library to run coroutines in Bevy |
homepage | |
repository | https://github.com/Maaxed/bevy_coroutine/ |
max_upload_size | |
id | 1289579 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A simple Bevy library to run coroutines similar to Unity's coroutines.
Bevy Coroutine is very experimental and new versions may contain breaking changes !
The main motivation behind Bevy Coroutine is to allows you to spread the execution of a system across several frames. It also helps with managing and running systems in sequence over time.
use bevy::prelude::*;
use bevy_coroutine::prelude::*;
fn startup_system(mut commands: Commands) {
// Launch the coroutine from a system
commands.queue(Coroutine::new(my_coroutine));
}
fn my_coroutine(
) -> CoResult {
let mut res = co_break();
// Print number from 0 to 3, printing a single number every second
for i in 0..=3 {
res.add_subroutines((
wait(std::time::Duration::from_secs(1)),
with_input(i, print_number),
));
}
res
}
fn print_number(
In(i): In<u32>,
) -> CoResult {
println!("{i}");
co_break()
}
Coroutines are systems. They can access any system parameter like Query
and Res
.
Coroutines don't use a 'yield' statement but instead return a CoResult
.
The CoResult
indicates if the coroutine should 'break' and stops its execution or 'continue' and be executed again at the next update.
In addition, a coroutine can run other coroutines and wait for their completion by adding them as subroutines to the CoResult
.
The state of execution of your coroutine can be stored between frames using Local
s as parameters in the coroutine system.
use bevy::prelude::*;
use bevy_coroutine::prelude::*;
fn my_coroutine(
mut i: Local<u32>,
) -> CoResult {
if *i <= 3
{
println!("{}", *i);
*i += 1;
return co_continue(); // Rerun the system next frame
}
co_break()
}
Each coroutine run in an exclusive system. They won't run in parallel with each other nor with other bevy systems.
For CPU intensive tasks, consider using bevy_tasks or bevy_defer.
bevy | bevy_coroutine |
---|---|
0.15 | 0.2.0 |
0.14 | 0.1.3 |
0.14 | 0.1.2 |
0.14 | 0.1.1 |
0.14.0-rc.4 | 0.1.0 |