| Crates.io | pecs |
| lib.rs | pecs |
| version | 0.6.0 |
| created_at | 2023-02-11 19:38:11.792077+00 |
| updated_at | 2024-03-18 10:21:36.318838+00 |
| description | Asynchronous operations for Bevy Engine |
| homepage | https://github.com/jkb0o/pecs |
| repository | https://github.com/jkb0o/pecs |
| max_upload_size | |
| id | 782664 |
| size | 4,732,708 |
pecs is a plugin for Bevy that allows you to execute code asynchronously
by chaining multiple promises as part of Bevy's ecs environment.
pecs stands for Promise Entity Component System.
Resources:
Compatibility:
| bevy | pecs |
|---|---|
| 0.13 | 0.6 |
| 0.12 | 0.5 |
| 0.11 | 0.4 |
| 0.10 | 0.3 |
| 0.9 | 0.2 |
then()/then_repeat()state for promises is like self for items).asyn mod and
stateful state.asyn() method.asyn! functions accept the same parameters as Bevy systems do).any/all for tuple/vec of promises via stateless Promise::any()
/Promise::all() methods or stateful state.any()/state.all() methods.with(value)/map(func) (changes state type/value over chain calls).with_result(value)/map_result(func) (changes result type/value over chain calls).use bevy::prelude::*;
use pecs::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(PecsPlugin)
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, time: Res<Time>) {
let start = time.elapsed_seconds();
commands
// create PromiseLike chainable commands
// with the current time as state
.promise(|| start)
// will be executed right after current stage
.then(asyn!(state => {
info!("Wait a second..");
state.asyn().timeout(1.0)
}))
// will be executed after in a second after previous call
.then(asyn!(state => {
info!("How large is is the Bevy main web page?");
state.asyn().http().get("https://bevyengine.org")
}))
// will be executed after request completes
.then(asyn!(state, result => {
match result {
Ok(response) => info!("It is {} bytes!", response.bytes.len()),
Err(err) => info!("Ahhh... something goes wrong: {err}")
}
state.pass()
}))
// will be executed right after the previous one
.then(asyn!(state, time: Res<Time> => {
let duration = time.elapsed_seconds() - state.value;
info!("It took {duration:0.2}s to do this job.");
info!("Exiting now");
asyn::app::exit()
}));
}
There is the output of the above example, pay some attention to time stamps:
18.667 INFO bevy_render::renderer: AdapterInfo { ... }
18.835 INFO simple: Wait a second..
19.842 INFO simple: How large is is the Bevy main web page?
19.924 INFO simple: It is 17759 bytes!
19.924 INFO simple: It tooks 1.09s to do this job.
19.924 INFO simple: Exiting now
This crate is pretty young. API could and will change. The app may crash. Some promises could silently drop. Documentation is incomplete.
But. But. Examples work like a charm. And this fact gives us a lot of hope.
The pecs is dual-licensed under either:
This means you can select the license you prefer! This dual-licensing approach is the de-facto standard in the Rust ecosystem and there are very good reasons to include both.