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. Dismiss Notice

Setting component data with a jobHandle?

Discussion in 'Entity Component System' started by crener, Aug 14, 2021.

  1. crener

    crener

    Joined:
    Aug 29, 2015
    Posts:
    27
    I've been messing with ECS again after a while and I have a long running task (it takes about 60% of the total frame time) and while that is being calculated it is possible for other systems that don't have a dependency to run. I noticed a bug in the long running system which requires me to wait for the job to finish and directly assign a component value to an Entity. As far as I can tell I have to wait for the handle to complete then set the value like this:
    Code (CSharp):
    1. MainJob job = new MainJob();
    2. JobHandle jobHandle = job.Schedule(Dependency);
    3.  
    4. //Super slow, but unavoidable?
    5. jobHandle.Complete();
    6.  
    7. // Have to wait in order to get access to the complete content of the result
    8. EntityManager.SetComponentData(m_entity, new JobResultData{FrameResult = job.Result});
    9.  
    Is there some trick to setting the component data automatically with a jobHandle so that other Systems can run as this one is finishing up? Maybe something like this where the dependency is passed directly into the component set (yes, this isn't an API atm but one can dream):
    Code (CSharp):
    1. MainJob job = new MainJob();
    2. Dependency = job.Schedule(Dependency);
    3.  
    4. // Allow system update to finish and start down stream systems immediately without blocking
    5. EntityManager.SetComponentData(m_entity, new JobResultData{FrameResult = job.Result}, Dependency);
    6.  
    I know that this might not be ideal because I have to create a new IComponent instance but if there is some way to do this I can just do the creation as part of the job. This would allow other threads to have work queued up as this job isn't parallel-able.
     
    Last edited: Aug 14, 2021
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,983
    Code (CSharp):
    1. Job.WithCode(() => { SetComponent(m_entity, new JobResultData{FrameResult = job.Result}); }).Schedule();
     
    Krajca and crener like this.
  3. crener

    crener

    Joined:
    Aug 29, 2015
    Posts:
    27
    Ooooooh that's really cool.