Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question how to Update PhysicsVelocity.Linear datas via multiple scheduled jobs?

Discussion in 'Entity Component System' started by Patrick-Cho, Mar 14, 2023.

  1. Patrick-Cho

    Patrick-Cho

    Joined:
    Jun 6, 2017
    Posts:
    8
    Hi I want to separate the entity behaviors in detail as possible (if i can..)

    for example a homing missile which accelerates has two functions(homing, accelerating)
    so i want to make AccelerateSystem, GuidingSystem.
    and each systems have jobs like below

    i'm using Entities 1.0.0-pre.44 version.

    Code (CSharp):
    1.  
    2.     [StructLayout(LayoutKind.Auto)]
    3.     public partial struct GuidingSystemJob : IJobEntity
    4.     {
    5.         public EntityCommandBuffer.ParallelWriter ECB_Parallel;
    6.         [ReadOnly] public float DeltaTime;
    7.  
    8.         [BurstCompile]
    9.         public void Execute(Entity entity, GuidingCompData guidingData, in PhysicsVelocity physicsVelocity, [EntityIndexInQuery] int sortKey)
    10.         {
    11.             var newPhysicsVelocity = physicsVelocity
    12.  
    13.              ... Do Rotate newPhysicsVelocity To Target Here
    14.  
    15.             ECB_Parallel.SetComponent(sortKey, entity, newPhysicsVelocity);
    16.             ECB_Parallel.SetComponent(sortKey, entity, guidingData);
    17.         }
    18.     }
    19.  
    20.     public partial struct AccelerateSystemJob : IJobEntity
    21.     {
    22.         public EntityCommandBuffer.ParallelWriter ECB_Parallel;
    23.         [ReadOnly] public float DeltaTime;
    24.  
    25.         [BurstCompile]
    26.         public void Execute(Entity entity, AccelerateCompData accelerateData, in PhysicsVelocity physicsVelocity, [EntityIndexInQuery] int sortKey)
    27.         {
    28.             var newPhysicsVelocity = physicsVelocity;
    29.  
    30.             ... Do Accelerating newPhysicsVelocity Here
    31.  
    32.             ECB_Parallel.SetComponent(sortKey, entity, newPhysicsVelocity);
    33.             ECB_Parallel.SetComponent(sortKey, entity, accelerateDat);
    34.         }
    35.     }
    36. //================================================================================
    37.     public partial struct GuidingSystem : ISystem
    38.     {
    39.         [BurstCompile]
    40.         public void OnUpdate(ref SystemState state)
    41.         {
    42.             if (SystemAPI.TryGetSingleton<EndSimulationEntityCommandBufferSystem.Singleton>(out var EndSimulatorECBSystem) == false)
    43.                 return;
    44.  
    45.             var ECB = EndSimulatorECBSystem.CreateCommandBuffer(state.WorldUnmanaged);
    46.             var deltaTime = state.WorldUnmanaged.Time.DeltaTime;
    47.  
    48.             JobHandle jobHandle;
    49.  
    50.             jobHandle = new GuidingSystemJob
    51.             {
    52.                 ECB_Parallel = ECB.AsParallelWriter(),
    53.                 DeltaTime = deltaTime,
    54.             }.ScheduleParallel(state.Dependency);
    55.  
    56.             state.Dependency = jobHandle;
    57.         }
    58.     }
    59.  
    AcceleratingSystem is almost identical to GuidingSystem


    The problem is when the jobs executes, PhyscisVelocity data is equal on Guiding and Accelerating.
    and the latest PhysicsVelocity data registered in ECB will be written to the entity component.

    i also tried separating each systems to different system groups like (InitializationSystemGroup, SimulationSystemGroup, PresentationSystemGroup)

    i thought ECB play backs would be called on end of System Groups.
    i must be misunderstanding the whole system

    So is there any method to make it work?

    i know i can just merge both function into a single job or make the code unscheduled and run procedurally.
    that will solve the problem.

    but i am figuring out a way to keep my code organized in detail and performant
     
  2. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    217
    You can use direct component data modification instead of ECBs. Your code does not introduce sync points, so I am not seeing obstacles for such simpler method. Then by ordering systems you will enforce correct ordering of your jobs.
     
  3. Patrick-Cho

    Patrick-Cho

    Joined:
    Jun 6, 2017
    Posts:
    8
    thank you for your reply i think i missed the concept of using ECB
    it's much useful on destroy, initializing or ect... than setting components