A while back we deprecated IJobForEach. This was done due to make sure that we weren’t continuing to support an interface that would have performance issues and could not be supported easily with DOTS Runtime. The two remaining solutions however - Entities.ForEach and IJobChunk - did not provide a middle ground for writing jobs that deal with entities and their components. You either wrote your code in an inline lambda or you wrote all of the per-entity iteration code on your own in an IJobChunk.
IJobEntity would provide a way to define entity iteration for users wanting similar functionality to IJobForEach. With this interface, it would be possible to define the job and query separate from an individual system and use it in multiple places. It is also built on much simpler codegen than Enties.ForEach. The job type is a separate type that is unmodified by codegen (a surrounding IJobChunk is generated that uses it). This allows better scaffolding to IJobChunk and more understandable generated code.
The interface has no abstract (or other) methods. Users can define a OnUpdate method that will get called for every entity that matches the query generated for the job. Queries are specified with the method parameters of OnUpdate (along with usage of ‘ref’, ‘in’, etc. similar to Entities.ForEach) as well as for any optional attributes placed on the OnUpdate method ([AllInQuery], [AnyInQuery], [NoneInQuery], etc.).
// EXAMPLE : RotationSpeedSystem
// User defined Job Type
[BurstCompile]
struct RotateEntityJob : IJobEntity
{
public float DeltaTime;
public void OnUpdate(ref Rotation rotation, in RotationSpeed_ForEach speed)
{
rotation.Value = math.mul(math.normalize(rotation.Value), quaternion.AxisAngle(math.up(), speed.RadiansPerSecond * DeltaTime));
}
}
// System that schedules the Job to run with parallel execution
public class RotationSpeedSystem : SystemBase
{
protected override void OnUpdate()
{
var rotateEntityJob = new RotateEntityJob() { DeltaTime = Time.DeltaTime };
Entities.OnUpdate(rotateEntityJob).ScheduleParallel();
}
}
Advantages over Entities.ForEach include:
Easier code re-use
Not built on lambdas (which some users clearly love and some…do not)
Generated code looks more similar to user written IJobChunk code (viewable through DOTS Compiler Inspector)
Can support methods with any combination of parameters (no need for predefined set of delegates like Entities.ForEach)
Disadvantages are:
No automatic capturing/writing back to local variables (they would need to be manually set on job struct)
Some things will still need to be defined with attributes (BurstCompile, optional EntityQuery components and ones not defined in OnUpdate params)
No patching of entity access through SystemBase methods (such as how we allow GetComponent/SetComponent inside of Entities.ForEach)
That looks like a great solution. I would use it for 90% of my entity jobs. I think the disadvantages are not really significant and worth the sacrifice.
Agreed with the statement above. The disadvantages are very minor and won’t affect many users that are still on IJobChunk or IJobForeach.
One side note IJobEntity name is not as specific as IJobForeach, maybe something like IJobBase, IJobUpdate, or even IJobForeach itself just without generic parameters will be helpful to slowly deprecate generic version of it.
I love the interface name. That is about as intuitive of an interface name as you could get, as it compares itself well to IJobChunk.
I don’t like the interface’s method being called OnUpdate instead of Execute. OnUpdate is for systems. Similarly, invoking the job also could use a better name, such as reusing ForEach, or maybe Entities.Job or something.
Seems like it’s basically an IJobChunk that skips all the boilerplate
Would it be possible to also have a “OnBeginChunk()” function that we can implement aside from OnUpdate()? To give us an opportunity to do some per-chunk logic before we iterate on all entities of the chunk? I feel like we’d almost never need to use IJobChunks again if we had that (for use cases like these , it would be better than working with a threadIndex at every entity iteration, possibly)
Nice to see, that Unity heard community Disadvantages really negligible
Couple of small things which will be useful:
Explicit query usage (same many people want fot Entities.ForEach), when we can explicitly define query, set types and access rules, and use that query as argument for that jobs (and Entities.ForEach) for reducing WithAll (AllInQuery) WithNone (NoneInQuery) WithAny (AnyInQuery) (and that namings, maybe lead them to one denominator? )
About ref, extend it somehow if possible (through codegen and code analysis maybe) for check if we really write to component (like with setter of ComponentDataFromEntity, chunk.GetNativeArray) for bumping chunk version and not just bump it every time just by definition. It will make life easier for change filtering
Can be a bit confusing for newcomers, because we have IJobFor IJobEntity IMO best naming for that type of job.
I also agree with this point. Well Execute (for me) sounds natural and represent behaviour which I expect - execute that block of code.
Pretty much what I think most of us wanted. I also don’t think OnUpdate is a good name. It has strong historical ties to the gameloop and I think it’s best kept reserved for that.
Is there a reason it cant reuse the same scheduling as chunk/old ijobforeach, ie Dependency = new Job().ScheduleParallel(myQuery, Dependency)? I personally prefer that over the proposed Entities.WithXXX.OnUpdate(job) etc
I generally prefer Phil’s suggestion with Entities.ScheduleParallel(rotateEntityJob), it makes it easier to read and see what it’s doing at a glance.
With IJobEntity’s OnUpdate(params), the OnUpdate method kind of picks at me since Execute(…) is so much more familiar, since IJob/IJobParallelFor/IJobChunk still use it.
I’m not too much of a fan with Attributes in function params, I find that syntax to be difficult to read, so being able to use entity queries alongside scheduling would be very nice addition.
This is looking pretty good, still needs an OnUpdate function that would give you Entity and iteration Index so same as IJobForEachWithEntity. I use that a lot!
The name is is clear. If IJobChunk is a job over a chunk, IJobEntity is a job over an entity. Is consistent.
Use Execute() consistently with other job types.
Also I know this is unrelated but I don’t think deserves a separate post… I find Job.WithCode to be a pretty bad name. Everything is code so it’s a meaningless name. I think Job.Execute ( lambda) would be clearer in intent (execute what I’m passing you) and consistent with other job-related terminology.
Sounds like Execute() is much preferred over OnUpdate() for the job method to be called. I think this makes a lot of sense.
It is important to be able to supply/reuse an optional query from the scheduling site (this goes along with thinking of an addition to Entities.ForEach as well).
Having a OnBeginChunk/OnBegin/OnPost methods would be handy (probably not going to be in first iteration, but noted).
We should be able to define Execute methods that take an entity and entity/thread indices (this is a given).
Attributes on method params aren’t great if we can avoid them and it would be nice to actually check to see if we write to writable params.
I think the following is what we are leaning towards as far as what the scheduling invocation would look like:
// Use Execute method (Fetch from parameters + attributes)
var rotateEntityJob = new RotateEntityJob() { DeltaTime = Time.DeltaTime };
Entities.Execute(rotateEntityJob).ScheduleParallel();
// Allow WithQuery on all Entities.ForEach and Execute etc
var rotateEntityJob = new RotateEntityJob() { DeltaTime = Time.DeltaTime };
Entities.WithQuery(myQuery).Execute(rotateEntityJob).ScheduleParallel();
// Allow WithAll both Entities.ForEach and Execute
var rotateEntityJob = new RotateEntityJob() { DeltaTime = Time.DeltaTime };
Entities.WithAll<AnotherRequiredComponent>.Execute(rotateEntityJob).ScheduleParallel();
Also, just to be clear, this is still just a design for an API. Internal buy-off needs to happen and the work needs to be complete before this is a reality. From all the feedback, it sounds like this would be a pretty significant improvement however.
I think it looks great! I more or less create these kinds of structs already using Entities.ForEach, but the fact that we need to write the method declaration inside of the ForEach and invoke a method in the struct manually makes it tedious. This would simplify a lot.
Super pumped about this! Thanks for making sure this is implemented. I think it is a good idea to allow the rest of the Lamba stuff on these ForEachEntity structs. I think having the Lambas override attributes on the ForEachEntity would make the most sense, for example if your lambda said to include ComponentA but the ForEachEntity said to exclude ComponentA or something.
I would say they should be exclusive. Either you specify attributes, or you specify WithAll, WithNone.
Both of them are relative to the parameter list of the Execute method.
When both attributes & WithAll are defined we give a compile error. I think if both are used. It will be exceptionally unintuitive to understand what it will actually happen. So better prevent it.