Search Unity

CreateArchetypeChunkArray() ignores FilterChanged. Is this a bug?

Discussion in 'Entity Component System' started by SergeyRomanko, Jan 29, 2019.

  1. SergeyRomanko

    SergeyRomanko

    Joined:
    Oct 18, 2014
    Posts:
    47
    Hello everyone! I am trying to use SetFilterChanged to iterate on chunks that have new or changed components. Here is my code

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using UnityEngine;
    4.  
    5. namespace SmithyShop {
    6.    
    7.     [AlwaysUpdateSystem]
    8.     public class FilterTestSystem : ComponentSystem {
    9.        
    10.         private struct FilterTestComponent : IComponentData {
    11.             public int test;
    12.         }
    13.        
    14.         private ComponentGroup query;
    15.  
    16.         protected override void OnUpdate() {
    17.  
    18.             if (Input.GetMouseButtonDown(0)) {
    19.                 EntityManager.CreateEntity(typeof(FilterTestComponent));
    20.             }
    21.            
    22.             var type = GetArchetypeChunkComponentType<FilterTestComponent>(false);
    23.  
    24.             var length = query.CalculateLength();
    25.             var chunks = query.CreateArchetypeChunkArray(Allocator.TempJob);
    26.             for (int i = 0; i < chunks.Length; i++) {
    27.                 var chunk = chunks[i];
    28.                
    29.                 Debug.Log($"Changed! length:{length} {chunk.GetComponentVersion(type)} {LastSystemVersion} {type.GlobalSystemVersion}");
    30.                
    31.                 chunk.GetNativeArray(type);    //Set ComponentVersion to type.GlobalSystemVersion
    32.             }
    33.             chunks.Dispose();
    34.         }
    35.        
    36.         protected override void OnCreateManager() {
    37.             query = GetComponentGroup(typeof(FilterTestComponent));
    38.             query.SetFilterChanged(typeof(FilterTestComponent));
    39.         }
    40.        
    41.     }
    42. }
    Here is what I see in output window
    Code (csharp):
    1. Changed! length:1 0 518 528
    2. Changed! length:0 528 528 538
    3. Changed! length:0 538 538 548
    4. Changed! length:0 548 548 558
    query.CalculateLength() works correctly. It returns 1 after new entity was created, then it returns 0 because the chunk doesn't pass through FilterChanged.

    query.CreateArchetypeChunkArray(Allocator.TempJob) acts like there is no FilterChanged. It just works permanently.

    Even when CalculateLength is 0 CreateArchetypeChunkArray returns array with size 1. This makes no sense to me.

    Am I doing something wrong?
    Or is it just a bug inside CreateArchetypeChunkArray()?
     
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Have you also tried to call it readonly?


    Code (CSharp):
    1.             var type = GetArchetypeChunkComponentType<FilterTestComponent>(true);
     
  3. SergeyRomanko

    SergeyRomanko

    Joined:
    Oct 18, 2014
    Posts:
    47
    Hi @Spy-Shifty !

    Yes, I've tried to use read only type. It doesn't solve the problem.

    The thing is that I have to use ReadAndWrite type because otherwise component version inside chunk will always stay the same. Only ReadAndWrite type sets component version to LastSystemVersion, here is code inside GetNativeArray()

    Code (CSharp):
    1.  
    2. if (!chunkComponentType.IsReadOnly)
    3.     m_Chunk->ChangeVersion[typeIndexInArchetype] = chunkComponentType.GlobalSystemVersion;
    4.