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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

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


    Jun 6, 2017
    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):
    2.     [StructLayout(LayoutKind.Auto)]
    3.     public partial struct GuidingSystemJob : IJobEntity
    4.     {
    5.         public EntityCommandBuffer.ParallelWriter ECB_Parallel;
    6.         [ReadOnly] public float DeltaTime;
    8.         [BurstCompile]
    9.         public void Execute(Entity entity, GuidingCompData guidingData, in PhysicsVelocity physicsVelocity, [EntityIndexInQuery] int sortKey)
    10.         {
    11.             var newPhysicsVelocity = physicsVelocity
    13.              ... Do Rotate newPhysicsVelocity To Target Here
    15.             ECB_Parallel.SetComponent(sortKey, entity, newPhysicsVelocity);
    16.             ECB_Parallel.SetComponent(sortKey, entity, guidingData);
    17.         }
    18.     }
    20.     public partial struct AccelerateSystemJob : IJobEntity
    21.     {
    22.         public EntityCommandBuffer.ParallelWriter ECB_Parallel;
    23.         [ReadOnly] public float DeltaTime;
    25.         [BurstCompile]
    26.         public void Execute(Entity entity, AccelerateCompData accelerateData, in PhysicsVelocity physicsVelocity, [EntityIndexInQuery] int sortKey)
    27.         {
    28.             var newPhysicsVelocity = physicsVelocity;
    30.             ... Do Accelerating newPhysicsVelocity Here
    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;
    45.             var ECB = EndSimulatorECBSystem.CreateCommandBuffer(state.WorldUnmanaged);
    46.             var deltaTime = state.WorldUnmanaged.Time.DeltaTime;
    48.             JobHandle jobHandle;
    50.             jobHandle = new GuidingSystemJob
    51.             {
    52.                 ECB_Parallel = ECB.AsParallelWriter(),
    53.                 DeltaTime = deltaTime,
    54.             }.ScheduleParallel(state.Dependency);
    56.             state.Dependency = jobHandle;
    57.         }
    58.     }
    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


    Dec 14, 2022
    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


    Jun 6, 2017
    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