1
1
use crate :: {
2
- entity:: Entity ,
3
2
prelude:: Mut ,
4
3
reflect:: { AppTypeRegistry , ReflectBundle , ReflectComponent } ,
5
4
resource:: Resource ,
6
5
system:: EntityCommands ,
7
- world:: { EntityWorldMut , World } ,
6
+ world:: EntityWorldMut ,
8
7
} ;
9
8
use alloc:: { borrow:: Cow , boxed:: Box } ;
10
9
use bevy_reflect:: { PartialReflect , TypeRegistry } ;
@@ -22,7 +21,7 @@ pub trait ReflectCommandExt {
22
21
/// - If [`AppTypeRegistry`] does not have the reflection data for the given
23
22
/// [`Component`](crate::component::Component) or [`Bundle`](crate::bundle::Bundle).
24
23
/// - If the component or bundle data is invalid. See [`PartialReflect::apply`] for further details.
25
- /// - If [`AppTypeRegistry`] is not present in the [`World`].
24
+ /// - If [`AppTypeRegistry`] is not present in the [`World`](crate::world::World) .
26
25
///
27
26
/// # Note
28
27
///
@@ -92,11 +91,11 @@ pub trait ReflectCommandExt {
92
91
///
93
92
/// # Panics
94
93
///
95
- /// - If the given [`Resource`] is not present in the [`World`].
94
+ /// - If the given [`Resource`] is not present in the [`World`](crate::world::World) .
96
95
///
97
96
/// # Note
98
97
///
99
- /// - The given [`Resource`] is removed from the [`World`] before the command is applied.
98
+ /// - The given [`Resource`] is removed from the [`World`](crate::world::World) before the command is applied.
100
99
fn insert_reflect_with_registry < T : Resource + AsRef < TypeRegistry > > (
101
100
& mut self ,
102
101
component : Box < dyn PartialReflect > ,
@@ -214,23 +213,18 @@ impl<'w> EntityWorldMut<'w> {
214
213
/// - If [`AppTypeRegistry`] does not have the reflection data for the given
215
214
/// [`Component`](crate::component::Component) or [`Bundle`](crate::bundle::Bundle).
216
215
/// - If the component or bundle data is invalid. See [`PartialReflect::apply`] for further details.
217
- /// - If [`AppTypeRegistry`] is not present in the [`World`].
216
+ /// - If [`AppTypeRegistry`] is not present in the [`World`](crate::world::World) .
218
217
///
219
218
/// # Note
220
219
///
221
220
/// Prefer to use the typed [`EntityWorldMut::insert`] if possible. Adding a reflected component
222
221
/// is much slower.
223
222
pub fn insert_reflect ( & mut self , component : Box < dyn PartialReflect > ) -> & mut Self {
224
223
self . assert_not_despawned ( ) ;
225
- let entity_id = self . id ( ) ;
226
- self . world_scope ( |world| {
227
- world. resource_scope ( |world, registry : Mut < AppTypeRegistry > | {
228
- let type_registry = & registry. as_ref ( ) . read ( ) ;
229
- insert_reflect_with_registry_ref ( world, entity_id, type_registry, component) ;
230
- } ) ;
231
- world. flush ( ) ;
224
+ self . resource_scope ( |entity, registry : Mut < AppTypeRegistry > | {
225
+ let type_registry = & registry. as_ref ( ) . read ( ) ;
226
+ insert_reflect_with_registry_ref ( entity, type_registry, component) ;
232
227
} ) ;
233
- self . update_location ( ) ;
234
228
self
235
229
}
236
230
@@ -245,21 +239,16 @@ impl<'w> EntityWorldMut<'w> {
245
239
/// - If the given [`Resource`] does not have the reflection data for the given
246
240
/// [`Component`](crate::component::Component) or [`Bundle`](crate::bundle::Bundle).
247
241
/// - If the component or bundle data is invalid. See [`PartialReflect::apply`] for further details.
248
- /// - If the given [`Resource`] is not present in the [`World`].
242
+ /// - If the given [`Resource`] is not present in the [`World`](crate::world::World) .
249
243
pub fn insert_reflect_with_registry < T : Resource + AsRef < TypeRegistry > > (
250
244
& mut self ,
251
245
component : Box < dyn PartialReflect > ,
252
246
) -> & mut Self {
253
247
self . assert_not_despawned ( ) ;
254
- let entity_id = self . id ( ) ;
255
- self . world_scope ( |world| {
256
- world. resource_scope ( |world, registry : Mut < T > | {
257
- let type_registry = registry. as_ref ( ) . as_ref ( ) ;
258
- insert_reflect_with_registry_ref ( world, entity_id, type_registry, component) ;
259
- } ) ;
260
- world. flush ( ) ;
248
+ self . resource_scope ( |entity, registry : Mut < T > | {
249
+ let type_registry = registry. as_ref ( ) . as_ref ( ) ;
250
+ insert_reflect_with_registry_ref ( entity, type_registry, component) ;
261
251
} ) ;
262
- self . update_location ( ) ;
263
252
self
264
253
}
265
254
@@ -275,28 +264,18 @@ impl<'w> EntityWorldMut<'w> {
275
264
/// # Panics
276
265
///
277
266
/// - If the entity has been despawned while this `EntityWorldMut` is still alive.
278
- /// - If [`AppTypeRegistry`] is not present in the [`World`].
267
+ /// - If [`AppTypeRegistry`] is not present in the [`World`](crate::world::World) .
279
268
///
280
269
/// # Note
281
270
///
282
271
/// Prefer to use the typed [`EntityCommands::remove`] if possible. Removing a reflected component
283
272
/// is much slower.
284
273
pub fn remove_reflect ( & mut self , component_type_path : Cow < ' static , str > ) -> & mut Self {
285
274
self . assert_not_despawned ( ) ;
286
- let entity_id = self . id ( ) ;
287
- self . world_scope ( |world| {
288
- world. resource_scope ( |world, registry : Mut < AppTypeRegistry > | {
289
- let type_registry = & registry. as_ref ( ) . read ( ) ;
290
- remove_reflect_with_registry_ref (
291
- world,
292
- entity_id,
293
- type_registry,
294
- component_type_path,
295
- ) ;
296
- } ) ;
297
- world. flush ( ) ;
275
+ self . resource_scope ( |entity, registry : Mut < AppTypeRegistry > | {
276
+ let type_registry = & registry. as_ref ( ) . read ( ) ;
277
+ remove_reflect_with_registry_ref ( entity, type_registry, component_type_path) ;
298
278
} ) ;
299
- self . update_location ( ) ;
300
279
self
301
280
}
302
281
@@ -313,75 +292,56 @@ impl<'w> EntityWorldMut<'w> {
313
292
/// # Panics
314
293
///
315
294
/// - If the entity has been despawned while this `EntityWorldMut` is still alive.
316
- /// - If [`AppTypeRegistry`] is not present in the [`World`].
295
+ /// - If [`AppTypeRegistry`] is not present in the [`World`](crate::world::World) .
317
296
pub fn remove_reflect_with_registry < T : Resource + AsRef < TypeRegistry > > (
318
297
& mut self ,
319
298
component_type_path : Cow < ' static , str > ,
320
299
) -> & mut Self {
321
300
self . assert_not_despawned ( ) ;
322
- let entity_id = self . id ( ) ;
323
- self . world_scope ( |world| {
324
- world. resource_scope ( |world, registry : Mut < T > | {
325
- let type_registry = registry. as_ref ( ) . as_ref ( ) ;
326
- remove_reflect_with_registry_ref (
327
- world,
328
- entity_id,
329
- type_registry,
330
- component_type_path,
331
- ) ;
332
- } ) ;
333
- world. flush ( ) ;
301
+ self . resource_scope ( |entity, registry : Mut < T > | {
302
+ let type_registry = registry. as_ref ( ) . as_ref ( ) ;
303
+ remove_reflect_with_registry_ref ( entity, type_registry, component_type_path) ;
334
304
} ) ;
335
- self . update_location ( ) ;
336
305
self
337
306
}
338
307
}
339
308
340
309
/// Helper function to add a reflect component or bundle to a given entity
341
310
fn insert_reflect_with_registry_ref (
342
- world : & mut World ,
343
- entity : Entity ,
311
+ entity : & mut EntityWorldMut ,
344
312
type_registry : & TypeRegistry ,
345
313
component : Box < dyn PartialReflect > ,
346
314
) {
347
315
let type_info = component
348
316
. get_represented_type_info ( )
349
317
. expect ( "component should represent a type." ) ;
350
318
let type_path = type_info. type_path ( ) ;
351
- let Ok ( mut entity) = world. get_entity_mut ( entity) else {
352
- panic ! ( "error[B0003]: Could not insert a reflected component (of type {type_path}) for entity {entity}, which {}. See: https://bevy.org/learn/errors/b0003" ,
353
- world. entities( ) . entity_does_not_exist_error_details( entity) ) ;
354
- } ;
355
319
let Some ( type_registration) = type_registry. get ( type_info. type_id ( ) ) else {
356
320
panic ! ( "`{type_path}` should be registered in type registry via `App::register_type<{type_path}>`" ) ;
357
321
} ;
358
322
359
323
if let Some ( reflect_component) = type_registration. data :: < ReflectComponent > ( ) {
360
- reflect_component. insert ( & mut entity, component. as_partial_reflect ( ) , type_registry) ;
324
+ reflect_component. insert ( entity, component. as_partial_reflect ( ) , type_registry) ;
361
325
} else if let Some ( reflect_bundle) = type_registration. data :: < ReflectBundle > ( ) {
362
- reflect_bundle. insert ( & mut entity, component. as_partial_reflect ( ) , type_registry) ;
326
+ reflect_bundle. insert ( entity, component. as_partial_reflect ( ) , type_registry) ;
363
327
} else {
364
328
panic ! ( "`{type_path}` should have #[reflect(Component)] or #[reflect(Bundle)]" ) ;
365
329
}
366
330
}
367
331
368
332
/// Helper function to remove a reflect component or bundle from a given entity
369
333
fn remove_reflect_with_registry_ref (
370
- world : & mut World ,
371
- entity : Entity ,
334
+ entity : & mut EntityWorldMut ,
372
335
type_registry : & TypeRegistry ,
373
336
component_type_path : Cow < ' static , str > ,
374
337
) {
375
- let Ok ( mut entity) = world. get_entity_mut ( entity) else {
376
- return ;
377
- } ;
378
338
let Some ( type_registration) = type_registry. get_with_type_path ( & component_type_path) else {
379
339
return ;
380
340
} ;
381
341
if let Some ( reflect_component) = type_registration. data :: < ReflectComponent > ( ) {
382
- reflect_component. remove ( & mut entity) ;
342
+ reflect_component. remove ( entity) ;
383
343
} else if let Some ( reflect_bundle) = type_registration. data :: < ReflectBundle > ( ) {
384
- reflect_bundle. remove ( & mut entity) ;
344
+ reflect_bundle. remove ( entity) ;
385
345
}
386
346
}
387
347
0 commit comments