-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e93fdfa
commit 3f13a88
Showing
1 changed file
with
63 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,72 @@ | ||
use bevy::core::FixedTimestep; | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::build() | ||
.add_startup_system(handle_command_error.system()) | ||
.add_startup_system(setup.system()) | ||
.add_system_set( | ||
SystemSet::new() | ||
.with_run_criteria(FixedTimestep::step(1.0)) | ||
.with_system(despawn_all_entities.system()), | ||
) | ||
.add_system(insert_components.system()) | ||
.run(); | ||
} | ||
|
||
#[derive(Debug)] | ||
struct AComponent(usize); | ||
|
||
fn handle_command_error(mut commands: Commands) { | ||
let e = commands.spawn().id(); | ||
|
||
// Immediately despawn the entity. | ||
commands.entity(e).despawn(); | ||
|
||
// This `despawn` command will fail because the entity was already despawned! | ||
// If no error handler is specified, the error will be logged. | ||
commands.entity(e).despawn(); | ||
|
||
// Optionally, `on_failure` allows you to provide a custom error handler! | ||
commands | ||
.entity(e) | ||
.insert(AComponent(0)) | ||
.on_failure(|CommandError { error, world, .. }| { | ||
// You'll notice that the `error` will also give you back the component you | ||
// attempted to insert on the entity. | ||
|
||
println!( | ||
"Sadly our component '{:?}' for entity '{:?}' didn't insert... :(", | ||
error.component, error.entity | ||
); | ||
|
||
// error handlers have mutable access to `World` | ||
world.insert_resource("🐦"); | ||
}); | ||
|
||
// Some nice things: | ||
// - You can still chain commands! | ||
// - There are a slew of built-in error handlers | ||
commands | ||
.entity(e) | ||
.insert(AComponent(1)) | ||
.ignore() // `ignore` will neither log nor panic the error | ||
.insert(AComponent(2)) | ||
.log_on_failure(); // `log_on_failure` is the default behavior, and will log the error | ||
|
||
// Uncomment the below line to see the command error cause a panic due to `panic_on_failure` | ||
// commands.entity(e).despawn().panic_on_failure(); | ||
struct A(usize); | ||
|
||
#[derive(Bundle, Default)] | ||
struct B { | ||
value: usize, | ||
} | ||
|
||
struct FailedDespawnAttempts(usize); | ||
|
||
fn setup(mut commands: Commands) { | ||
for i in 0..3 { | ||
// Note that `insert` is a fallible function and could fail if the entity doesn't exist. | ||
// If no error handler is specified, the default behavior is to log the error, and continue. | ||
// This call to `insert` will not fail, since the entity is valid. | ||
commands.spawn().insert(A(i)); | ||
} | ||
|
||
commands.insert_resource(FailedDespawnAttempts(0)); | ||
} | ||
|
||
fn despawn_all_entities(mut commands: Commands, query: Query<Entity>) { | ||
for e in query.iter() { | ||
// `on_failure` allows you to provide a custom error handler! | ||
commands | ||
.entity(e) | ||
.despawn() | ||
.on_failure(|CommandError { error, world, .. }| { | ||
// You'll notice that the `error` will also give you back the entity | ||
// you tried to despawn. | ||
let entity = error.entity; | ||
|
||
println!("Sadly our entity '{:?}' didn't despawned... :(", entity); | ||
|
||
// error handlers have mutable access to `World` | ||
if let Some(mut failed_despawns) = world.get_resource_mut::<FailedDespawnAttempts>() | ||
{ | ||
failed_despawns.0 += 1; | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn insert_components(mut commands: Commands, query: Query<Entity>) { | ||
for (i, e) in query.iter().enumerate() { | ||
// Some nice things: | ||
// - You can still chain commands! | ||
// - There are a slew of built-in error handlers | ||
commands | ||
.entity(e) | ||
.insert(A(i)) | ||
.ignore() // `ignore` will neither log nor panic the error | ||
.insert_bundle(B::default()) | ||
.log_on_failure(); // `log_on_failure` is the default behavior, and will log the error. | ||
// `panic_on_failure` is another alternative which will panic on the error. | ||
} | ||
} |