Skip to content

Commit 36cd46c

Browse files
NiliradItsDoot
authored andcommitted
Fix API docs for Commands methods (bevyengine#5955)
# Objective The doc comments for `Command` methods are a bit inconsistent on the format, they sometimes go out of scope, and most importantly they are wrong, in the sense that they claim to perform the action described by the command, while in reality, they just push a command to perform the action. - Follow-up of bevyengine#5938. - Related to bevyengine#5913. ## Solution - Where applicable, only stated that a `Command` is pushed. - Added a “See also” section for similar methods. - Added a missing “Panics” section for `Commands::entity`. - Removed a wrong comment about `Commands::get_or_spawn` returning `None` (It does not return an option). - Removed polluting descriptions of other items. - Misc formatting changes. ## Future possibilities Since the `Command` implementors (`Spawn`, `InsertBundle`, `InitResource`, ...) are public, I thought that it might be appropriate to describe the action of the command there instead of the method, and to add a `method → command struct` link to fill the gap. If that seems too far-fetched, we may opt to make them private, if possible, or `#[doc(hidden)]`.
1 parent cf01522 commit 36cd46c

File tree

1 file changed

+84
-41
lines changed
  • crates/bevy_ecs/src/system/commands

1 file changed

+84
-41
lines changed

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

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,29 @@ pub struct Commands<'w, 's> {
102102
}
103103

104104
impl<'w, 's> Commands<'w, 's> {
105-
/// Create a new `Commands` from a queue and a world.
105+
/// Returns a new `Commands` instance from a [`CommandQueue`] and a [`World`].
106+
///
107+
/// It is not required to call this constructor when using `Commands` as a [system parameter].
108+
///
109+
/// [system parameter]: crate::system::SystemParam
106110
pub fn new(queue: &'s mut CommandQueue, world: &'w World) -> Self {
107111
Self {
108112
queue,
109113
entities: world.entities(),
110114
}
111115
}
112116

113-
/// Create a new `Commands` from a queue and an [`Entities`] reference.
117+
/// Returns a new `Commands` instance from a [`CommandQueue`] and an [`Entities`] reference.
118+
///
119+
/// It is not required to call this constructor when using `Commands` as a [system parameter].
120+
///
121+
/// [system parameter]: crate::system::SystemParam
114122
pub fn new_from_entities(queue: &'s mut CommandQueue, entities: &'w Entities) -> Self {
115123
Self { queue, entities }
116124
}
117125

118-
/// Creates a new empty [`Entity`] and returns an [`EntityCommands`] builder for it.
119-
///
120-
/// To directly spawn an entity with a [`Bundle`] included, you can use
121-
/// [`spawn_bundle`](Self::spawn_bundle) instead of `.spawn().insert_bundle()`.
126+
/// Pushes a [`Command`] to the queue for creating a new empty [`Entity`],
127+
/// and returns its corresponding [`EntityCommands`].
122128
///
123129
/// See [`World::spawn`] for more details.
124130
///
@@ -147,6 +153,11 @@ impl<'w, 's> Commands<'w, 's> {
147153
/// }
148154
/// # bevy_ecs::system::assert_is_system(example_system);
149155
/// ```
156+
///
157+
/// # See also
158+
///
159+
/// - [`spawn_bundle`](Self::spawn_bundle) to spawn an entity with a bundle.
160+
/// - [`spawn_batch`](Self::spawn_batch) to spawn entities with a bundle each.
150161
pub fn spawn<'a>(&'a mut self) -> EntityCommands<'w, 's, 'a> {
151162
let entity = self.entities.reserve_entity();
152163
EntityCommands {
@@ -155,10 +166,13 @@ impl<'w, 's> Commands<'w, 's> {
155166
}
156167
}
157168

158-
/// Returns an [`EntityCommands`] for the given `entity` (if it exists) or spawns one if it
159-
/// doesn't exist. This will return [`None`] if the `entity` exists with a different generation.
169+
/// Pushes a [`Command`] to the queue for creating a new [`Entity`] if the given one does not exists,
170+
/// and returns its corresponding [`EntityCommands`].
171+
///
172+
/// See [`World::get_or_spawn`] for more details.
160173
///
161174
/// # Note
175+
///
162176
/// Spawning a specific `entity` value is rarely the right choice. Most apps should favor
163177
/// [`Commands::spawn`]. This method should generally only be used for sharing entities across
164178
/// apps, and only when they have a scheme worked out to share an ID space (which doesn't happen
@@ -171,14 +185,8 @@ impl<'w, 's> Commands<'w, 's> {
171185
}
172186
}
173187

174-
/// Creates a new entity with the components contained in `bundle`.
175-
///
176-
/// This returns an [`EntityCommands`] builder, which enables inserting more components and
177-
/// bundles using a "builder pattern".
178-
///
179-
/// Note that `bundle` is a [`Bundle`], which is a collection of components. [`Bundle`] is
180-
/// automatically implemented for tuples of components. You can also create your own bundle
181-
/// types by deriving [`derive@Bundle`].
188+
/// Pushes a [`Command`] to the queue for creating a new entity with the given [`Bundle`]'s components,
189+
/// and returns its corresponding [`EntityCommands`].
182190
///
183191
/// # Example
184192
///
@@ -219,13 +227,22 @@ impl<'w, 's> Commands<'w, 's> {
219227
/// }
220228
/// # bevy_ecs::system::assert_is_system(example_system);
221229
/// ```
230+
///
231+
/// # See also
232+
///
233+
/// - [`spawn`](Self::spawn) to just spawn an entity without any component.
234+
/// - [`spawn_batch`](Self::spawn_batch) to spawn entities with a bundle each.
222235
pub fn spawn_bundle<'a, T: Bundle>(&'a mut self, bundle: T) -> EntityCommands<'w, 's, 'a> {
223236
let mut e = self.spawn();
224237
e.insert_bundle(bundle);
225238
e
226239
}
227240

228-
/// Returns an [`EntityCommands`] builder for the requested [`Entity`].
241+
/// Returns the [`EntityCommands`] for the requested [`Entity`].
242+
///
243+
/// # Panics
244+
///
245+
/// This method panics if the requested entity does not exist.
229246
///
230247
/// # Example
231248
///
@@ -238,7 +255,7 @@ impl<'w, 's> Commands<'w, 's> {
238255
/// struct Strength(u32);
239256
/// #[derive(Component)]
240257
/// struct Agility(u32);
241-
258+
///
242259
/// fn example_system(mut commands: Commands) {
243260
/// // Create a new, empty entity
244261
/// let entity = commands.spawn().id();
@@ -251,6 +268,10 @@ impl<'w, 's> Commands<'w, 's> {
251268
/// }
252269
/// # bevy_ecs::system::assert_is_system(example_system);
253270
/// ```
271+
///
272+
/// # See also
273+
///
274+
/// - [`get_entity`](Self::get_entity) for the fallible version.
254275
#[inline]
255276
#[track_caller]
256277
pub fn entity<'a>(&'a mut self, entity: Entity) -> EntityCommands<'w, 's, 'a> {
@@ -262,8 +283,12 @@ impl<'w, 's> Commands<'w, 's> {
262283
})
263284
}
264285

265-
/// Returns an option containing an [`EntityCommands`] builder for the requested [`Entity`] if it exists, otherwise `None`.
266-
/// This does not ensure that the commands will succeed as the entity may no longer exist by the time the associated commands are executed.
286+
/// Returns the [`EntityCommands`] for the requested [`Entity`], if it exists.
287+
///
288+
/// Returns `None` if the entity does not exist.
289+
///
290+
/// This method does not guarantee that `EntityCommands` will be successfully applied,
291+
/// since another command in the queue may delete the entity before them.
267292
///
268293
/// # Example
269294
///
@@ -285,6 +310,10 @@ impl<'w, 's> Commands<'w, 's> {
285310
/// }
286311
/// # bevy_ecs::system::assert_is_system(example_system);
287312
/// ```
313+
///
314+
/// # See also
315+
///
316+
/// - [`entity`](Self::entity) for the panicking version.
288317
#[inline]
289318
#[track_caller]
290319
pub fn get_entity<'a>(&'a mut self, entity: Entity) -> Option<EntityCommands<'w, 's, 'a>> {
@@ -294,12 +323,14 @@ impl<'w, 's> Commands<'w, 's> {
294323
})
295324
}
296325

297-
/// Spawns entities to the [`World`] according to the given iterator (or a type that can
298-
/// be converted to it).
326+
/// Pushes a [`Command`] to the queue for creating entities with a particular [`Bundle`] type.
299327
///
300-
/// The end result of this command is equivalent to iterating `bundles_iter` and calling
301-
/// [`spawn`](Self::spawn) on each bundle, but it is more performant due to memory
302-
/// pre-allocation.
328+
/// `bundles_iter` is a type that can be converted into a `Bundle` iterator
329+
/// (it can also be a collection).
330+
///
331+
/// This method is equivalent to iterating `bundles_iter`
332+
/// and calling [`spawn`](Self::spawn) on each bundle,
333+
/// but it is faster due to memory pre-allocation.
303334
///
304335
/// # Example
305336
///
@@ -325,6 +356,11 @@ impl<'w, 's> Commands<'w, 's> {
325356
/// # }
326357
/// # bevy_ecs::system::assert_is_system(system);
327358
/// ```
359+
///
360+
/// # See also
361+
///
362+
/// - [`spawn`](Self::spawn) to just spawn an entity without any component.
363+
/// - [`spawn_bundle`](Self::spawn_bundle) to spawn an entity with a bundle.
328364
pub fn spawn_batch<I>(&mut self, bundles_iter: I)
329365
where
330366
I: IntoIterator + Send + Sync + 'static,
@@ -333,10 +369,21 @@ impl<'w, 's> Commands<'w, 's> {
333369
self.queue.push(SpawnBatch { bundles_iter });
334370
}
335371

336-
/// For a given batch of ([Entity], [Bundle]) pairs, either spawns each [Entity] with the given
337-
/// bundle (if the entity does not exist), or inserts the [Bundle] (if the entity already exists).
372+
/// Pushes a [`Command`] to the queue for creating entities, if needed,
373+
/// and for adding a bundle to each entity.
374+
///
375+
/// `bundles_iter` is a type that can be converted into an ([`Entity`], [`Bundle`]) iterator
376+
/// (it can also be a collection).
338377
///
339-
/// This is faster than doing equivalent operations one-by-one.
378+
/// When the command is applied,
379+
/// for each (`Entity`, `Bundle`) pair in the given `bundles_iter`,
380+
/// the `Entity` is spawned, if it does not exist already.
381+
/// Then, the `Bundle` is added to the entity.
382+
///
383+
/// This method is equivalent to iterating `bundles_iter`,
384+
/// calling [`get_or_spawn`](Self::get_or_spawn) for each bundle,
385+
/// and passing it to [`insert_bundle`](EntityCommands::insert_bundle),
386+
/// but it is faster due to memory pre-allocation.
340387
///
341388
/// # Note
342389
///
@@ -352,17 +399,13 @@ impl<'w, 's> Commands<'w, 's> {
352399
self.queue.push(InsertOrSpawnBatch { bundles_iter });
353400
}
354401

355-
/// Inserts a resource with standard starting values to the [`World`].
356-
///
357-
/// If the resource already exists, nothing happens.
402+
/// Pushes a [`Command`] to the queue for inserting a [`Resource`] in the [`World`] with an inferred value.
358403
///
359-
/// The value given by the [`FromWorld::from_world`] method will be used.
360-
/// Note that any resource with the `Default` trait automatically implements `FromWorld`,
361-
/// and those default values will be here instead.
404+
/// The inferred value is determined by the [`FromWorld`] trait of the resource.
405+
/// When the command is applied,
406+
/// if the resource already exists, nothing happens.
362407
///
363408
/// See [`World::init_resource`] for more details.
364-
/// Note that commands do not take effect immediately.
365-
/// When possible, prefer the equivalent methods on `App` or `World`.
366409
///
367410
/// # Example
368411
///
@@ -386,11 +429,11 @@ impl<'w, 's> Commands<'w, 's> {
386429
});
387430
}
388431

389-
/// Inserts a resource to the [`World`], overwriting any previous value of the same type.
432+
/// Pushes a [`Command`] to the queue for inserting a [`Resource`] in the [`World`] with a specific value.
433+
///
434+
/// This will overwrite any previous value of the same resource type.
390435
///
391436
/// See [`World::insert_resource`] for more details.
392-
/// Note that commands do not take effect immediately.
393-
/// When possible, prefer the equivalent methods on `App` or `World`.
394437
///
395438
/// # Example
396439
///
@@ -415,7 +458,7 @@ impl<'w, 's> Commands<'w, 's> {
415458
self.queue.push(InsertResource { resource });
416459
}
417460

418-
/// Removes a resource from the [`World`].
461+
/// Pushes a [`Command`] to the queue for removing a [`Resource`] from the [`World`].
419462
///
420463
/// See [`World::remove_resource`] for more details.
421464
///
@@ -441,7 +484,7 @@ impl<'w, 's> Commands<'w, 's> {
441484
});
442485
}
443486

444-
/// Adds a command directly to the command queue.
487+
/// Pushes a generic [`Command`] to the command queue.
445488
///
446489
/// `command` can be a built-in command, custom struct that implements [`Command`] or a closure
447490
/// that takes [`&mut World`](World) as an argument.

0 commit comments

Comments
 (0)