Skip to content

Commit

Permalink
Fallible param notes (#1729)
Browse files Browse the repository at this point in the history
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
  • Loading branch information
MiniaczQ and alice-i-cecile authored Oct 31, 2024
1 parent 9f18e4b commit 3bd3a4c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
<!-- Reduce runtime panics through `SystemParam` validation -->
<!-- https://github.com/bevyengine/bevy/pull/15276 -->

<!-- TODO -->
<!-- `QuerySingle` family of system params -->
<!-- https://github.com/bevyengine/bevy/pull/15476 -->

<!-- `Populated` (query) system param -->
<!-- https://github.com/bevyengine/bevy/pull/15488 -->


In Bevy 0.14 and prior, the following code would panic:
```rust
#[derive(Resource)]
struct MyResource;

fn my_system(my_resource: Res<MyResource>) {}

fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
app.add_systems(my_system);
// Panic here: `my_system` cannot fetch `MyResource`, because it was never added.
app.run();
}
```
but in Bevy 0.15, `my_system` simply won't be executed.

This works for all system-based features:
- Systems & Observers - will be skipped,
- Run conditions - will be skipped and return `false`.

Compound systems, like `system_a.pipe(system_b)`, are currently skipped if any required data is missing.

Pre-existing parameters which now benefit from this feature are: `Res` and `ResMut` as well as their siblings `NonSend` and `NonSendMut`.
Parameters that build on top of other parameters: tuples, `DynSystemParam` and `ParamSet` are considered present if and only if all of their system parameters are present.

Additionally, few new system params were introduced to simplify existing code:
- `Single<D, F>` - Works like `Query<D, F>::single`, fails if query contains 0 or more than 1 match,
- `Option<Single<D, F>>` - Works like `Query<D, F>::single`, fails if query contains more than 1 match,
- `Populated<D, F>` - Works like a `Query<D, F>`, fails if query contains no matches.

## Warnings

Fallible system params come with a primitive warning mechanic.
Currently, systems can behave in one of two ways:
- (default) warn exactly once,
- never warn.

The default can be changed as following:
```rust
// For systems
app.add_systems(my_system.never_param_warn());
// For observers
app.add_observer(my_observer.never_param_warn());
// For run conditions
app.add_systems(my_system.run_if(my_condition.never_param_warn()));
```

Let us know what other warning strategies you'd like!

This file was deleted.

This file was deleted.

20 changes: 3 additions & 17 deletions release-content/0.15/release-notes/_release-notes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ prs = [14920]
file_name = "14920_Add_cached_run_system_API.md"

[[release_notes]]
title = "Reduce runtime panics through `SystemParam` validation"
title = "Fallible system parameters"
authors = ["@MiniaczQ"]
contributors = []
prs = [15276]
contributors = ["@alice-i-cecile"]
prs = [15276, 15476, 15488]
file_name = "15276_Reduce_runtime_panics_through_SystemParam_validation.md"

[[release_notes]]
Expand Down Expand Up @@ -311,13 +311,6 @@ contributors = ["@cart"]
prs = [12770]
file_name = "12770_Implement_gamepads_as_entities.md"

[[release_notes]]
title = "`QuerySingle` family of system params"
authors = ["@MiniaczQ"]
contributors = []
prs = [15476]
file_name = "15476_QuerySingle_family_of_system_params.md"

[[release_notes]]
title = "Implement volumetric fog support for both point lights and spotlights"
authors = ["@Soulghost"]
Expand Down Expand Up @@ -353,13 +346,6 @@ contributors = []
prs = [14838]
file_name = "14838_bevy_reflect_add_Type_type.md"

[[release_notes]]
title = "`Populated` (query) system param"
authors = ["@MiniaczQ"]
contributors = ["@alice-i-cecile"]
prs = [15488]
file_name = "15488_Populated_query_system_param.md"

[[release_notes]]
title = "Runtime required components"
authors = ["@Jondolf"]
Expand Down

0 comments on commit 3bd3a4c

Please sign in to comment.