-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Deprecate FilteredResources and similair structs
#25331
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| --- | ||
| title: "`FilteredResources` and similar structs have been deprecated" | ||
| pull_requests: [25331] | ||
| --- | ||
|
|
||
| `FilteredResources`, `FilteredResourcesMut`, `FilteredResourcesBuilder`, `FilteredResourcesMutBuilder`, `FilteredResourcesParamBuilder`, and `FilteredResourcesMutParamBuilder`, have been deprecated in favor of `QueryBuilder` and `QueryParamBuilder`. | ||
|
|
||
| The API has changed somewhat, below we provide an example. | ||
|
|
||
| ```rust | ||
| // 0.19 | ||
| let system = | ||
| FilteredResourcesParamBuilder::new(|builder| { | ||
| builder.add_read::<ResA>(); | ||
| }) | ||
| .build_state(&mut world) | ||
| .build_system(resource_system); | ||
|
|
||
| fn resource_system(filtered: FilteredResources) { | ||
| let resource_a: Ref<ResA> = filtered.get::<ResA>().unwrap(); | ||
| } | ||
|
|
||
| // 0.20 | ||
| let system = | ||
| QueryParamBuilder::new(|builder| { | ||
| builder.data::<Ref<ResA>>().with::<IsResource>(); | ||
| }) | ||
| .build_state(&mut world) | ||
| .build_system(resource_system); | ||
|
|
||
| fn resource_system(query: Query<()>) { | ||
| let resource_a: Ref<ResA> = query.single().unwrap(); | ||
|
Contributor
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. It might be worth including some guidance for uses of Another option if we merge your components-as-entities PR would be to create a single query for resources that has all of the access and is accessed with Or, hmm, in the meantime, would it make sense to |
||
| } | ||
| ``` | ||
|
|
||
| So instead of a `FilteredResourcesParamBuilder` that provides a `FilteredResourcesBuilder`, which resolves to `FilteredResources`, we have a `QueryParamBuilder` that provides a `QueryBuilder` that resolves to a `Query`. The `Mut` variants also turn into `Query`, `QueryParam`, and `QueryParamBuilder`. | ||
| Most of the migration should be rather straightforward, but there are some specifics we need to clear up. | ||
| First, change detection was automatically included for `FilteredResources` and `FilteredResourcesMut`, which is now opt-in. You have to specify `Ref` and `Mut` in `QueryBuilder::data` if you want change detection. | ||
|
Contributor
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. Nope, you can use |
||
| Secondly, when is adding `.with::<IsResource>` necessary? In general, `.with::<IsResource>` is used to stop system conflicts. Take a look at the following example: | ||
|
|
||
| ```rust | ||
| // 0.20 | ||
| fn resource_system(resource_query: Query<()>, broad_query: Query<EntityMut>) {} | ||
|
|
||
| let system = ( | ||
| QueryParamBuilder::new(|builder| { | ||
| builder.data::<&mut ResA>(); | ||
| }), | ||
| ParamBuilder, | ||
| ) | ||
| .build_state(&mut world) | ||
| .build_system(resource_system); // panic! | ||
| ``` | ||
|
|
||
| Here, `.build_system` panics, because `broad_query` also has mutable access to `ResA`, just as `resource_query` does. | ||
| In order to avoid conflicts, you can add an `IsResource` filter, like so: | ||
|
|
||
| ```rust | ||
| // 0.20 | ||
| fn resource_system(resource_query: Query<()>, broad_query: Query<EntityMut, Without<IsResource>>) {} | ||
|
|
||
| let system = ( | ||
| QueryParamBuilder::new(|builder| { | ||
| builder.data::<&mut ResA>().with::<IsResource>(); | ||
| }), | ||
| ParamBuilder, | ||
| ) | ||
| .build_state(&mut world) | ||
| .build_system(resource_system); // works! | ||
| ``` | ||
|
|
||
| Adding `IsResource` is therefor only occasionally necessary, as these conflicts arise. Still, since a resource entity always has an `IsResource` marker attached, it can't hurt. | ||
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.
This doesn't compile, does it? I think you want
Query<FilteredEntityRef>and then to useget<R>orget_by_idto get the resource back out.