@@ -299,26 +299,55 @@ impl World {
299
299
}
300
300
}
301
301
302
- /// The [`Command`] type for [`World::run_system`].
302
+ /// The [`Command`] type for [`World::run_system`] or [`World::run_system_with_input`] .
303
303
///
304
304
/// This command runs systems in an exclusive and single threaded way.
305
305
/// Running slow systems can become a bottleneck.
306
+ ///
307
+ /// If the system needs an [`In<_>`](crate::system::In) input value to run, it must
308
+ /// be provided as part of the command.
309
+ ///
310
+ /// There is no way to get the output of a system when run as a command, because the
311
+ /// execution of the system happens later. To get the output of a system, use
312
+ /// [World::run_system] or [World::run_system_with_input] instead of running the system as a command.
306
313
#[ derive( Debug , Clone ) ]
307
- pub struct RunSystem {
308
- system_id : SystemId ,
314
+ pub struct RunSystemWithInput < I : ' static > {
315
+ system_id : SystemId < I > ,
316
+ input : I ,
309
317
}
310
318
319
+ /// The [`Command`] type for [`World::run_system`].
320
+ ///
321
+ /// This command runs systems in an exclusive and single threaded way.
322
+ /// Running slow systems can become a bottleneck.
323
+ ///
324
+ /// If the system needs an [`In<_>`](crate::system::In) input value to run, use the
325
+ /// [crate::system::RunSystemWithInput] type instead.
326
+ ///
327
+ /// There is no way to get the output of a system when run as a command, because the
328
+ /// execution of the system happens later. To get the output of a system, use
329
+ /// [World::run_system] or [World::run_system_with_input] instead of running the system as a command.
330
+ pub type RunSystem = RunSystemWithInput < ( ) > ;
331
+
311
332
impl RunSystem {
312
333
/// Creates a new [`Command`] struct, which can be added to [`Commands`](crate::system::Commands)
313
334
pub fn new ( system_id : SystemId ) -> Self {
314
- Self { system_id }
335
+ Self :: new_with_input ( system_id, ( ) )
336
+ }
337
+ }
338
+
339
+ impl < I : ' static > RunSystemWithInput < I > {
340
+ /// Creates a new [`Command`] struct, which can be added to [`Commands`](crate::system::Commands)
341
+ /// in order to run the specified system with the provided [`In<_>`](crate::system::In) input value.
342
+ pub fn new_with_input ( system_id : SystemId < I > , input : I ) -> Self {
343
+ Self { system_id, input }
315
344
}
316
345
}
317
346
318
- impl Command for RunSystem {
347
+ impl < I : ' static + Send > Command for RunSystemWithInput < I > {
319
348
#[ inline]
320
349
fn apply ( self , world : & mut World ) {
321
- let _ = world. run_system ( self . system_id ) ;
350
+ let _ = world. run_system_with_input ( self . system_id , self . input ) ;
322
351
}
323
352
}
324
353
@@ -505,4 +534,32 @@ mod tests {
505
534
let _ = world. run_system ( nested_id) ;
506
535
assert_eq ! ( * world. resource:: <Counter >( ) , Counter ( 5 ) ) ;
507
536
}
537
+
538
+ #[ test]
539
+ fn nested_systems_with_inputs ( ) {
540
+ use crate :: system:: SystemId ;
541
+
542
+ #[ derive( Component ) ]
543
+ struct Callback ( SystemId < u8 > , u8 ) ;
544
+
545
+ fn nested ( query : Query < & Callback > , mut commands : Commands ) {
546
+ for callback in query. iter ( ) {
547
+ commands. run_system_with_input ( callback. 0 , callback. 1 ) ;
548
+ }
549
+ }
550
+
551
+ let mut world = World :: new ( ) ;
552
+ world. insert_resource ( Counter ( 0 ) ) ;
553
+
554
+ let increment_by =
555
+ world. register_system ( |In ( amt) : In < u8 > , mut counter : ResMut < Counter > | {
556
+ counter. 0 += amt;
557
+ } ) ;
558
+ let nested_id = world. register_system ( nested) ;
559
+
560
+ world. spawn ( Callback ( increment_by, 2 ) ) ;
561
+ world. spawn ( Callback ( increment_by, 3 ) ) ;
562
+ let _ = world. run_system ( nested_id) ;
563
+ assert_eq ! ( * world. resource:: <Counter >( ) , Counter ( 5 ) ) ;
564
+ }
508
565
}
0 commit comments