#coroutine #bevy #async #gamedev #game-engine #game

bevy_coroutine

A simple library to run coroutines in Bevy

4 releases

0.1.3 Oct 23, 2024
0.1.2 Jul 15, 2024
0.1.1 Jul 6, 2024
0.1.0 Jul 2, 2024

#559 in Game dev

Download history 6/week @ 2024-07-20 7/week @ 2024-07-27 4/week @ 2024-09-21 15/week @ 2024-10-12 138/week @ 2024-10-19 13/week @ 2024-10-26 15/week @ 2024-11-02

181 downloads per month

MIT license

34KB
318 lines

Bevy Coroutine

Docs Crates.io Downloads license

A simple Bevy library to run coroutines similar to Unity's coroutines.
Bevy Coroutine is very experimental and new versions may contain breaking changes !

Usage

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.add(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 Locals 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()
}

Performance

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.

Versions

bevy bevy_coroutine
0.14 0.1.3
0.14 0.1.2
0.14 0.1.1
0.14.0-rc.4 0.1.0

Dependencies

~25MB
~464K SLoC