Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Bug SystemAPI.Query produces wrong results with IEnableableComponent

Discussion in 'Entity Component System' started by GbaistPlarium, Feb 28, 2023.

  1. GbaistPlarium

    GbaistPlarium

    Joined:
    Jul 16, 2021
    Posts:
    3
    Hi All,
    We observe incorrect behavior of the SystemAPI.Query in Entities package version 1.0.0-pre.44. You can see in the ISystem example that the SystemAPI.Query produce completely wrong entities. Also, the behavior changes depending on whether to specify the type of parameters. The query builder works correctly. This system can be tested on an empty project.

    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using UnityEngine;
    4.  
    5. public struct EnableableComponent : IComponentData, IEnableableComponent {}
    6.  
    7. public struct IndexComponent : IComponentData
    8. {
    9.     public int index;
    10. }
    11.  
    12. public partial struct BrokenQuerySystem : ISystem
    13. {
    14.     public void OnCreate(ref SystemState state)
    15.     {
    16.         var archetype = state.EntityManager.CreateArchetype(
    17.             ComponentType.ReadOnly<EnableableComponent>(),
    18.             ComponentType.ReadOnly<IndexComponent>());
    19.        
    20.         for (var i = 0; i < 10; i++) {
    21.             var entity = state.EntityManager.CreateEntity(archetype);
    22.             state.EntityManager.SetComponentData(entity, new IndexComponent { index = i });
    23.             state.EntityManager.SetComponentEnabled<EnableableComponent>(entity, i % 3 == 0);
    24.         }
    25.         //RESULT
    26.         //Entity:11 BrokenIndex:0 EnableableComponent:true
    27.         //Entity:12 BrokenIndex:1 EnableableComponent:false
    28.         //Entity:13 BrokenIndex:2 EnableableComponent:false
    29.         //Entity:14 BrokenIndex:3 EnableableComponent:true
    30.         //Entity:15 BrokenIndex:4 EnableableComponent:false
    31.         //Entity:16 BrokenIndex:5 EnableableComponent:false
    32.         //Entity:17 BrokenIndex:6 EnableableComponent:true
    33.         //Entity:18 BrokenIndex:7 EnableableComponent:false
    34.         //Entity:19 BrokenIndex:8 EnableableComponent:false
    35.         //Entity:20 BrokenIndex:9 EnableableComponent:true
    36.     }
    37.  
    38.     public void OnUpdate(ref SystemState state)
    39.     {
    40.         foreach ((EnableableComponent _, RefRO<IndexComponent> indexRo, Entity entity) in
    41.                  SystemAPI.Query<EnableableComponent, RefRO<IndexComponent>>().WithEntityAccess()) {
    42.             Debug.Log($"Entity:{entity.Index} BrokenIndex:{indexRo.ValueRO.index}");
    43.         }
    44.         //RESULT
    45.         //Entity:11 BrokenIndex:0
    46.         //Entity:13 BrokenIndex:0 --> this log is displayed x3 times
    47.  
    48.         foreach (var (_, indexRo, entity) in
    49.                  SystemAPI.Query<EnableableComponent, RefRO<IndexComponent>>().WithEntityAccess()) {
    50.             Debug.Log($"Entity:{entity.Index} BrokenIndex:{indexRo.ValueRO.index}");
    51.         }
    52.         //RESULT
    53.         //Entity:11 BrokenIndex:0
    54.         //Entity:13 BrokenIndex:2 --> this log is displayed x3 times
    55.        
    56.         var query = SystemAPI.QueryBuilder().WithAll<EnableableComponent, IndexComponent>().Build();
    57.         var entityArray = query.ToEntityArray(Allocator.Temp);
    58.         var indexArray = query.ToComponentDataArray<IndexComponent>(Allocator.Temp);
    59.        
    60.         for (var i = 0; i < entityArray.Length; i++) {
    61.             var entity = entityArray[i];
    62.             var index = indexArray[i];
    63.             Debug.Log($"Entity:{entity.Index} BrokenIndex:{index.index}");
    64.         }
    65.         //RESULT
    66.         //Entity:11 BrokenIndex:0
    67.         //Entity:14 BrokenIndex:3
    68.         //Entity:17 BrokenIndex:6
    69.         //Entity:20 BrokenIndex:9
    70.     }
    71. }
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,647
  3. Nestilion

    Nestilion

    Joined:
    Jul 21, 2015
    Posts:
    4
  4. GbaistPlarium

    GbaistPlarium

    Joined:
    Jul 16, 2021
    Posts:
    3
    There seems to be a problem with RefRO and here something else, we checked without RefRo, and the query still works incorrectly in this case.

    Code (CSharp):
    1.         foreach (var (_, index, entity) in
    2.                  SystemAPI.Query<EnableableComponent, IndexComponent>().WithEntityAccess()) {
    3.             Debug.Log($"Entity:{entity.Index} BrokenIndex:{index.index}");
    4.         }
    5.         //RESULT
    6.         //Entity:11 BrokenIndex:0
    7.         //Entity:13 BrokenIndex:2
     
  5. GbaistPlarium

    GbaistPlarium

    Joined:
    Jul 16, 2021
    Posts:
    3
    UPD This bug is fixed in the version 1.0.0-pre.65
     
    cort_of_unity and oivanov like this.