-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
feat(ecs): implement fallible observer systems #17731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alice-i-cecile
merged 7 commits into
bevyengine:main
from
dcdpr:fallible-observer-systems
Feb 11, 2025
+259
−19
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2f4b47f
feat(ecs): implement fallible observer systems
JeanMertz 9113b90
Merge branch 'main' into fallible-observer-systems
JeanMertz 1c4b6e5
resolve review feedback
JeanMertz 6e120a2
resolve review feedback
JeanMertz a11c9e4
fixup! resolve review feedback
JeanMertz 4abe402
Merge branch 'main' into fallible-observer-systems
JeanMertz 37b8ba1
merge with system error handling
JeanMertz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 +1,7 @@ | ||
//! Showcases how fallible systems can be make use of rust's powerful result handling syntax. | ||
//! Showcases how fallible systems and observers can make use of Rust's powerful result handling | ||
//! syntax. | ||
use bevy::ecs::world::DeferredWorld; | ||
use bevy::math::sampling::UniformMeshSampler; | ||
use bevy::prelude::*; | ||
|
||
|
@@ -10,6 +12,9 @@ fn main() { | |
|
||
app.add_plugins(DefaultPlugins); | ||
|
||
#[cfg(feature = "bevy_mesh_picking_backend")] | ||
app.add_plugins(MeshPickingPlugin); | ||
|
||
// Fallible systems can be used the same way as regular systems. The only difference is they | ||
// return a `Result<(), Box<dyn Error>>` instead of a `()` (unit) type. Bevy will handle both | ||
// types of systems the same way, except for the error handling. | ||
|
@@ -44,6 +49,9 @@ fn main() { | |
}), | ||
); | ||
|
||
// Fallible observers are also sypported. | ||
app.add_observer(fallible_observer); | ||
|
||
// If we run the app, we'll see the following output at startup: | ||
// | ||
// WARN Encountered an error in system `fallible_systems::failing_system`: "Resource not initialized" | ||
|
@@ -53,6 +61,8 @@ fn main() { | |
} | ||
|
||
/// An example of a system that calls several fallible functions with the question mark operator. | ||
/// | ||
/// See: <https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yay! |
||
fn setup( | ||
mut commands: Commands, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
|
@@ -118,6 +128,29 @@ fn setup( | |
Ok(()) | ||
} | ||
|
||
// Observer systems can also return a `Result`. | ||
fn fallible_observer( | ||
trigger: Trigger<Pointer<Move>>, | ||
mut world: DeferredWorld, | ||
mut step: Local<f32>, | ||
) -> Result { | ||
let mut transform = world | ||
.get_mut::<Transform>(trigger.target) | ||
.ok_or("No transform found.")?; | ||
|
||
*step = if transform.translation.x > 3. { | ||
-0.1 | ||
} else if transform.translation.x < -3. || *step == 0. { | ||
0.1 | ||
} else { | ||
*step | ||
}; | ||
|
||
transform.translation.x += *step; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[derive(Resource)] | ||
struct UninitializedResource; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading this block of code makes me feel like Im a kid running in the street 😆