Skip to content

Commit f3f4a99

Browse files
LoipesMasItsDoot
authored andcommitted
Improve Command(s) docs (bevyengine#4994)
# Objective - Improve Command(s) docs - Fixes bevyengine#4737 ## Solution - Update and improve documentation. --- - "list" -> "queue" in `Commands` doc (this better represents reality) - expand `Command` doc - update/improve `Commands::add` doc, as specified in linked issue Let me know if you want any changes!
1 parent 3bd0e17 commit f3f4a99

File tree

1 file changed

+56
-30
lines changed
  • crates/bevy_ecs/src/system/commands

1 file changed

+56
-30
lines changed

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

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,41 @@ 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_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+
/// ```
1844
pub trait Command: Send + Sync + 'static {
1945
fn write(self, world: &mut World);
2046
}
2147

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.
2349
///
2450
/// 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:
2653
/// * spawning or despawning entities
2754
/// * inserting components on new or existing entities
2855
/// * inserting resources
@@ -72,7 +99,7 @@ impl<'w, 's> Commands<'w, 's> {
7299
/// # Example
73100
///
74101
/// ```
75-
/// use bevy_ecs::prelude::*;
102+
/// # use bevy_ecs::prelude::*;
76103
///
77104
/// #[derive(Component)]
78105
/// struct Label(&'static str);
@@ -362,40 +389,39 @@ impl<'w, 's> Commands<'w, 's> {
362389
});
363390
}
364391

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.
366396
///
367397
/// # Example
368398
///
369399
/// ```
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);
387403
///
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;
396420
/// });
397421
/// }
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);
399425
/// ```
400426
pub fn add<C: Command>(&mut self, command: C) {
401427
self.queue.push(command);

0 commit comments

Comments
 (0)