#Generic system with extra arguments

9 messages · Page 1 of 1 (latest)

rocky hinge
#

Hi,
I'd like to create a generic system for my dying animation component.
Something like

pub fn add_death_animation_helper<T: ReadOnlyWorldQuery>(
    commands: &mut Commands,
    dying_query: Query<Entity, (Added<Dying>, T)>,
    animation_time_millis: f32,
) {
    for entity in dying_query.iter() {
        if let Some(mut entity) = commands.get_entity(entity) {
            entity.insert(Animation { timer: Timer::new(
                Duration::from_millis(animation_time_millis as u64),
                TimerMode::Once
            ) });
        }
    }
}

My plan was then to use it like:

app.add_system(add_death_animation_helper<With<Player>>);
app.add_system(add_death_animation_helper<With<Food>>);

But as you can see there is an extra argument ( animation_time_millis) that is blocking me. Any ideas on how I could still use a generic system pattern?

sweet adder
#

use a const generic

rocky hinge
#

I read the value from a config file so i cannot use const generics

sweet adder
#

probably use pipe systems?

app.add_system(read_animation_time.pipe(add_death_animation_helper<With<Food>>));

then do In(animation_time_millis): In<f32> in your animation helper system

#

also, if the value is from a config file, then probably put the config file as a resource, then access it using Res<Config> or something

rocky hinge
#

Yes I'm accessing it with Res<Settings>, but I don't know how to add my systems while specifying the keys that I want to use. Using pipes is a good idea, I can try that. It's still not ideal ergonomics-wise but it's better than what i had before. Thanks

sweet adder
#

but I don't know how to add my systems while specifying the keys that I want to use
can you elaborate on this?

#

is your Settings a HashMap?

rocky hinge