|
1 |
| -use bevy::prelude::*; |
| 1 | +use bevy::{core::FixedTimestep, prelude::*}; |
2 | 2 |
|
3 | 3 | fn main() {
|
4 | 4 | App::build()
|
5 |
| - .add_startup_system(handle_command_error.system()) |
| 5 | + .add_startup_system(setup.system()) |
| 6 | + .add_system_set( |
| 7 | + SystemSet::new() |
| 8 | + .with_run_criteria(FixedTimestep::step(0.5)) |
| 9 | + .with_system(remove_components.system()), |
| 10 | + ) |
| 11 | + .add_system_set( |
| 12 | + SystemSet::new() |
| 13 | + .with_run_criteria(FixedTimestep::step(2.0)) |
| 14 | + .with_system(despawn_all_entities.system()), |
| 15 | + ) |
6 | 16 | .run();
|
7 | 17 | }
|
8 | 18 |
|
9 |
| -#[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(); |
| 19 | +struct A(usize); |
| 20 | + |
| 21 | +#[derive(Bundle, Default)] |
| 22 | +struct B { |
| 23 | + value: usize, |
| 24 | +} |
| 25 | + |
| 26 | +struct FailedDespawnAttempts(usize); |
| 27 | + |
| 28 | +fn setup(mut commands: Commands) { |
| 29 | + for i in 0..3 { |
| 30 | + // Note that `insert` and `insert_bundle` are fallible functions. |
| 31 | + // If no error handler is specified, the default behavior is to log the error, and continue. |
| 32 | + // These calls to `insert` and `insert_bundle` will not fail, since the entity is valid. |
| 33 | + commands.spawn().insert(A(i)).insert_bundle(B::default()); |
| 34 | + } |
| 35 | + |
| 36 | + commands.insert_resource(FailedDespawnAttempts(0)); |
| 37 | +} |
| 38 | + |
| 39 | +fn despawn_all_entities(mut commands: Commands, query: Query<Entity>) { |
| 40 | + for e in query.iter() { |
| 41 | + // `on_failure` allows you to provide a custom error handler! |
| 42 | + commands |
| 43 | + .entity(e) |
| 44 | + .despawn() |
| 45 | + .on_failure(|CommandError { error, world, .. }| { |
| 46 | + // You'll notice that the `error` will also give you back the entity |
| 47 | + // you tried to despawn. |
| 48 | + let entity = error.entity; |
| 49 | + |
| 50 | + println!("Sadly our entity '{:?}' didn't despawned... :(", entity); |
| 51 | + |
| 52 | + // error handlers have mutable access to `World` |
| 53 | + if let Some(mut failed_despawns) = world.get_resource_mut::<FailedDespawnAttempts>() |
| 54 | + { |
| 55 | + failed_despawns.0 += 1; |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +fn remove_components(mut commands: Commands, query: Query<Entity>) { |
| 62 | + for e in query.iter() { |
| 63 | + // Some nice things: |
| 64 | + // - You can still chain commands! |
| 65 | + // - There are a slew of built-in error handlers |
| 66 | + commands |
| 67 | + .entity(e) |
| 68 | + .remove::<A>() |
| 69 | + .ignore() // `ignore` will neither log nor panic the error |
| 70 | + .remove_bundle::<B>() |
| 71 | + .log_on_failure(); // `log_on_failure` is the default behavior, and will log the error. |
| 72 | + // `panic_on_failure` is another alternative which will panic on the error. |
| 73 | + } |
51 | 74 | }
|
0 commit comments