Any way to schedule a series of Jobs in array?

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’

        using Unity.Jobs;
        using static Unity.Entities.JobForEachExtensions;

        public IBaseJobForEach[] Jobs = {new xJob(), new yJob()};

        public JobHandle ScheduleJobs()
        {
            foreach (var job in Jobs)
            {
                new SleepConsiderJob().Schedule();
            }
        }

Any suggestion would be appreciated!

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.

1 Like

When i need to combine several jobs (more than 3) i use this:

    public static JobHandle CombineDependencies(params JobHandle[] jobs) {
      var deps = default(JobHandle);
      for (var i = 0; i < jobs.Length; ++i)
        deps = JobHandle.CombineDependencies(deps, jobs[i]);
      return deps;
    }
1 Like

Combinedependencies takes a nativearray

1 Like

That’s right. Forgot about it. :slight_smile:

Your code works fine! Thanks!