Search Unity

Any way to schedule a series of Jobs in array?

Discussion in 'Entity Component System' started by eterlan, Oct 22, 2019.

  1. eterlan

    eterlan

    Joined:
    Sep 29, 2018
    Posts:
    177
    Hi, I have some jobs, which are similar & parameterless. So I want to put them into an array to schedule and then combine the dependency, instead of doing it one by one.

    I try this, but the complier complain that:
    The type 'Unity.Entities.JobForEachExtensions.IBaseJobForEach' must be a non-nullable value type in order to use it as parameter 'T'
    Code (CSharp):
    1.  
    2.         using Unity.Jobs;
    3.         using static Unity.Entities.JobForEachExtensions;
    4.  
    5.         public IBaseJobForEach[] Jobs = {new xJob(), new yJob()};
    6.  
    7.         public JobHandle ScheduleJobs()
    8.         {
    9.             foreach (var job in Jobs)
    10.             {
    11.                 new SleepConsiderJob().Schedule();
    12.             }
    13.         }
    Any suggestion would be appreciated!
     
  2. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    Storing a struct in an array typed as an interface will result in a boxing conversion, essentially wrapping it in a reference type. The extension method has no way of knowing that whats in there is actually a value type and the Schedule extension requires a value type.

    Set the type of your array to the actual type of the job, not the IJob interface.
     
    Last edited: Oct 22, 2019
    eterlan likes this.
  3. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    When i need to combine several jobs (more than 3) i use this:
    Code (CSharp):
    1.     public static JobHandle CombineDependencies(params JobHandle[] jobs) {
    2.       var deps = default(JobHandle);
    3.       for (var i = 0; i < jobs.Length; ++i)
    4.         deps = JobHandle.CombineDependencies(deps, jobs[i]);
    5.       return deps;
    6.     }
     
    eterlan likes this.
  4. sngdan

    sngdan

    Joined:
    Feb 7, 2014
    Posts:
    1,154
    Combinedependencies takes a nativearray<jobhandle>
     
    GilCat likes this.
  5. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    That's right. Forgot about it. :)
     
  6. eterlan

    eterlan

    Joined:
    Sep 29, 2018
    Posts:
    177
    Your code works fine! Thanks!