bevy_ecs/system/commands/
command.rs1use crate::{
8 bundle::{Bundle, InsertMode, NoBundleEffect},
9 change_detection::MaybeLocation,
10 entity::Entity,
11 error::Result,
12 event::{Event, Events},
13 observer::TriggerTargets,
14 resource::Resource,
15 schedule::ScheduleLabel,
16 system::{IntoSystem, SystemId, SystemInput},
17 world::{FromWorld, SpawnBatchIter, World},
18};
19
20pub trait Command<Out = ()>: Send + 'static {
49 fn apply(self, world: &mut World) -> Out;
55}
56
57impl<F, Out> Command<Out> for F
58where
59 F: FnOnce(&mut World) -> Out + Send + 'static,
60{
61 fn apply(self, world: &mut World) -> Out {
62 self(world)
63 }
64}
65
66#[track_caller]
70pub fn spawn_batch<I>(bundles_iter: I) -> impl Command
71where
72 I: IntoIterator + Send + Sync + 'static,
73 I::Item: Bundle<Effect: NoBundleEffect>,
74{
75 let caller = MaybeLocation::caller();
76 move |world: &mut World| {
77 SpawnBatchIter::new(world, bundles_iter.into_iter(), caller);
78 }
79}
80
81#[track_caller]
88pub fn insert_batch<I, B>(batch: I, insert_mode: InsertMode) -> impl Command<Result>
89where
90 I: IntoIterator<Item = (Entity, B)> + Send + Sync + 'static,
91 B: Bundle<Effect: NoBundleEffect>,
92{
93 let caller = MaybeLocation::caller();
94 move |world: &mut World| -> Result {
95 world.try_insert_batch_with_caller(batch, insert_mode, caller)?;
96 Ok(())
97 }
98}
99
100#[track_caller]
103pub fn init_resource<R: Resource + FromWorld>() -> impl Command {
104 move |world: &mut World| {
105 world.init_resource::<R>();
106 }
107}
108
109#[track_caller]
111pub fn insert_resource<R: Resource>(resource: R) -> impl Command {
112 let caller = MaybeLocation::caller();
113 move |world: &mut World| {
114 world.insert_resource_with_caller(resource, caller);
115 }
116}
117
118pub fn remove_resource<R: Resource>() -> impl Command {
120 move |world: &mut World| {
121 world.remove_resource::<R>();
122 }
123}
124
125pub fn run_system<O: 'static>(id: SystemId<(), O>) -> impl Command<Result> {
127 move |world: &mut World| -> Result {
128 world.run_system(id)?;
129 Ok(())
130 }
131}
132
133pub fn run_system_with<I>(id: SystemId<I>, input: I::Inner<'static>) -> impl Command<Result>
136where
137 I: SystemInput<Inner<'static>: Send> + 'static,
138{
139 move |world: &mut World| -> Result {
140 world.run_system_with(id, input)?;
141 Ok(())
142 }
143}
144
145pub fn run_system_cached<M, S>(system: S) -> impl Command<Result>
148where
149 M: 'static,
150 S: IntoSystem<(), (), M> + Send + 'static,
151{
152 move |world: &mut World| -> Result {
153 world.run_system_cached(system)?;
154 Ok(())
155 }
156}
157
158pub fn run_system_cached_with<I, M, S>(system: S, input: I::Inner<'static>) -> impl Command<Result>
161where
162 I: SystemInput<Inner<'static>: Send> + Send + 'static,
163 M: 'static,
164 S: IntoSystem<I, (), M> + Send + 'static,
165{
166 move |world: &mut World| -> Result {
167 world.run_system_cached_with(system, input)?;
168 Ok(())
169 }
170}
171
172pub fn unregister_system<I, O>(system_id: SystemId<I, O>) -> impl Command<Result>
176where
177 I: SystemInput + Send + 'static,
178 O: Send + 'static,
179{
180 move |world: &mut World| -> Result {
181 world.unregister_system(system_id)?;
182 Ok(())
183 }
184}
185
186pub fn unregister_system_cached<I, O, M, S>(system: S) -> impl Command<Result>
191where
192 I: SystemInput + Send + 'static,
193 O: 'static,
194 M: 'static,
195 S: IntoSystem<I, O, M> + Send + 'static,
196{
197 move |world: &mut World| -> Result {
198 world.unregister_system_cached(system)?;
199 Ok(())
200 }
201}
202
203pub fn run_schedule(label: impl ScheduleLabel) -> impl Command<Result> {
205 move |world: &mut World| -> Result {
206 world.try_run_schedule(label)?;
207 Ok(())
208 }
209}
210
211#[track_caller]
213pub fn trigger(event: impl Event) -> impl Command {
214 let caller = MaybeLocation::caller();
215 move |world: &mut World| {
216 world.trigger_with_caller(event, caller);
217 }
218}
219
220pub fn trigger_targets(
222 event: impl Event,
223 targets: impl TriggerTargets + Send + Sync + 'static,
224) -> impl Command {
225 let caller = MaybeLocation::caller();
226 move |world: &mut World| {
227 world.trigger_targets_with_caller(event, targets, caller);
228 }
229}
230
231#[track_caller]
233pub fn send_event<E: Event>(event: E) -> impl Command {
234 let caller = MaybeLocation::caller();
235 move |world: &mut World| {
236 let mut events = world.resource_mut::<Events<E>>();
237 events.send_with_caller(event, caller);
238 }
239}