@@ -15,14 +15,41 @@ use std::marker::PhantomData;
15
15
use super :: Resource ;
16
16
17
17
/// A [`World`] mutation.
18
+ ///
19
+ /// Should be used with [`Commands::add`].
20
+ ///
21
+ /// # Usage
22
+ ///
23
+ /// ```
24
+ /// # use bevy_ecs::prelude::*;
25
+ /// # use bevy_ecs::system::Command;
26
+ /// // Our world resource
27
+ /// #[derive(Default)]
28
+ /// struct Counter(u64);
29
+ ///
30
+ /// // Our custom command
31
+ /// struct AddToCounter(u64);
32
+ ///
33
+ /// impl Command for AddToCounter {
34
+ /// fn write(self, world: &mut World) {
35
+ /// let mut counter = world.get_resource_or_insert_with(Counter::default);
36
+ /// counter.0 += self.0;
37
+ /// }
38
+ /// }
39
+ ///
40
+ /// fn some_system(mut commands: Commands) {
41
+ /// commands.add(AddToCounter(42));
42
+ /// }
43
+ /// ```
18
44
pub trait Command : Send + Sync + ' static {
19
45
fn write ( self , world : & mut World ) ;
20
46
}
21
47
22
- /// A list of commands that runs at the end of the stage of the system that called them.
48
+ /// A queue of [ commands](Command) that get executed at the end of the stage of the system that called them.
23
49
///
24
50
/// Commands are executed one at a time in an exclusive fashion.
25
- //// Each command can be used to modify the [`World`] in arbitrary ways:
51
+ ///
52
+ /// Each command can be used to modify the [`World`] in arbitrary ways:
26
53
/// * spawning or despawning entities
27
54
/// * inserting components on new or existing entities
28
55
/// * inserting resources
@@ -72,7 +99,7 @@ impl<'w, 's> Commands<'w, 's> {
72
99
/// # Example
73
100
///
74
101
/// ```
75
- /// use bevy_ecs::prelude::*;
102
+ /// # use bevy_ecs::prelude::*;
76
103
///
77
104
/// #[derive(Component)]
78
105
/// struct Label(&'static str);
@@ -362,40 +389,39 @@ impl<'w, 's> Commands<'w, 's> {
362
389
} ) ;
363
390
}
364
391
365
- /// Adds a command directly to the command list.
392
+ /// Adds a command directly to the command queue.
393
+ ///
394
+ /// `command` can be a built-in command, custom struct that implements [`Command`] or a closure
395
+ /// that takes [`&mut World`](World) as an argument.
366
396
///
367
397
/// # Example
368
398
///
369
399
/// ```
370
- /// # use bevy_ecs::prelude::*;
371
- /// use bevy_ecs::system::InsertBundle;
372
- /// #
373
- /// # struct PlayerEntity { entity: Entity }
374
- /// # #[derive(Component)]
375
- /// # struct Health(u32);
376
- /// # #[derive(Component)]
377
- /// # struct Strength(u32);
378
- /// # #[derive(Component)]
379
- /// # struct Defense(u32);
380
- /// #
381
- /// # #[derive(Bundle)]
382
- /// # struct CombatBundle {
383
- /// # health: Health,
384
- /// # strength: Strength,
385
- /// # defense: Defense,
386
- /// # }
400
+ /// # use bevy_ecs::{system::Command, prelude::*};
401
+ /// #[derive(Default)]
402
+ /// struct Counter(u64);
387
403
///
388
- /// fn add_combat_stats_system(mut commands: Commands, player: Res<PlayerEntity>) {
389
- /// commands.add(InsertBundle {
390
- /// entity: player.entity,
391
- /// bundle: CombatBundle {
392
- /// health: Health(100),
393
- /// strength: Strength(40),
394
- /// defense: Defense(20),
395
- /// },
404
+ /// struct AddToCounter(u64);
405
+ ///
406
+ /// impl Command for AddToCounter {
407
+ /// fn write(self, world: &mut World) {
408
+ /// let mut counter = world.get_resource_or_insert_with(Counter::default);
409
+ /// counter.0 += self.0;
410
+ /// }
411
+ /// }
412
+ ///
413
+ /// fn add_three_to_counter_system(mut commands: Commands) {
414
+ /// commands.add(AddToCounter(3));
415
+ /// }
416
+ /// fn add_twenty_five_to_counter_system(mut commands: Commands) {
417
+ /// commands.add(|world: &mut World| {
418
+ /// let mut counter = world.get_resource_or_insert_with(Counter::default);
419
+ /// counter.0 += 25;
396
420
/// });
397
421
/// }
398
- /// # bevy_ecs::system::assert_is_system(add_combat_stats_system);
422
+
423
+ /// # bevy_ecs::system::assert_is_system(add_three_to_counter_system);
424
+ /// # bevy_ecs::system::assert_is_system(add_twenty_five_to_counter_system);
399
425
/// ```
400
426
pub fn add < C : Command > ( & mut self , command : C ) {
401
427
self . queue . push ( command) ;
0 commit comments