#Schedule docs & clarification

6 messages ยท Page 1 of 1 (latest)

faint pelican
#

Hey there,
I'm working with Bevy's SystemSets and Schedules and need clarification on the "new" Schedule implementation (0.10+). Specifically, I'm unsure where schedules added via .add_schedule() are placed by default. How can I insert them at a specific position in the execution order?

Additionally, I'm seeking best practices for using Schedules in Bevy. Here's the relevant code for context:

pub struct TrackPlugin;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemSet)]
enum TrackSet {
    Extract,
    Queue,
}

#[derive(ScheduleLabel, Debug, Hash, PartialEq, Eq, Clone)]
pub struct TrackSchedule;

impl TrackSchedule {
    pub fn base_schedule() -> Schedule {
        use TrackSet::*;

        let mut schedule = Schedule::new(Self);

        schedule.configure_sets((Extract, Queue).chain());

        return schedule;
    }
}

// TODO: Could it run in a separate sub app parallel to the core and rendering?
impl Plugin for TrackPlugin {
    fn build(&self, app: &mut App) {
        // Register resources
        app.init_resource::<TrackedEntities>();
        app.init_resource::<ChangedComponents>();

        // Register schedules
        app.add_schedule(TrackSchedule::base_schedule());

        // Register systems
        app.add_systems(
            TrackSchedule,
            (
                track_changes::<DimensionMixin>.in_set(TrackSet::Extract),
                track_changes::<RelativeTransformMixin>.in_set(TrackSet::Extract),
                queue_tracked_changes
                    .in_set(TrackSet::Queue)
                    .after(TrackSet::Extract),
            ),
        );
    }
}

Thanks ๐Ÿ™‚

fallen breach
#

you can think of schedules as a FnMut(&mut World) that you can call when given a &mut World and the name of the schedule.

what bevy does is basically

|w: &mut World| {loop {
  ...
  pre_update(&mut w)
  update(&mut w)
  post_update(&mut w)
  ...
}}

there is no way to add a schedule into this but you can run a schedule at any point when you have access to a &mut World, you can do this via Commands or a system with a &mut World input.

What you might want instead is organizing your systems with SystemSet and use apply_deferred to apply commands during a schedule if needed.

faint pelican
# fallen breach you can think of schedules as a `FnMut(&mut World)` that you can call when given...

thanks @fallen breach I guess SystemSets are fitting for my use case ๐Ÿ‘

Do you know whether I can like group register systems to SystemSets or do I've to define the SystemSet via in_set() on each system separately and define when it should run?

  app.add_systems(
            PreUpdate,
            (
                handle_cursor_entered_composition.in_set(InteractionSet::First),
                handle_cursor_down_on_composition
                    .in_set(InteractionSet::Initial)
                    .after(InteractionSet::First),
                handle_cursor_down_on_entity_event
                    .in_set(InteractionSet::Initial)
                    .after(handle_cursor_down_on_composition)
                    .after(InteractionSet::First),
                handle_cursor_moved_on_composition
                    .in_set(InteractionSet::Continuous)
                    .after(InteractionSet::Initial),
                handle_cursor_up_on_composition
                    .in_set(InteractionSet::Completion)
                    .after(InteractionSet::Continuous),
                handle_cursor_exited_composition
                    .in_set(InteractionSet::Last)
                    .after(InteractionSet::Completion),
            ),
        );

Thanks ๐Ÿ™‚

fallen breach
faint pelican
#

@fallen breach Seems to work ๐Ÿ‘ What will happen though if I define a different schedule when registering the systems (.add_systems()) than when configuring the sets (.configure_sets())? Thanks ๐Ÿ™‚

 // Configure system sets
        app.configure_sets(
            PreUpdate, // <- 
            (
                InteractionSet::First,
                InteractionSet::Initial,
                InteractionSet::Continuous,
                InteractionSet::Completion,
                InteractionSet::Last,
            )
                .chain(),
        );

        // Register systems
        app.add_systems(
            PreUpdate, // <-
            (
                handle_cursor_entered_composition.in_set(InteractionSet::First),
                handle_cursor_down_on_composition.in_set(InteractionSet::Initial),
                handle_cursor_down_on_entity_event
                    .in_set(InteractionSet::Initial)
                    .after(handle_cursor_down_on_composition),
                handle_cursor_moved_on_composition.in_set(InteractionSet::Continuous),
                handle_cursor_up_on_composition.in_set(InteractionSet::Completion),
                handle_cursor_exited_composition.in_set(InteractionSet::Last),
            ),
        );