Skip to content

Add support for traits in Query filters #50

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

Closed
dbidwell94 opened this issue Dec 12, 2023 · 3 comments
Closed

Add support for traits in Query filters #50

dbidwell94 opened this issue Dec 12, 2023 · 3 comments

Comments

@dbidwell94
Copy link

In my particular use case, I would like the ability to be able to use traits in a With<> or Without<> filter in the query.

Example:

pub fn cleanup_status(
    mut commands: Commands,
    q: Query<
        Entity,
        (
            With<PawnStatus<pawn_status::Attacking>>,
            Without<dyn work_order::OrderItem>,
        ),
    >,
) {
}

The above return a compile error because

  • the size for values of type (dyn OrderItem + 'static) cannot be known at compilation time the trait Sized is not implemented for (dyn OrderItem + 'static)
  • the trait bound (dyn OrderItem + 'static): bevy::prelude::Component is not satisfied
    the following other types implement trait bevy::prelude::Component

The definition for OrderItem is as such:

    #[bevy_trait_query::queryable]
    pub trait OrderItem {}
@brandon-reinhart
Copy link

I would also love to be able to do this!

@RobWalt
Copy link
Collaborator

RobWalt commented May 11, 2024

The desired behavior should be achievable with the results from #55 and #58 . Instead of using the native With and Without filters, you should be able to use WithOne and WithoutOne to get the same result. If I'm missing a case, let me know!

RobWalt added a commit that referenced this issue Oct 28, 2024
solves #50 

feat: Implement `WithoutAny` filter

This PR:

1. implements `WithoutAny`, a `QueryFilter` which is supposed to be the opposite of `WithOne`
2. cleans up some parts of `WithOne` to make it `QueryFilter` only

Note that 2. is a breaking change since we can't use it in the Data position anymore. However, I think this is fine since there is `One` which already fills this gap. So this is really a fix since it clears up the separation of concerns of the two structs.

---

Small pseudo code example:

```rust
struct Food;

trait Fruit {}
struct Banana;
impl Fruit for Banana {}
struct Apple;
impl Fruit for Apple {}

struct Sweets;
struct Cake;

fn eat_unhealthy(
  mut commands: Commands,
  q_non_fruits: Query<Entity, (With<Food>, WithoutAny<&dyn Fruit>)>
) {
  q_non_fruits.iter().for_each(|food| {
    // only sweets and cakes without fruits
    commands.eat(food); 
  });
}  
```
@RobWalt
Copy link
Collaborator

RobWalt commented Nov 2, 2024

This feature is now supported as of bevy-trait-query v0.6.0!

Please see WithoutAny for details

@RobWalt RobWalt closed this as completed Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants