mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
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!
This commit is contained in:
parent
382cd49c3b
commit
de92054bbe
1 changed files with 56 additions and 30 deletions
|
@ -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<PlayerEntity>) {
|
||||
/// 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<C: Command>(&mut self, command: C) {
|
||||
self.queue.push(command);
|
||||
|
|
Loading…
Reference in a new issue