Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to stop, start and interrupt JobComponentSystems

Discussion in 'Entity Component System' started by calabi, Jul 16, 2019.

  1. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    Sorry for the basic question. I've looked for several days, and cant get adequate examples or well explained code that I can get my head around. I have the below code just for test purpose which runs all the time on 1000's of entitys.

    Code (CSharp):
    1. public class DeductWagesfrom : JobComponentSystem
    2. {
    3.     struct DeductWagesjob : IJobForEach<MoneyEntityInfo>
    4.     {
    5.  
    6.         public void Execute(ref MoneyEntityInfo maney)
    7.         {
    8.             maney.MoneyHeld += 100;
    9.  
    10.         }
    11.  
    12.     }
    13.        
    14.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    15.     {
    16.         var unschedulejaab = new DeductWagesjob()
    17.         {
    18.  
    19.  
    20.         }.Schedule(this, inputDeps);        
    21.        
    22.         return unschedulejaab;      
    23.     }
    24.  
    25. }
    I understand you can use the World.Active.GetExistingSystem to get access to the system externally, I also understand you can use the Updateafter method above it or whatever. But I don't really understand how the JobComponentSystem works and runs. Like it has to return a jobhandle, but how do I return a jobhandle that doesn't make it run immediately. What if I want it to run according to an external ComponentSystem that has an input, or I want it run according to certain ticks rates in another script or after another event. How do I make it run only once? I'm trying to understand the flow and control of how the JobComponentSystem works.
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,222
    OnUpdate runs on the main thread. Think of it like a MonoBehaviour's Update. When you schedule a job, you get a JobHandle. That doesn't make the job immediately finish. It just let's you keep track of it. JobComponentSystem requires you to return the combined JobHandle of all the jobs you scheduled in OnUpdate. It does that so that it can chain all your jobs together so that they can run alongside OnUpdate calls of other systems.
     
  3. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    Thanks for the reply. So I'm guessing I need to find another way of controlling the flow. Like how do I know when the jobsytem has done one loop through all the required entity's. I've seen lots of different answers for how to control the flow of these jobsystems.

    Like adding a component to the entities I want to do the job with(I kind of want a lot of entities at least 1 million and more if able). That seems like non perform-ant and kind of a waste to me?

    I could add an inputcomponent type to all these entities that I want to control with an input?

    I could use create an entity and use that as an event somehow?

    I dont really have a clue how to stop these jobcomponentysystems when I want.

    Sorry for such a basic questions but its kind of difficult for me to understand. Like I used the same code with the simple job system using nativearrays in a monobehaviour, and it works as I expect, it has a schedule, it runs through the nativearray once and then I use job.complete and it finishes with one loop. I'm kind of struggling with the jobcomponentsystem in comparison. Do I have to get an external jobhandle to the jobsystem, so I can stop and start it from the main thread?
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,222
    Try using a ComponentSystem first and see if you can do what your MonoBehaviour does with that (including scheduling and completing the jobs). From there I think the transition from ComponentSystem to JobComponentSystem will be more intuitive.

    Another approach is to be as specific as possible about the problem you are trying to solve/understand.
     
  5. calabi

    calabi

    Joined:
    Oct 29, 2009
    Posts:
    232
    Thanks yeah I guess I'll do that, I'm reading a book on DOTS as well hopefully I'll understand it eventually.