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. Dismiss Notice

Question Best practice for scheduling jobs with additional filters

Discussion in 'Entity Component System' started by ts_headfirst, Sep 18, 2023.

  1. ts_headfirst

    ts_headfirst

    Joined:
    Aug 1, 2023
    Posts:
    24
    Hi,

    There seems to be two ways to specify additional filters to a job query.
    1. Apply attributes to the job itself
    2. Pass an EntityQuery to the Schedule method
    I personally prefer the latter as it reuses the query in both the update condition and the actual query, but my question is which of the two is best practice and why?

    Examples of both approaches:
    1. Attributes

    Code (CSharp):
    1.        
    2.         [BurstCompile]
    3.         public void OnUpdate(ref SystemState state)
    4.         {
    5.             new MoveJob
    6.             {
    7.                 deltaTime = SystemAPI.Time.DeltaTime
    8.             }.ScheduleParallel();
    9.         }
    10.  
    11.         [BurstCompile]
    12.         [WithAll(typeof(Simulate))]
    13.         private partial struct MoveJob : IJobEntity
    14.         {
    15.             public float deltaTime;
    16.  
    17.             private void Execute(ref LocalTransform transform, in MoveDirection dir, in MoveSpeed speed)
    18.             {
    19.                 transform.Position += dir.direction * speed.speed * deltaTime;
    20.             }
    21.         }
    2. EntityQuery

    Code (CSharp):
    1.         private EntityQuery _movablesQuery;
    2.      
    3.         [BurstCompile]
    4.         public void OnCreate(ref SystemState state)
    5.         {
    6.             _movablesQuery = SystemAPI.QueryBuilder()
    7.                 .WithAll<Simulate, LocalTransform, MoveSpeed, MoveDirection>()
    8.                 .Build();
    9.          
    10.             state.RequireForUpdate(_movablesQuery);
    11.         }
    12.  
    13.         [BurstCompile]
    14.         public void OnUpdate(ref SystemState state)
    15.         {
    16.             new MoveJob
    17.             {
    18.                 deltaTime = SystemAPI.Time.DeltaTime
    19.             }.ScheduleParallel(_movablesQuery);
    20.         }
    21.  
    22.         [BurstCompile]
    23.         private partial struct MoveJob : IJobEntity
    24.         {
    25.             public float deltaTime;
    26.      
    27.             private void Execute(ref LocalTransform transform, in MoveDirection dir, in MoveSpeed speed)
    28.             {
    29.                 transform.Position += dir.direction * speed.speed * deltaTime;
    30.             }
    31.         }
     
    Last edited: Sep 18, 2023
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    943
    There's no best practice really. That depends on your needs. If you need the EntityQuery, then use the one with the EntityQuery. Personally, I would avoid the "magic" ways as much as I can like IJobEntity and Entities.ForEach(). I would use the code where they are generated to which is IJobChunk. They have their uses for simple jobs but jobs in production code is rarely simple.
     
    WAYNGames and ts_headfirst like this.