Search Unity

How do you pause or stop running the job?

Discussion in 'Entity Component System' started by castor76, Jul 31, 2019.

  1. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    I have basic ECS job system up and running which is great, but they all run without any "run" or "execute" command from main script. The JobComponentSystem runs without any scripting. It has OnUpdate method that schedules the its own job but there is nothing I can do to control it.

    If I want the game to stop or pause executing this JobComponentSystem, how do I go on about doing that?

    Code (CSharp):
    1. public class TreeMoveSystem : JobComponentSystem
    2. {
    3.     [BurstCompile]
    4.     private struct UpdateTreeMoveJob : IJobForEach<Translation, MoveSpeedComponent>
    5.     {
    6.         public float deltaTime;
    7.  
    8.         public void Execute(ref Translation translation, ref MoveSpeedComponent moveSpeedComponent)
    9.         {
    10.             translation.Value.y += moveSpeedComponent.moveSpeed * deltaTime;
    11.  
    12.             if (translation.Value.y > 10f)
    13.             {
    14.                 moveSpeedComponent.moveSpeed = -math.abs(moveSpeedComponent.moveSpeed);
    15.             }
    16.  
    17.             if (translation.Value.y < -10f)
    18.             {
    19.                 moveSpeedComponent.moveSpeed = math.abs(moveSpeedComponent.moveSpeed);
    20.             }
    21.         }
    22.     }
    23.  
    24.    
    25.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    26.     {
    27.         UpdateTreeMoveJob updateTreeMoveJob = new UpdateTreeMoveJob
    28.         {
    29.             deltaTime = Time.deltaTime
    30.         };
    31.  
    32.         JobHandle jobHandle = updateTreeMoveJob.Schedule(this);
    33.  
    34.         return jobHandle;
    35.     }  
    36. }
    So basically if I have this script in the project, it just runs... no matter what.
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Ir runs first time always, cause you not declare EntityQuery explicitly, and IJobForEach generates it at first Schedule, after that system runs by query All: (Translation, MoveSpeedComponent) if entities match query exists.
     
  3. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    JobComponentSystems have an Enabled property that you can set to False. (P.S: This corresponds to the checkbox left of the system in the Entity Debugger)
     
    Last edited: Jul 31, 2019
  4. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Oh... Enabled property seems to be promising.. Also got to learn about query. It is not so easy to find info about ECS ..
     
  5. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
  6. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    Personally to get a job to run once I just incremented an integer in the onupdate loop and checked if it was equal to one then set this.enabled to false. I don't know if its a good way of doing, but its seems ok to me you can get a job simply to loop as many times as you want with this way.

    I've also heard about using event entities where you create an entity with an component, and use that to schedule jobs on creation and stop them on destruction. I havent tried that method myself yet though.