Skip to content

Commit 3f13a88

Browse files
committed
rework example
1 parent e93fdfa commit 3f13a88

File tree

1 file changed

+63
-42
lines changed

1 file changed

+63
-42
lines changed
Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,72 @@
1+
use bevy::core::FixedTimestep;
12
use bevy::prelude::*;
23

34
fn main() {
45
App::build()
5-
.add_startup_system(handle_command_error.system())
6+
.add_startup_system(setup.system())
7+
.add_system_set(
8+
SystemSet::new()
9+
.with_run_criteria(FixedTimestep::step(1.0))
10+
.with_system(despawn_all_entities.system()),
11+
)
12+
.add_system(insert_components.system())
613
.run();
714
}
815

916
#[derive(Debug)]
10-
struct AComponent(usize);
11-
12-
fn handle_command_error(mut commands: Commands) {
13-
let e = commands.spawn().id();
14-
15-
// Immediately despawn the entity.
16-
commands.entity(e).despawn();
17-
18-
// This `despawn` command will fail because the entity was already despawned!
19-
// If no error handler is specified, the error will be logged.
20-
commands.entity(e).despawn();
21-
22-
// Optionally, `on_failure` allows you to provide a custom error handler!
23-
commands
24-
.entity(e)
25-
.insert(AComponent(0))
26-
.on_failure(|CommandError { error, world, .. }| {
27-
// You'll notice that the `error` will also give you back the component you
28-
// attempted to insert on the entity.
29-
30-
println!(
31-
"Sadly our component '{:?}' for entity '{:?}' didn't insert... :(",
32-
error.component, error.entity
33-
);
34-
35-
// error handlers have mutable access to `World`
36-
world.insert_resource("🐦");
37-
});
38-
39-
// Some nice things:
40-
// - You can still chain commands!
41-
// - There are a slew of built-in error handlers
42-
commands
43-
.entity(e)
44-
.insert(AComponent(1))
45-
.ignore() // `ignore` will neither log nor panic the error
46-
.insert(AComponent(2))
47-
.log_on_failure(); // `log_on_failure` is the default behavior, and will log the error
48-
49-
// Uncomment the below line to see the command error cause a panic due to `panic_on_failure`
50-
// commands.entity(e).despawn().panic_on_failure();
17+
struct A(usize);
18+
19+
#[derive(Bundle, Default)]
20+
struct B {
21+
value: usize,
22+
}
23+
24+
struct FailedDespawnAttempts(usize);
25+
26+
fn setup(mut commands: Commands) {
27+
for i in 0..3 {
28+
// Note that `insert` is a fallible function and could fail if the entity doesn't exist.
29+
// If no error handler is specified, the default behavior is to log the error, and continue.
30+
// This call to `insert` will not fail, since the entity is valid.
31+
commands.spawn().insert(A(i));
32+
}
33+
34+
commands.insert_resource(FailedDespawnAttempts(0));
35+
}
36+
37+
fn despawn_all_entities(mut commands: Commands, query: Query<Entity>) {
38+
for e in query.iter() {
39+
// `on_failure` allows you to provide a custom error handler!
40+
commands
41+
.entity(e)
42+
.despawn()
43+
.on_failure(|CommandError { error, world, .. }| {
44+
// You'll notice that the `error` will also give you back the entity
45+
// you tried to despawn.
46+
let entity = error.entity;
47+
48+
println!("Sadly our entity '{:?}' didn't despawned... :(", entity);
49+
50+
// error handlers have mutable access to `World`
51+
if let Some(mut failed_despawns) = world.get_resource_mut::<FailedDespawnAttempts>()
52+
{
53+
failed_despawns.0 += 1;
54+
}
55+
});
56+
}
57+
}
58+
59+
fn insert_components(mut commands: Commands, query: Query<Entity>) {
60+
for (i, e) in query.iter().enumerate() {
61+
// Some nice things:
62+
// - You can still chain commands!
63+
// - There are a slew of built-in error handlers
64+
commands
65+
.entity(e)
66+
.insert(A(i))
67+
.ignore() // `ignore` will neither log nor panic the error
68+
.insert_bundle(B::default())
69+
.log_on_failure(); // `log_on_failure` is the default behavior, and will log the error.
70+
// `panic_on_failure` is another alternative which will panic on the error.
71+
}
5172
}

0 commit comments

Comments
 (0)