-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
#[derive(Query)] #786
Comments
For example, I have the query |
I wonder how much overlap this has with #[derive(Bundle, Query)]
struct Enemy {
behaviour: Ai,
health: Health,
transform: Transform,
extra: Stats,
} Looks like this has already bene mentioned in #9 |
Does it make sense to have |
Derive Bundle and derive query wouldn't work; queryies need references not the values. |
I assumed it would be possible to map |
That screws up mutable borrows, and doesn't really make sense (especially as it would have to generate a separate type). |
@Plecra I think the only way to map |
I'm also looking forward to seeing this feature in Bevy. Before stumbling upon on this issue, I've created #800 - this feature request covers both components and resources, and might give some additional idea about the motivation for the feature. |
@smokku, I think it makes perfect sense since |
It makes sense for Query. My question is about Bundle. |
@smokku right, I mean that everything that is queriable makes sense to be a part of a bundle. I don't think there's any reason not to support |
Ralith/hecs#110 was opened recently to add a |
Was this addressed by #798 ?
Edit: Sorry, never mind. This is for that inner param. |
I would say having it being called |
Yeah, I'd say it's something worth backporting to |
# Objective - Closes bevyengine#786 - Closes bevyengine#2252 - Closes bevyengine#2588 This PR implements a derive macro that allows users to define their queries as structs with named fields. ## Example ```rust #[derive(WorldQuery)] #[world_query(derive(Debug))] struct NumQuery<'w, T: Component, P: Component> { entity: Entity, u: UNumQuery<'w>, generic: GenericQuery<'w, T, P>, } #[derive(WorldQuery)] #[world_query(derive(Debug))] struct UNumQuery<'w> { u_16: &'w u16, u_32_opt: Option<&'w u32>, } #[derive(WorldQuery)] #[world_query(derive(Debug))] struct GenericQuery<'w, T: Component, P: Component> { generic: (&'w T, &'w P), } #[derive(WorldQuery)] #[world_query(filter)] struct NumQueryFilter<T: Component, P: Component> { _u_16: With<u16>, _u_32: With<u32>, _or: Or<(With<i16>, Changed<u16>, Added<u32>)>, _generic_tuple: (With<T>, With<P>), _without: Without<Option<u16>>, _tp: PhantomData<(T, P)>, } fn print_nums_readonly(query: Query<NumQuery<u64, i64>, NumQueryFilter<u64, i64>>) { for num in query.iter() { println!("{:#?}", num); } } #[derive(WorldQuery)] #[world_query(mutable, derive(Debug))] struct MutNumQuery<'w, T: Component, P: Component> { i_16: &'w mut i16, i_32_opt: Option<&'w mut i32>, } fn print_nums(mut query: Query<MutNumQuery, NumQueryFilter<u64, i64>>) { for num in query.iter_mut() { println!("{:#?}", num); } } ``` ## TODOs: - [x] Add support for `&T` and `&mut T` - [x] Test - [x] Add support for optional types - [x] Test - [x] Add support for `Entity` - [x] Test - [x] Add support for nested `WorldQuery` - [x] Test - [x] Add support for tuples - [x] Test - [x] Add support for generics - [x] Test - [x] Add support for query filters - [x] Test - [x] Add support for `PhantomData` - [x] Test - [x] Refactor `read_world_query_field_type_info` - [x] Properly document `readonly` attribute for nested queries and the static assertions that guarantee safety - [x] Test that we never implement `ReadOnlyFetch` for types that need mutable access - [x] Test that we insert static assertions for nested `WorldQuery` that a user marked as readonly
What problem does this solve or what need does it fill?
Giant tuples within query (
Query<(&Foo, &Bar, &mut Baz)>
) is really annoying as then everything has to be referenced by.0
, etc, and if I decide to addEntity
to the list I have to shift everything.Describe the solution would you like?
Make a derive macro
#[derive(Query)]
which implementsHecsQuery
for the structure in question; as such the structure can be used as a query.Describe the alternative(s) you've considered?
None.
Additional context
This could also support
Or
by allowing deriving an enum.The text was updated successfully, but these errors were encountered: