#Generic Event Registration

39 messages · Page 1 of 1 (latest)

dull galleon
#

I have the following struct:

pub struct FullTimerFireRequest<T: FullTimerFireRequestType> {
    pub affecting_timer_set_policy: AffectingTimerSetPolicy,
    pub timer_to_fire: T,
}```

The following lines throw an error as I don't specify the type:
```impl Plugin for FullTimerFireRequestPlugin {
    fn build(&self, app: &mut App) {
        app.add_event::<FullTimerFireRequest>();
    }
}```

Does that mean that in order to implement the event for all types that implement `FullTimerFireRequestType` I'd have to add a separate event for each one of them?
dull galleon
#

I was thinking of

  1. Have a specific plugin that'll register all events for a specific type
  2. Make a "type iterator" though I couldn't make it work
barren hill
#

A plugin would definitely work, just make it generic and then you can just add the plugin and it adds the event and whatever else you need for you :)

#

@dull galleon ping for visibility

dull galleon
#

Interesting

barren hill
#

the Phantomdata is only needed if you won't use an instance of T

dull galleon
#

So I actually used that but I'm having problem registring that plugin within other plugins

#

not sure what's wrong tbh, it says I should try to add a value and not an object but I'm just feeding it the T the bigger function gets

barren hill
#

the first thing i can think of is that SendableTimerFireRequestType doesn't have the same trait requirements (or whatever they're called) as the one that FullTimerFireRequestPlugin needs

dull galleon
barren hill
dull galleon
#

true, turning on the computer now so I can copy it

barren hill
#

do you have Default implemented?

#

try calling ::default

#

when adding the plugin

dull galleon
#

I don't have default for the plugin

barren hill
#

the problem is that the plugin has a field, so you need to construct an instance of it

#

currently you're just giving it the struct itself

#

so either implement Default, or implement your own functions like new, or just construct it

dull galleon
#

The error remains

barren hill
#

and then do

app.add_plugins(FulltimerFireRequestPlugin::<T>::default());
dull galleon
#

oh I see

#

Well, now it says that T has to implement default which would mean making sure each variant always has a default value

#

(thinking)

#

maybe I'll just make the phantom an Option

#

That
did not work

#

new() time

barren hill
#
#[derive(Event)]
struct MyEvent(f32, i32, String);

struct MyPlugin;

impl Plugin for MyPlugin {
    fn build(&self, app: &mut App) {
        app.add_plugins(EventRegistrator::<MyEvent>::default());
    }
}

struct EventRegistrator<T: Event>(std::marker::PhantomData<T>);

impl<T: Event> Plugin for EventRegistrator<T> {
    fn build(&self, app: &mut App) {
        app.add_event::<T>();
    }
}

impl<T: Event> Default for EventRegistrator<T> {
    fn default() -> Self {
        Self(Default::default())
    }
}
#

this works for me

#

and MyEvent is not implementing default

dull galleon
#

yea my bad I should have implemented Default by hand rather than just add the derive one

#

I wonder if I should make a macro for that

#

because the entire phantom and default boilerplate isn't really important

#

well, that should do