Skip to content

Commit 12ae995

Browse files
committed
Improve Command(s) docs
1 parent 57b4620 commit 12ae995

File tree

1 file changed

+57
-29
lines changed
  • crates/bevy_ecs/src/system/commands

1 file changed

+57
-29
lines changed

crates/bevy_ecs/src/system/commands/mod.rs

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,42 @@ use std::marker::PhantomData;
1515
use super::Resource;
1616

1717
/// 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+
/// ```
1845
pub trait Command: Send + Sync + 'static {
1946
fn write(self, world: &mut World);
2047
}
2148

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.
2350
///
2451
/// 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:
2654
/// * spawning or despawning entities
2755
/// * inserting components on new or existing entities
2856
/// * inserting resources
@@ -362,40 +390,40 @@ impl<'w, 's> Commands<'w, 's> {
362390
});
363391
}
364392

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.
366397
///
367398
/// # Example
368399
///
369400
/// ```
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::*};
387402
///
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;
396422
/// });
397423
/// }
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);
399427
/// ```
400428
pub fn add<C: Command>(&mut self, command: C) {
401429
self.queue.push(command);

0 commit comments

Comments
 (0)