Search Unity

Feedback Chunk Component Data doesn't behave as advertised

Discussion in 'Entity Component System' started by sschoener, Oct 8, 2019.

  1. sschoener

    sschoener

    Joined:
    Aug 18, 2014
    Posts:
    73
    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:

    Code (CSharp):
    1. using NUnit.Framework;
    2. using NUnit.Framework.Constraints;
    3. using Unity.Entities;
    4. using Unity.Entities.Tests;
    5. using UnityEngine;
    6.  
    7. public struct Tag : IComponentData { }
    8. public struct DataComponent : IComponentData
    9. {
    10.     public int Value;
    11. }
    12.  
    13. public class ChunkComponentTest : ECSTestsFixture
    14. {
    15.     private int GetChunkData(Entity e) => m_Manager.GetChunkComponentData<DataComponent>(e).Value;
    16.  
    17.     [Test]
    18.     public void DataSurvivesChunkMove()
    19.     {
    20.         var entity = m_Manager.CreateEntity();
    21.         m_Manager.AddChunkComponentData(m_Manager.UniversalQuery, new DataComponent { Value = 2 });
    22.         Assert.AreEqual(GetChunkData(entity), 2);
    23.         m_Manager.AddComponent<Tag>(entity);
    24.         Assert.AreEqual(GetChunkData(entity), 2); // this fails, the actual value is 0
    25.  
    26.         m_Manager.DestroyEntity(entity);
    27.     }
    28.  
    29.     [Test]
    30.     public void DataSurvivesChunkMove_Query()
    31.     {
    32.         var entity = m_Manager.CreateEntity();
    33.         m_Manager.AddChunkComponentData(m_Manager.UniversalQuery, new DataComponent { Value = 2 });
    34.         Assert.AreEqual(GetChunkData(entity), 2);
    35.         m_Manager.AddComponent<Tag>(m_Manager.UniversalQuery); // this line is different, using a query instead of the entity
    36.         Assert.AreEqual(GetChunkData(entity), 2); // this passes
    37.         m_Manager.DestroyEntity(entity);
    38.     }
    39. }
    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?
     
    Last edited: Oct 8, 2019
  2. Fabrice_Lete

    Fabrice_Lete

    Unity Technologies

    Joined:
    May 5, 2018
    Posts:
    55
    There's no guarantee about the value of a chunk component associated to an entity that gets moved to another chunk. Only adding or removing tag components on whole chunks (via query) is expected to preserve the chunk components.

    We'll get the documentation fixed, thanks for the reporting the problem!