@@ -1474,10 +1474,41 @@ impl World {
1474
1474
self . entity_mut ( disabled. entity )
1475
1475
}
1476
1476
1477
+ /// Temporarily disables the requested entity from this [`World`], runs
1478
+ /// custom user code with a mutable reference to the entity's component of
1479
+ /// type `C`, then re-enables the entity before returning.
1480
+ ///
1481
+ /// This enables safe simultaneous mutable access to both a component of an
1482
+ /// entity and the rest of the [`World`].
1483
+ ///
1484
+ /// # Panics
1485
+ ///
1486
+ /// Panics if the resource does not exist.
1487
+ /// Use [`try_component_scope`](Self::try_component_scope) instead if you want to handle this case.
1488
+ /// Panics if the world is replaced during the execution of the closure.
1489
+ ///
1490
+ /// # Example
1491
+ /// ```
1492
+ /// use bevy_ecs::prelude::*;
1493
+ /// #[derive(Component)]
1494
+ /// struct A(u32);
1495
+ /// #[derive(Component)]
1496
+ /// struct B(u32);
1497
+ /// let mut world = World::new();
1498
+ /// let scoped_entity = world.spawn(A(1)).id();
1499
+ /// let entity = world.spawn(B(1)).id();
1500
+ ///
1501
+ /// world.component_scope(scoped_entity, |world, _, a: &mut A| {
1502
+ /// assert!(world.get_mut::<A>(scoped_entity).is_none());
1503
+ /// let b = world.get_mut::<B>(entity).unwrap();
1504
+ /// a.0 += b.0;
1505
+ /// });
1506
+ /// assert_eq!(world.get::<A>(scoped_entity).unwrap().0, 2);
1507
+ /// ```
1477
1508
pub fn component_scope <
1478
1509
C : Component < Mutability = Mutable > ,
1479
1510
R ,
1480
- F : FnOnce ( Entity , & mut C , & mut World ) -> R ,
1511
+ F : FnOnce ( & mut World , Entity , & mut C ) -> R ,
1481
1512
> (
1482
1513
& mut self ,
1483
1514
entity : Entity ,
@@ -1491,17 +1522,26 @@ impl World {
1491
1522
} ) ;
1492
1523
}
1493
1524
1525
+ /// Temporarily disables the requested entity from this [`World`], runs
1526
+ /// custom user code with a mutable reference to the entity's component of
1527
+ /// type `C`, then re-enables the entity before returning.
1528
+ ///
1529
+ /// This enables safe simultaneous mutable access to both a component of an
1530
+ /// entity and the rest of the [`World`].
1531
+ ///
1532
+ /// See also [`component_scope`](Self::component_scope).
1494
1533
pub fn try_component_scope <
1495
1534
C : Component < Mutability = Mutable > ,
1496
1535
R ,
1497
- F : FnOnce ( Entity , & mut C , & mut World ) -> R ,
1536
+ F : FnOnce ( & mut World , Entity , & mut C ) -> R ,
1498
1537
> (
1499
1538
& mut self ,
1500
1539
entity : Entity ,
1501
1540
f : F ,
1502
1541
) -> Option < R > {
1503
1542
self . flush ( ) ;
1504
1543
1544
+ let world_id = self . id ( ) ;
1505
1545
let mut entity_mut = self . get_entity_mut ( entity) . ok ( ) ?;
1506
1546
let mut component = {
1507
1547
let component = entity_mut. get_mut :: < C > ( ) ?;
@@ -1511,7 +1551,13 @@ impl World {
1511
1551
1512
1552
let disabled = entity_mut. disable ( ) ;
1513
1553
1514
- let out = f ( entity, & mut component, self ) ;
1554
+ let out = f ( self , entity, & mut component) ;
1555
+
1556
+ assert_eq ! (
1557
+ self . id( ) ,
1558
+ world_id,
1559
+ "World was replaced during component scope"
1560
+ ) ;
1515
1561
1516
1562
let mut entity_mut = self . enable ( disabled) ;
1517
1563
0 commit comments