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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bug Bug (?) with aspects captured when using ScheduleParalell()

Discussion in 'Entity Component System' started by alior, Jun 14, 2023.

  1. alior

    alior

    Joined:
    Sep 6, 2014
    Posts:
    24
    I am playing with ECS and aspects feature and found out that I am constantly getting errors with aspect captured by Entities query when using ScheduleParalell().
    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. namespace Pathfinding
    4. {
    5.     public readonly partial struct TestComponent : IComponentData
    6.     {
    7.         public readonly int Value;
    8.  
    9.         public TestComponent(int value)
    10.         {
    11.             Value = value;
    12.         }
    13.     }
    14.  
    15.     public partial struct OtherTestComponent : IComponentData
    16.     {
    17.         public int Value;
    18.  
    19.         public OtherTestComponent(int value)
    20.         {
    21.             Value = value;
    22.         }
    23.     }
    24.  
    25.     public readonly partial struct TestAspect : IAspect
    26.     {
    27.         private readonly RefRO<TestComponent> test;
    28.  
    29.         public readonly int Value => test.ValueRO.Value;
    30.     }
    31.  
    32.     public partial class BugSystem : SystemBase
    33.     {
    34.         protected override void OnCreate()
    35.         {
    36.             var entity = World.EntityManager.CreateSingleton<TestComponent>();
    37.             World.EntityManager.SetComponentData(entity, new TestComponent(100500));
    38.  
    39.             for (var index = 0; index < 100; index++)
    40.             {
    41.                 var otherEntity = World.EntityManager.CreateEntity(typeof(OtherTestComponent));
    42.                 World.EntityManager.SetComponentData(otherEntity, new OtherTestComponent(index));
    43.             }
    44.         }
    45.  
    46.         protected override void OnUpdate()
    47.         {
    48.             var testAspectEntity = SystemAPI.GetSingletonEntity<TestComponent>();
    49.             var aspect = SystemAPI.GetAspect<TestAspect>(testAspectEntity);
    50.  
    51.             Entities.ForEach((ref OtherTestComponent other) =>
    52.                              {
    53.                                  other.Value += aspect.Value;
    54.                              }).ScheduleParallel();
    55.         }
    56.     }
    57. }
    When system runs I get the following error message:
    I suspect it's somehow related to the implementation of RefRO and RefRW which uses pointers internally:
    upload_2023-6-15_1-27-59.png

    The issue disappears after adding [NativeDisableUnsafePtrRestriction] attribute to the fields of the aspect but it looks hacky:

    Code (CSharp):
    1.  
    2.     public readonly partial struct TestAspect : IAspect
    3.     {
    4.         [NativeDisableUnsafePtrRestriction] private readonly RefRO<TestComponent> test;
    5.  
    6.         public readonly int Value => test.ValueRO.Value;
    7.     }
    I am not 100% sure if it's a bug or intended behaviour but it feels odd that you can easily use aspects in a query but not captured ones.

    If someone has some info about it, please provide it :)
     
    Last edited: Jun 15, 2023
    quieye likes this.
  2. FaithlessOne

    FaithlessOne

    Joined:
    Jun 19, 2017
    Posts:
    264
    While this seems buggy here, you should not use Entities.ForEach, because it is considered outdated. Use idiomatic foreach or jobs when parallel processing is required. Excerpt from the DOTS best practices guide:
    Source: https://learn.unity.com/tutorial/pa...255bedbc2a2e590fbe60#639affd6edbc2a72e6eb288c

    I did not faced any issues using jobs regarding your stated error with the exception that pointers are supplied to the job. Then the error message is totally valid.
     
  3. alior

    alior

    Joined:
    Sep 6, 2014
    Posts:
    24
    Thanks for your response. I'll try to refit my code to new standards.
     
  4. quieye

    quieye

    Joined:
    Jan 19, 2022
    Posts:
    1
    May I ask how you resolved this issue in the end? I have encountered the same problem again.
     
  5. alior

    alior

    Joined:
    Sep 6, 2014
    Posts:
    24
    Hi there! Adding [NativeDisableUnsafePtrRestriction] to the field solved the issue. But actually I rewriten the part so there is no issue for me anymore.
     
    quieye likes this.