diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index 1480c200ab..289e37e3b4 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -14,6 +14,7 @@ use crate::{ entity::{Entities, Entity}, event::{Event, SendEvent}, observer::{Observer, TriggerEvent, TriggerTargets}, + schedule::ScheduleLabel, system::{input::SystemInput, RunSystemWithInput, SystemId}, world::{ command_queue::RawCommandQueue, unsafe_world_cell::UnsafeWorldCell, Command, CommandQueue, @@ -973,6 +974,53 @@ impl<'w, 's> Commands<'w, 's> { self.queue(SendEvent { event }); self } + + /// Runs the schedule corresponding to the given [`ScheduleLabel`]. + /// + /// Calls [`World::try_run_schedule`](World::try_run_schedule). + /// + /// This will log an error if the schedule is not available to be run. + /// + /// # Examples + /// + /// ``` + /// # use bevy_ecs::prelude::*; + /// # use bevy_ecs::schedule::ScheduleLabel; + /// # + /// # #[derive(Default, Resource)] + /// # struct Counter(u32); + /// # + /// #[derive(ScheduleLabel, Hash, Debug, PartialEq, Eq, Clone, Copy)] + /// struct FooSchedule; + /// + /// # fn foo_system(mut counter: ResMut) { + /// # counter.0 += 1; + /// # } + /// # + /// # let mut schedule = Schedule::new(FooSchedule); + /// # schedule.add_systems(foo_system); + /// # + /// # let mut world = World::default(); + /// # + /// # world.init_resource::(); + /// # world.add_schedule(schedule); + /// # + /// # assert_eq!(world.resource::().0, 0); + /// # + /// # let mut commands = world.commands(); + /// commands.run_schedule(FooSchedule); + /// # + /// # world.flush(); + /// # + /// # assert_eq!(world.resource::().0, 1); + /// ``` + pub fn run_schedule(&mut self, label: impl ScheduleLabel) { + self.queue(|world: &mut World| { + if let Err(error) = world.try_run_schedule(label) { + panic!("Failed to run schedule: {error}"); + } + }); + } } /// A [`Command`] which gets executed for a given [`Entity`].