@@ -102,23 +102,29 @@ pub struct Commands<'w, 's> {
102
102
}
103
103
104
104
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
106
110
pub fn new ( queue : & ' s mut CommandQueue , world : & ' w World ) -> Self {
107
111
Self {
108
112
queue,
109
113
entities : world. entities ( ) ,
110
114
}
111
115
}
112
116
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
114
122
pub fn new_from_entities ( queue : & ' s mut CommandQueue , entities : & ' w Entities ) -> Self {
115
123
Self { queue, entities }
116
124
}
117
125
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`].
122
128
///
123
129
/// See [`World::spawn`] for more details.
124
130
///
@@ -147,6 +153,11 @@ impl<'w, 's> Commands<'w, 's> {
147
153
/// }
148
154
/// # bevy_ecs::system::assert_is_system(example_system);
149
155
/// ```
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.
150
161
pub fn spawn < ' a > ( & ' a mut self ) -> EntityCommands < ' w , ' s , ' a > {
151
162
let entity = self . entities . reserve_entity ( ) ;
152
163
EntityCommands {
@@ -155,10 +166,13 @@ impl<'w, 's> Commands<'w, 's> {
155
166
}
156
167
}
157
168
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.
160
173
///
161
174
/// # Note
175
+ ///
162
176
/// Spawning a specific `entity` value is rarely the right choice. Most apps should favor
163
177
/// [`Commands::spawn`]. This method should generally only be used for sharing entities across
164
178
/// 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> {
171
185
}
172
186
}
173
187
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`].
182
190
///
183
191
/// # Example
184
192
///
@@ -219,13 +227,22 @@ impl<'w, 's> Commands<'w, 's> {
219
227
/// }
220
228
/// # bevy_ecs::system::assert_is_system(example_system);
221
229
/// ```
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.
222
235
pub fn spawn_bundle < ' a , T : Bundle > ( & ' a mut self , bundle : T ) -> EntityCommands < ' w , ' s , ' a > {
223
236
let mut e = self . spawn ( ) ;
224
237
e. insert_bundle ( bundle) ;
225
238
e
226
239
}
227
240
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.
229
246
///
230
247
/// # Example
231
248
///
@@ -238,7 +255,7 @@ impl<'w, 's> Commands<'w, 's> {
238
255
/// struct Strength(u32);
239
256
/// #[derive(Component)]
240
257
/// struct Agility(u32);
241
-
258
+ ///
242
259
/// fn example_system(mut commands: Commands) {
243
260
/// // Create a new, empty entity
244
261
/// let entity = commands.spawn().id();
@@ -251,6 +268,10 @@ impl<'w, 's> Commands<'w, 's> {
251
268
/// }
252
269
/// # bevy_ecs::system::assert_is_system(example_system);
253
270
/// ```
271
+ ///
272
+ /// # See also
273
+ ///
274
+ /// - [`get_entity`](Self::get_entity) for the fallible version.
254
275
#[ inline]
255
276
#[ track_caller]
256
277
pub fn entity < ' a > ( & ' a mut self , entity : Entity ) -> EntityCommands < ' w , ' s , ' a > {
@@ -262,8 +283,12 @@ impl<'w, 's> Commands<'w, 's> {
262
283
} )
263
284
}
264
285
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.
267
292
///
268
293
/// # Example
269
294
///
@@ -285,6 +310,10 @@ impl<'w, 's> Commands<'w, 's> {
285
310
/// }
286
311
/// # bevy_ecs::system::assert_is_system(example_system);
287
312
/// ```
313
+ ///
314
+ /// # See also
315
+ ///
316
+ /// - [`entity`](Self::entity) for the panicking version.
288
317
#[ inline]
289
318
#[ track_caller]
290
319
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> {
294
323
} )
295
324
}
296
325
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.
299
327
///
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.
303
334
///
304
335
/// # Example
305
336
///
@@ -325,6 +356,11 @@ impl<'w, 's> Commands<'w, 's> {
325
356
/// # }
326
357
/// # bevy_ecs::system::assert_is_system(system);
327
358
/// ```
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.
328
364
pub fn spawn_batch < I > ( & mut self , bundles_iter : I )
329
365
where
330
366
I : IntoIterator + Send + Sync + ' static ,
@@ -333,10 +369,21 @@ impl<'w, 's> Commands<'w, 's> {
333
369
self . queue . push ( SpawnBatch { bundles_iter } ) ;
334
370
}
335
371
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).
338
377
///
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.
340
387
///
341
388
/// # Note
342
389
///
@@ -352,17 +399,13 @@ impl<'w, 's> Commands<'w, 's> {
352
399
self . queue . push ( InsertOrSpawnBatch { bundles_iter } ) ;
353
400
}
354
401
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.
358
403
///
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 .
362
407
///
363
408
/// 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`.
366
409
///
367
410
/// # Example
368
411
///
@@ -386,11 +429,11 @@ impl<'w, 's> Commands<'w, 's> {
386
429
} ) ;
387
430
}
388
431
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.
390
435
///
391
436
/// 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`.
394
437
///
395
438
/// # Example
396
439
///
@@ -415,7 +458,7 @@ impl<'w, 's> Commands<'w, 's> {
415
458
self . queue . push ( InsertResource { resource } ) ;
416
459
}
417
460
418
- /// Removes a resource from the [`World`].
461
+ /// Pushes a [`Command`] to the queue for removing a [`Resource`] from the [`World`].
419
462
///
420
463
/// See [`World::remove_resource`] for more details.
421
464
///
@@ -441,7 +484,7 @@ impl<'w, 's> Commands<'w, 's> {
441
484
} ) ;
442
485
}
443
486
444
- /// Adds a command directly to the command queue.
487
+ /// Pushes a generic [`Command`] to the command queue.
445
488
///
446
489
/// `command` can be a built-in command, custom struct that implements [`Command`] or a closure
447
490
/// that takes [`&mut World`](World) as an argument.
0 commit comments