-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include examples for command error handling
- Loading branch information
1 parent
141b5fa
commit bbb4e5d
Showing
5 changed files
with
61 additions
and
4 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::build() | ||
.add_startup_system(handle_command_error.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(); | ||
} |