Hi,
I have been toying around with Chunk Component Data and have run into some surprising behavior (entities 0.1.1-preview). Here are two test cases, the first one fails:
using NUnit.Framework;
using NUnit.Framework.Constraints;
using Unity.Entities;
using Unity.Entities.Tests;
using UnityEngine;
public struct Tag : IComponentData { }
public struct DataComponent : IComponentData
{
public int Value;
}
public class ChunkComponentTest : ECSTestsFixture
{
private int GetChunkData(Entity e) => m_Manager.GetChunkComponentData<DataComponent>(e).Value;
[Test]
public void DataSurvivesChunkMove()
{
var entity = m_Manager.CreateEntity();
m_Manager.AddChunkComponentData(m_Manager.UniversalQuery, new DataComponent { Value = 2 });
Assert.AreEqual(GetChunkData(entity), 2);
m_Manager.AddComponent<Tag>(entity);
Assert.AreEqual(GetChunkData(entity), 2); // this fails, the actual value is 0
m_Manager.DestroyEntity(entity);
}
[Test]
public void DataSurvivesChunkMove_Query()
{
var entity = m_Manager.CreateEntity();
m_Manager.AddChunkComponentData(m_Manager.UniversalQuery, new DataComponent { Value = 2 });
Assert.AreEqual(GetChunkData(entity), 2);
m_Manager.AddComponent<Tag>(m_Manager.UniversalQuery); // this line is different, using a query instead of the entity
Assert.AreEqual(GetChunkData(entity), 2); // this passes
m_Manager.DestroyEntity(entity);
}
}
In both cases a new entity is created, then DataComponent with Value set to 2 is added as chunk component data, and then the entity is tagged. In the second case I’m using a query to do the tagging, in the first case the entity itself is used.
It seems like the first case creates a new chunk with the new archetype, default initializes the DataComponent, and copies the entity over (dropping the original value). The second case keeps the value. In my understanding the first case contradicts the behavior as described in the documentation, which says (emphasis mine):
Is this as intended?