@@ -1035,7 +1035,7 @@ impl<'w> BundleInserter<'w> {
1035
1035
change_tick : Tick ,
1036
1036
) -> Self {
1037
1037
// SAFETY: We will not make any accesses to the command queue, component or resource data of this world
1038
- let mut bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1038
+ let bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1039
1039
let bundle_id = bundle_info. id ( ) ;
1040
1040
let ( new_archetype_id, is_new_created) = bundle_info. insert_bundle_into_archetype (
1041
1041
& mut world. archetypes ,
@@ -1045,12 +1045,7 @@ impl<'w> BundleInserter<'w> {
1045
1045
archetype_id,
1046
1046
) ;
1047
1047
1048
- if is_new_created {
1049
- world. trigger ( ArchetypeCreated ( new_archetype_id) ) ;
1050
- bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1051
- }
1052
-
1053
- if new_archetype_id == archetype_id {
1048
+ let inserter = if new_archetype_id == archetype_id {
1054
1049
let archetype = & mut world. archetypes [ archetype_id] ;
1055
1050
// SAFETY: The edge is assured to be initialized when we called insert_bundle_into_archetype
1056
1051
let archetype_after_insert = unsafe {
@@ -1110,7 +1105,15 @@ impl<'w> BundleInserter<'w> {
1110
1105
world : world. as_unsafe_world_cell ( ) ,
1111
1106
}
1112
1107
}
1108
+ } ;
1109
+
1110
+ if is_new_created {
1111
+ inserter
1112
+ . world
1113
+ . into_deferred ( )
1114
+ . trigger ( ArchetypeCreated ( new_archetype_id) ) ;
1113
1115
}
1116
+ inserter
1114
1117
}
1115
1118
1116
1119
/// # Safety
@@ -1200,16 +1203,16 @@ impl<'w> BundleInserter<'w> {
1200
1203
unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
1201
1204
entities. set (
1202
1205
swapped_entity. index ( ) ,
1203
- EntityLocation {
1206
+ Some ( EntityLocation {
1204
1207
archetype_id : swapped_location. archetype_id ,
1205
1208
archetype_row : location. archetype_row ,
1206
1209
table_id : swapped_location. table_id ,
1207
1210
table_row : swapped_location. table_row ,
1208
- } ,
1211
+ } ) ,
1209
1212
) ;
1210
1213
}
1211
1214
let new_location = new_archetype. allocate ( entity, result. table_row ) ;
1212
- entities. set ( entity. index ( ) , new_location) ;
1215
+ entities. set ( entity. index ( ) , Some ( new_location) ) ;
1213
1216
let after_effect = bundle_info. write_components (
1214
1217
table,
1215
1218
sparse_sets,
@@ -1249,19 +1252,19 @@ impl<'w> BundleInserter<'w> {
1249
1252
unsafe { entities. get ( swapped_entity) . debug_checked_unwrap ( ) } ;
1250
1253
entities. set (
1251
1254
swapped_entity. index ( ) ,
1252
- EntityLocation {
1255
+ Some ( EntityLocation {
1253
1256
archetype_id : swapped_location. archetype_id ,
1254
1257
archetype_row : location. archetype_row ,
1255
1258
table_id : swapped_location. table_id ,
1256
1259
table_row : swapped_location. table_row ,
1257
- } ,
1260
+ } ) ,
1258
1261
) ;
1259
1262
}
1260
1263
// PERF: store "non bundle" components in edge, then just move those to avoid
1261
1264
// redundant copies
1262
1265
let move_result = table. move_to_superset_unchecked ( result. table_row , new_table) ;
1263
1266
let new_location = new_archetype. allocate ( entity, move_result. new_row ) ;
1264
- entities. set ( entity. index ( ) , new_location) ;
1267
+ entities. set ( entity. index ( ) , Some ( new_location) ) ;
1265
1268
1266
1269
// If an entity was moved into this entity's table spot, update its table row.
1267
1270
if let Some ( swapped_entity) = move_result. swapped_entity {
@@ -1271,12 +1274,12 @@ impl<'w> BundleInserter<'w> {
1271
1274
1272
1275
entities. set (
1273
1276
swapped_entity. index ( ) ,
1274
- EntityLocation {
1277
+ Some ( EntityLocation {
1275
1278
archetype_id : swapped_location. archetype_id ,
1276
1279
archetype_row : swapped_location. archetype_row ,
1277
1280
table_id : swapped_location. table_id ,
1278
1281
table_row : result. table_row ,
1279
- } ,
1282
+ } ) ,
1280
1283
) ;
1281
1284
1282
1285
if archetype. id ( ) == swapped_location. archetype_id {
@@ -1426,7 +1429,7 @@ impl<'w> BundleRemover<'w> {
1426
1429
bundle_id : BundleId ,
1427
1430
require_all : bool ,
1428
1431
) -> Option < Self > {
1429
- let mut bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1432
+ let bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1430
1433
// SAFETY: Caller ensures archetype and bundle ids are correct.
1431
1434
let ( new_archetype_id, is_new_created) = unsafe {
1432
1435
bundle_info. remove_bundle_from_archetype (
@@ -1444,11 +1447,6 @@ impl<'w> BundleRemover<'w> {
1444
1447
return None ;
1445
1448
}
1446
1449
1447
- if is_new_created {
1448
- world. trigger ( ArchetypeCreated ( new_archetype_id) ) ;
1449
- bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1450
- }
1451
-
1452
1450
let ( old_archetype, new_archetype) =
1453
1451
world. archetypes . get_2_mut ( archetype_id, new_archetype_id) ;
1454
1452
@@ -1462,13 +1460,20 @@ impl<'w> BundleRemover<'w> {
1462
1460
Some ( ( old. into ( ) , new. into ( ) ) )
1463
1461
} ;
1464
1462
1465
- Some ( Self {
1463
+ let remover = Self {
1466
1464
bundle_info : bundle_info. into ( ) ,
1467
1465
new_archetype : new_archetype. into ( ) ,
1468
1466
old_archetype : old_archetype. into ( ) ,
1469
1467
old_and_new_table : tables,
1470
1468
world : world. as_unsafe_world_cell ( ) ,
1471
- } )
1469
+ } ;
1470
+ if is_new_created {
1471
+ remover
1472
+ . world
1473
+ . into_deferred ( )
1474
+ . trigger ( ArchetypeCreated ( new_archetype_id) ) ;
1475
+ }
1476
+ Some ( remover)
1472
1477
}
1473
1478
1474
1479
/// This can be passed to [`remove`](Self::remove) as the `pre_remove` function if you don't want to do anything before removing.
@@ -1588,12 +1593,12 @@ impl<'w> BundleRemover<'w> {
1588
1593
1589
1594
world. entities . set (
1590
1595
swapped_entity. index ( ) ,
1591
- EntityLocation {
1596
+ Some ( EntityLocation {
1592
1597
archetype_id : swapped_location. archetype_id ,
1593
1598
archetype_row : location. archetype_row ,
1594
1599
table_id : swapped_location. table_id ,
1595
1600
table_row : swapped_location. table_row ,
1596
- } ,
1601
+ } ) ,
1597
1602
) ;
1598
1603
}
1599
1604
@@ -1629,12 +1634,12 @@ impl<'w> BundleRemover<'w> {
1629
1634
1630
1635
world. entities . set (
1631
1636
swapped_entity. index ( ) ,
1632
- EntityLocation {
1637
+ Some ( EntityLocation {
1633
1638
archetype_id : swapped_location. archetype_id ,
1634
1639
archetype_row : swapped_location. archetype_row ,
1635
1640
table_id : swapped_location. table_id ,
1636
1641
table_row : location. table_row ,
1637
- } ,
1642
+ } ) ,
1638
1643
) ;
1639
1644
world. archetypes [ swapped_location. archetype_id ]
1640
1645
. set_entity_table_row ( swapped_location. archetype_row , location. table_row ) ;
@@ -1650,7 +1655,7 @@ impl<'w> BundleRemover<'w> {
1650
1655
1651
1656
// SAFETY: The entity is valid and has been moved to the new location already.
1652
1657
unsafe {
1653
- world. entities . set ( entity. index ( ) , new_location) ;
1658
+ world. entities . set ( entity. index ( ) , Some ( new_location) ) ;
1654
1659
}
1655
1660
1656
1661
( new_location, pre_remove_result)
@@ -1689,7 +1694,7 @@ impl<'w> BundleSpawner<'w> {
1689
1694
bundle_id : BundleId ,
1690
1695
change_tick : Tick ,
1691
1696
) -> Self {
1692
- let mut bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1697
+ let bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1693
1698
let ( new_archetype_id, is_new_created) = bundle_info. insert_bundle_into_archetype (
1694
1699
& mut world. archetypes ,
1695
1700
& mut world. storages ,
@@ -1698,20 +1703,22 @@ impl<'w> BundleSpawner<'w> {
1698
1703
ArchetypeId :: EMPTY ,
1699
1704
) ;
1700
1705
1701
- if is_new_created {
1702
- world. trigger ( ArchetypeCreated ( new_archetype_id) ) ;
1703
- bundle_info = world. bundles . get_unchecked ( bundle_id) ;
1704
- }
1705
-
1706
1706
let archetype = & mut world. archetypes [ new_archetype_id] ;
1707
1707
let table = & mut world. storages . tables [ archetype. table_id ( ) ] ;
1708
- Self {
1708
+ let spawner = Self {
1709
1709
bundle_info : bundle_info. into ( ) ,
1710
1710
table : table. into ( ) ,
1711
1711
archetype : archetype. into ( ) ,
1712
1712
change_tick,
1713
1713
world : world. as_unsafe_world_cell ( ) ,
1714
+ } ;
1715
+ if is_new_created {
1716
+ spawner
1717
+ . world
1718
+ . into_deferred ( )
1719
+ . trigger ( ArchetypeCreated ( new_archetype_id) ) ;
1714
1720
}
1721
+ spawner
1715
1722
}
1716
1723
1717
1724
#[ inline]
@@ -1757,7 +1764,7 @@ impl<'w> BundleSpawner<'w> {
1757
1764
InsertMode :: Replace ,
1758
1765
caller,
1759
1766
) ;
1760
- entities. set ( entity. index ( ) , location) ;
1767
+ entities. set ( entity. index ( ) , Some ( location) ) ;
1761
1768
entities. mark_spawn_despawn ( entity. index ( ) , caller, self . change_tick ) ;
1762
1769
( location, after_effect)
1763
1770
} ;
0 commit comments