@@ -15,14 +15,42 @@ 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::prelude::*;
25
+ /// use bevy::ecs::system::Command;
26
+ ///
27
+ /// // Our world resource
28
+ /// #[derive(Default)]
29
+ /// struct Counter(u64);
30
+ ///
31
+ /// // Our custom command
32
+ /// struct AddToCounter(u64);
33
+ ///
34
+ /// impl Command for AddToCounter {
35
+ /// fn write(self, world: &mut World) {
36
+ /// let mut counter = world.get_resource_or_insert_with(Counter::default);
37
+ /// counter.0 += self.0;
38
+ /// }
39
+ /// }
40
+ ///
41
+ /// fn some_system(mut commands: Commands) {
42
+ /// commands.add(AddToCounter(42));
43
+ /// }
44
+ /// ```
18
45
pub trait Command : Send + Sync + ' static {
19
46
fn write ( self , world : & mut World ) ;
20
47
}
21
48
22
- /// A list of commands that runs at the end of the stage of the system that called them.
49
+ /// A queue of [ commands](Command) that get executed at the end of the stage of the system that called them.
23
50
///
24
51
/// Commands are executed one at a time in an exclusive fashion.
25
- //// Each command can be used to modify the [`World`] in arbitrary ways:
52
+ ///
53
+ /// Each command can be used to modify the [`World`] in arbitrary ways:
26
54
/// * spawning or despawning entities
27
55
/// * inserting components on new or existing entities
28
56
/// * inserting resources
@@ -362,40 +390,40 @@ impl<'w, 's> Commands<'w, 's> {
362
390
} ) ;
363
391
}
364
392
365
- /// Adds a command directly to the command list.
393
+ /// Adds a command directly to the command queue.
394
+ ///
395
+ /// `command` can be a built-in command, custom struct that implements [`Command`] or a closure
396
+ /// that takes `&mut [World]` as an argument.
366
397
///
367
398
/// # Example
368
399
///
369
400
/// ```
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
- /// # }
401
+ /// use bevy::{ecs::system::Command, prelude::*};
387
402
///
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
- /// },
403
+ /// #[derive(Default)]
404
+ /// struct Counter(u64);
405
+ ///
406
+ /// struct AddToCounter(u64);
407
+ ///
408
+ /// impl Command for AddToCounter {
409
+ /// fn write(self, world: &mut World) {
410
+ /// let mut counter = world.get_resource_or_insert_with(Counter::default);
411
+ /// counter.0 += self.0;
412
+ /// }
413
+ /// }
414
+ ///
415
+ /// fn add_three_to_counter_system(mut commands: Commands) {
416
+ /// commands.add(AddToCounter(3));
417
+ /// }
418
+ /// fn add_twenty_five_to_counter_system(mut commands: Commands) {
419
+ /// commands.add(|world: &mut World| {
420
+ /// let mut counter = world.get_resource_or_insert_with(Counter::default);
421
+ /// counter.0 += 25;
396
422
/// });
397
423
/// }
398
- /// # bevy_ecs::system::assert_is_system(add_combat_stats_system);
424
+
425
+ /// # bevy_ecs::system::assert_is_system(add_three_to_counter_system);
426
+ /// # bevy_ecs::system::assert_is_system(add_twenty_five_to_counter_system);
399
427
/// ```
400
428
pub fn add < C : Command > ( & mut self , command : C ) {
401
429
self . queue . push ( command) ;
0 commit comments