Search Unity

Assertion failure when adding existing tag component via bulk AddComponent (Entities 0.1.0)

Discussion in 'Entity Component System' started by Shinyclef, Jul 31, 2019.

  1. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    505
    Calling
    public void AddComponent(NativeArray<Entity> entities, ComponentType componentType)
    to add a tag, when one or more of the entities already has that tag, causes the following error. One part of the code is ok with the duplicate and returns a null, but a subsequent part of the code doesn't like that null which causes a fail in the assertion here:

    Code (CSharp):
    1.         [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
    2.         public static void AssertAreLayoutCompatible(Archetype* a, Archetype* b)
    3.         {
    4.             Assert.IsTrue(AreLayoutCompatible(a,b));
    Am I meant to be disabling unity collections checks or something when trying to add tags that already exist? Or is it expected that I manually check my entities in my array to remove those which already have the tag?Or is this a regression?

    Full stack below. Ciao. :)

     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    By the way, assert error also triggers when chunk already has that component on it. E.g. via EntityQuery.

    Which sux, because you can't just fire and forget a component.

    I guess this is by design? Because adding component still triggers hard sync point.
    So the intention is to have less of them? But then again, it just means that tag components are invalid design.
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Asserts are generally not expected, they indicate a bug in our code.
    Validation of API calls generally throws ArgumentException.

    Can you provide a project folder that reproduces this issue?
     
    Shinyclef likes this.
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    What about this one in EntityManager (e.g. when component is set via EntityQuery from different systems):
    Code (CSharp):
    1. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
    2.         public void AssertCanAddComponent(NativeArray<ArchetypeChunk> chunkArray, ComponentType componentType)
    3.         {
    4.             var chunks = (ArchetypeChunk*) chunkArray.GetUnsafeReadOnlyPtr();
    5.             for (int i = 0; i < chunkArray.Length; ++i)
    6.             {
    7.                 var chunk = chunks[i].m_Chunk;
    8.                 if (ChunkDataUtility.GetIndexInTypeArray(chunk->Archetype, componentType.TypeIndex) != -1)
    9.                     throw new ArgumentException(
    10.                         $"A component with type:{componentType} has already been added to the chunk."); // <--- THIS ONE
    11.                 ...
    12.             }
    13.         }
    This one is by design and wont be removed, right?

    Will there be a way to check if the chunk has a component attached to it efficiently?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This is by design and unrelated to the original post here. When using entityQuery to add a component you should make sure that the query specifically does not yet have the component using exlusive / none components.

     
    xVergilx likes this.
  6. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    505
    Sure thing. Submitted bug report with case number 1173373.
    Now comes the part when I hope I haven't made some stupid user error haha. Fairly confident I haven't, but we'll see :D.
     
  7. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    56
    I found a bug in our code and made a fix in the Entities package. The fix should be available in the next package release.

    The bug is due to duplicate entities in the NativeArray that you pass to AddComponent(). A workaround you can do for now is to be sure no duplicates are in that array.
     
  8. Shinyclef

    Shinyclef

    Joined:
    Nov 20, 2013
    Posts:
    505
    Great, sounds like something I can improve on my end too! Cheers.
     
  9. Dale_Kim

    Dale_Kim

    Unity Technologies

    Joined:
    May 8, 2019
    Posts:
    56
    To be clear, when the fix is out, you should not have to use the workaround and having duplicate entities for that function call should be OK.
     
    Shinyclef likes this.