#Generic Component

8 messages · Page 1 of 1 (latest)

timid loom
#

I had an idea to add a component to a button from bevy ui to automaticaly emit event on hover and click. But for this to be effective i need the component to be generic like so: ```rust
#[derive(Component)]
pub struct OnHover<E: Event>(E)

but when i try to create the query in my system like so:
```rust
fn button_hover_system<E: Event>(
    mut query: Query<
        (&Interaction, OnHover<E>),
        (Changed<Interaction>, With<Button>),
    >,
    mut event_writer: EventWriter<E>,
) {
    for (interaction, hover) in &mut query {
        match interaction {
            Interaction::Hovered => event_writer.send(hover.0),
            _ => (),
        }
    }
}

I get this error:

error[E0277]: the trait bound `OnHover<E>: WorldQuery` is not satisfied
   --> beditor/src/widget/mod.rs:11:16
    |
11  |       mut query: Query<
    |  ________________^
12  | |         (&Interaction, OnHover<E>),
13  | |         (Changed<Interaction>, With<Button>),
14  | |     >,
    | |_____^ the trait `WorldQuery` is not implemented for `OnHover<E>`
    |
    = help: the following other types implement trait `WorldQuery`:
              &'__w mut T
              &T
              ()
              (F0, F1)
              (F0, F1, F2)
              (F0, F1, F2, F3)
              (F0, F1, F2, F3, F4)
              (F0, F1, F2, F3, F4, F5)
            and 54 others
    = note: required for `(&Interaction, OnHover<E>)` to implement `WorldQuery`
note: required by a bound in `bevy::prelude::Query`
   --> /home/adml/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_ecs-0.10.0/src/system/query.rs:276:37
    |
276 | pub struct Query<'world, 'state, Q: WorldQuery, F: ReadOnlyWorldQuery = ()> {
    |                                     ^^^^^^^^^^ required by this bound in `bevy::prelude::Query`

Is that not possible right now ? Will it be in the future ? or am i doing something wrong ?

gusty aspen
#

Yes, need to add a Sync + Send + 'static bound to the generic type parameter

#

try this:

fn button_hover_system<E: Event + Send + Sync + 'static>(
#

Honestly I thought it was already fixed

#

I'm surprised you are encoutering this issue

timid loom
#

oh nice thx

#

now lets see if it works ^^

#

nope i still get the error