@@ -317,23 +317,29 @@ public Entity Create(in Signature types)
317
317
/// Moves an <see cref="Entity"/> from one <see cref="Archetype"/> <see cref="Slot"/> to another.
318
318
/// </summary>
319
319
/// <param name="entity">The <see cref="Entity"/>.</param>
320
+ /// <param name="data">The <see cref="EntityData"/> of the supplied entity.</param>
320
321
/// <param name="source">Its <see cref="Archetype"/>.</param>
321
322
/// <param name="destination">The new <see cref="Archetype"/>.</param>
322
323
/// <param name="destinationSlot">The new <see cref="Slot"/> in which the moved <see cref="Entity"/> will land.</param>
323
- internal void Move ( Entity entity , Archetype source , Archetype destination , out Slot destinationSlot )
324
+ internal void Move ( Entity entity , ref EntityData data , Archetype source , Archetype destination , out Slot destinationSlot )
324
325
{
326
+ // Entity should match the supplied EntityData.
327
+ Debug . Assert ( entity == data . Archetype . Entity ( ref data . Slot ) ) ;
328
+
325
329
// A common mistake, happening in many cases.
326
330
Debug . Assert ( source != destination , "From-Archetype is the same as the To-Archetype. Entities cannot move within the same archetype using this function. Probably an attempt was made to attach already existing components to the entity or to remove non-existing ones." ) ;
327
331
328
332
// Copy entity to other archetype
329
- ref var slot = ref EntityInfo . GetSlot ( entity . Id ) ;
333
+ var slot = data . Slot ;
330
334
var allocatedEntities = destination . Add ( entity , out _ , out destinationSlot ) ;
331
335
Archetype . CopyComponents ( source , ref slot , destination , ref destinationSlot ) ;
332
336
source . Remove ( slot , out var movedEntity ) ;
333
337
334
338
// Update moved entity from the remove
335
339
EntityInfo . Move ( movedEntity , slot ) ;
336
- EntityInfo . Move ( entity . Id , destination , destinationSlot ) ;
340
+
341
+ data . Archetype = destination ;
342
+ data . Slot = destinationSlot ;
337
343
338
344
// Calculate the entity difference between the moved archetypes to allocate more space accordingly.
339
345
Capacity += allocatedEntities ;
@@ -1223,11 +1229,12 @@ public ref T AddOrGet<T>(Entity entity, T? component = default)
1223
1229
[ StructuralChange ]
1224
1230
internal void Add < T > ( Entity entity , out Archetype newArchetype , out Slot slot )
1225
1231
{
1226
- var oldArchetype = EntityInfo . GetArchetype ( entity . Id ) ;
1232
+ ref var data = ref EntityInfo . EntityData [ entity . Id ] ;
1233
+ var oldArchetype = data . Archetype ;
1227
1234
var type = Component < T > . ComponentType ;
1228
1235
newArchetype = GetOrCreateArchetypeByAddEdge ( in type , oldArchetype ) ;
1229
1236
1230
- Move ( entity , oldArchetype , newArchetype , out slot ) ;
1237
+ Move ( entity , ref data , oldArchetype , newArchetype , out slot ) ;
1231
1238
}
1232
1239
1233
1240
/// <summary>
@@ -1279,12 +1286,14 @@ public void Add<T>(Entity entity, in T component)
1279
1286
[ StructuralChange ]
1280
1287
public void Remove < T > ( Entity entity )
1281
1288
{
1282
- var oldArchetype = EntityInfo . GetArchetype ( entity . Id ) ;
1289
+ ref var data = ref EntityInfo . EntityData [ entity . Id ] ;
1290
+ var oldArchetype = data . Archetype ;
1291
+
1283
1292
var type = Component < T > . ComponentType ;
1284
1293
var newArchetype = GetOrCreateArchetypeByRemoveEdge ( in type , oldArchetype ) ;
1285
1294
1286
1295
OnComponentRemoved < T > ( entity ) ;
1287
- Move ( entity , oldArchetype , newArchetype , out _ ) ;
1296
+ Move ( entity , ref data , oldArchetype , newArchetype , out _ ) ;
1288
1297
}
1289
1298
}
1290
1299
@@ -1449,11 +1458,13 @@ public bool TryGet(Entity entity, ComponentType type, out object? component)
1449
1458
[ StructuralChange ]
1450
1459
public void Add ( Entity entity , in object cmp )
1451
1460
{
1452
- var oldArchetype = EntityInfo . GetArchetype ( entity . Id ) ;
1461
+ ref var data = ref EntityInfo . EntityData [ entity . Id ] ;
1462
+
1463
+ var oldArchetype = data . Archetype ;
1453
1464
var type = ( ComponentType ) cmp . GetType ( ) ;
1454
1465
var newArchetype = GetOrCreateArchetypeByAddEdge ( in type , oldArchetype ) ;
1455
1466
1456
- Move ( entity , oldArchetype , newArchetype , out var slot ) ;
1467
+ Move ( entity , ref data , oldArchetype , newArchetype , out var slot ) ;
1457
1468
newArchetype . Set ( ref slot , cmp ) ;
1458
1469
OnComponentAdded ( entity , type ) ;
1459
1470
}
@@ -1470,7 +1481,8 @@ public void Add(Entity entity, in object cmp)
1470
1481
[ StructuralChange ]
1471
1482
public void AddRange ( Entity entity , Span < object > components )
1472
1483
{
1473
- var oldArchetype = EntityInfo . GetArchetype ( entity . Id ) ;
1484
+ ref var data = ref EntityInfo . EntityData [ entity . Id ] ;
1485
+ var oldArchetype = data . Archetype ;
1474
1486
1475
1487
// BitSet to stack/span bitset, size big enough to contain ALL registered components.
1476
1488
Span < uint > stack = stackalloc uint [ BitSet . RequiredLength ( ComponentRegistry . Size ) ] ;
@@ -1498,7 +1510,7 @@ public void AddRange(Entity entity, Span<object> components)
1498
1510
}
1499
1511
1500
1512
// Move and fire events
1501
- Move ( entity , oldArchetype , newArchetype , out var slot ) ;
1513
+ Move ( entity , ref data , oldArchetype , newArchetype , out var slot ) ;
1502
1514
foreach ( var cmp in components )
1503
1515
{
1504
1516
newArchetype . Set ( ref slot , cmp ) ;
@@ -1519,7 +1531,8 @@ public void AddRange(Entity entity, Span<object> components)
1519
1531
[ StructuralChange ]
1520
1532
public void AddRange ( Entity entity , Span < ComponentType > components )
1521
1533
{
1522
- var oldArchetype = EntityInfo . GetArchetype ( entity . Id ) ;
1534
+ ref var data = ref EntityInfo . EntityData [ entity . Id ] ;
1535
+ var oldArchetype = data . Archetype ;
1523
1536
1524
1537
// BitSet to stack/span bitset, size big enough to contain ALL registered components.
1525
1538
Span < uint > stack = stackalloc uint [ BitSet . RequiredLength ( ComponentRegistry . Size ) ] ;
@@ -1540,7 +1553,7 @@ public void AddRange(Entity entity, Span<ComponentType> components)
1540
1553
newArchetype = GetOrCreate ( newSignature ) ;
1541
1554
}
1542
1555
1543
- Move ( entity , oldArchetype , newArchetype , out _ ) ;
1556
+ Move ( entity , ref data , oldArchetype , newArchetype , out _ ) ;
1544
1557
1545
1558
#if EVENTS
1546
1559
for ( var i = 0 ; i < components . Length ; i ++ )
@@ -1561,7 +1574,8 @@ public void AddRange(Entity entity, Span<ComponentType> components)
1561
1574
[ StructuralChange ]
1562
1575
public void Remove ( Entity entity , ComponentType type )
1563
1576
{
1564
- var oldArchetype = EntityInfo . GetArchetype ( entity . Id ) ;
1577
+ ref var data = ref EntityInfo . EntityData [ entity . Id ] ;
1578
+ var oldArchetype = data . Archetype ;
1565
1579
1566
1580
// BitSet to stack/span bitset, size big enough to contain ALL registered components.
1567
1581
Span < uint > stack = stackalloc uint [ oldArchetype . BitSet . Length ] ;
@@ -1578,7 +1592,7 @@ public void Remove(Entity entity, ComponentType type)
1578
1592
}
1579
1593
1580
1594
OnComponentRemoved ( entity , type ) ;
1581
- Move ( entity , oldArchetype , newArchetype , out _ ) ;
1595
+ Move ( entity , ref data , oldArchetype , newArchetype , out _ ) ;
1582
1596
}
1583
1597
1584
1598
/// <summary>
@@ -1593,7 +1607,8 @@ public void Remove(Entity entity, ComponentType type)
1593
1607
[ StructuralChange ]
1594
1608
public void RemoveRange ( Entity entity , Span < ComponentType > types )
1595
1609
{
1596
- var oldArchetype = EntityInfo . GetArchetype ( entity . Id ) ;
1610
+ ref var data = ref EntityInfo . EntityData [ entity . Id ] ;
1611
+ var oldArchetype = data . Archetype ;
1597
1612
1598
1613
// BitSet to stack/span bitset, size big enough to contain ALL registered components.
1599
1614
Span < uint > stack = stackalloc uint [ oldArchetype . BitSet . Length ] ;
@@ -1620,7 +1635,7 @@ public void RemoveRange(Entity entity, Span<ComponentType> types)
1620
1635
OnComponentRemoved ( entity , type ) ;
1621
1636
}
1622
1637
1623
- Move ( entity , oldArchetype , newArchetype , out _ ) ;
1638
+ Move ( entity , ref data , oldArchetype , newArchetype , out _ ) ;
1624
1639
}
1625
1640
}
1626
1641
0 commit comments