Search Unity

Having a job that makes structural changes run across multiple frames

Discussion in 'Entity Component System' started by Abbrew, Sep 21, 2019.

  1. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    I currently have a long-running single-threaded pathfinding IJob that at its end adds a PathfindingComplete component to the pathfinder entity. I have the resulting JobHandle added to EndSimulationEntityCommandBufferSystem. Right now it appears that during the end of the Simulation phase JobHandle.Complete is called on the pathfinding job's JobHandle, forcing the intensive pathfinding job to complete in one frame. If this is the case, is there a way for a long running job to run across multiple frame while also writing to an ECB?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You have to manage the job handle yourself and don't pass it back to the job system and it won't complete. This means your ECB will have to be handled by you as well (not from a barrier). That said it's unwise to use a ECB in a multi frame job anyway as there is usually no guarantee that the Entity exists, has the same components etc.

    Generally if you need to do a multi frame job you would.

    Copy required data from entities into containers.
    Work on data over as many frames as you want.
    Copy data back to entities, checking the entity is still valid.

    That all said, I often find it simply easier when an expensive job manipulates an entity to instead of having a job run over multiple frames, instead limit the maximum number of entities the job can work on and do it in a frame. Reduces a lot of complexities.
     
    Antypodish likes this.
  3. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Thank you for the detailed response. Taking your advice into account, I'm having a Monobehavior do the scheduling of the pathfinding job, waiting, and completing the jobhandle some time later, as well as handling how many pathfinding jobs to schedule in a timeframe. The change is that the job will only perform calculates upon a weights array, and an EntityManager is used to attach a PathfindingCompleteMessage after JobHandle.Complete. A System that looks for PathfindingStartMessage one entity at a time will calculate those weights in one frame using Unity.Physics queries. This way all the calculations dependent on the world is done in the system, and the expensive pathfinding is done in the Monobehavior. Still though, it would be nice if the Unity.Physics queries can be used across multiple frames in a job. BuildPhysicsWorld is deallocated every frame...