From f3f4a996cee2f40619f0267b8cd68b088530aa1e Mon Sep 17 00:00:00 2001 From: LoipesMas <46327403+LoipesMas@users.noreply.github.com> Date: Sat, 2 Jul 2022 09:49:20 +0000 Subject: [PATCH] Improve Command(s) docs (#4994) # Objective - Improve Command(s) docs - Fixes #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! --- crates/bevy_ecs/src/system/commands/mod.rs | 86 ++++++++++++++-------- 1 file changed, 56 insertions(+), 30 deletions(-) diff --git a/crates/bevy_ecs/src/system/commands/mod.rs b/crates/bevy_ecs/src/system/commands/mod.rs index aafaf45f276b0b..1c78d473c124b3 100644 --- a/crates/bevy_ecs/src/system/commands/mod.rs +++ b/crates/bevy_ecs/src/system/commands/mod.rs @@ -15,14 +15,41 @@ use std::marker::PhantomData; use super::Resource; /// A [`World`] mutation. +/// +/// Should be used with [`Commands::add`]. +/// +/// # Usage +/// +/// ``` +/// # use bevy_ecs::prelude::*; +/// # use bevy_ecs::system::Command; +/// // Our world resource +/// #[derive(Default)] +/// struct Counter(u64); +/// +/// // Our custom command +/// struct AddToCounter(u64); +/// +/// impl Command for AddToCounter { +/// fn write(self, world: &mut World) { +/// let mut counter = world.get_resource_or_insert_with(Counter::default); +/// counter.0 += self.0; +/// } +/// } +/// +/// fn some_system(mut commands: Commands) { +/// commands.add(AddToCounter(42)); +/// } +/// ``` pub trait Command: Send + Sync + 'static { fn write(self, world: &mut World); } -/// A list of commands that runs at the end of the stage of the system that called them. +/// A queue of [commands](Command) that get executed at the end of the stage of the system that called them. /// /// Commands are executed one at a time in an exclusive fashion. -//// Each command can be used to modify the [`World`] in arbitrary ways: +/// +/// Each command can be used to modify the [`World`] in arbitrary ways: /// * spawning or despawning entities /// * inserting components on new or existing entities /// * inserting resources @@ -72,7 +99,7 @@ impl<'w, 's> Commands<'w, 's> { /// # Example /// /// ``` - /// use bevy_ecs::prelude::*; + /// # use bevy_ecs::prelude::*; /// /// #[derive(Component)] /// struct Label(&'static str); @@ -362,40 +389,39 @@ impl<'w, 's> Commands<'w, 's> { }); } - /// Adds a command directly to the command list. + /// Adds a command directly to the command queue. + /// + /// `command` can be a built-in command, custom struct that implements [`Command`] or a closure + /// that takes [`&mut World`](World) as an argument. /// /// # Example /// /// ``` - /// # use bevy_ecs::prelude::*; - /// use bevy_ecs::system::InsertBundle; - /// # - /// # struct PlayerEntity { entity: Entity } - /// # #[derive(Component)] - /// # struct Health(u32); - /// # #[derive(Component)] - /// # struct Strength(u32); - /// # #[derive(Component)] - /// # struct Defense(u32); - /// # - /// # #[derive(Bundle)] - /// # struct CombatBundle { - /// # health: Health, - /// # strength: Strength, - /// # defense: Defense, - /// # } + /// # use bevy_ecs::{system::Command, prelude::*}; + /// #[derive(Default)] + /// struct Counter(u64); /// - /// fn add_combat_stats_system(mut commands: Commands, player: Res) { - /// commands.add(InsertBundle { - /// entity: player.entity, - /// bundle: CombatBundle { - /// health: Health(100), - /// strength: Strength(40), - /// defense: Defense(20), - /// }, + /// struct AddToCounter(u64); + /// + /// impl Command for AddToCounter { + /// fn write(self, world: &mut World) { + /// let mut counter = world.get_resource_or_insert_with(Counter::default); + /// counter.0 += self.0; + /// } + /// } + /// + /// fn add_three_to_counter_system(mut commands: Commands) { + /// commands.add(AddToCounter(3)); + /// } + /// fn add_twenty_five_to_counter_system(mut commands: Commands) { + /// commands.add(|world: &mut World| { + /// let mut counter = world.get_resource_or_insert_with(Counter::default); + /// counter.0 += 25; /// }); /// } - /// # bevy_ecs::system::assert_is_system(add_combat_stats_system); + + /// # bevy_ecs::system::assert_is_system(add_three_to_counter_system); + /// # bevy_ecs::system::assert_is_system(add_twenty_five_to_counter_system); /// ``` pub fn add(&mut self, command: C) { self.queue.push(command);