Question regarding the newly announced Lambda API inside of JobComponentSystem

It’s really nice to see Unity working on reducing the boilerplates in writing JobComponentSystems. It’s possibly the biggest pain point in our project.

My question is: how will ComponentDataFromEntity (or just passing data to Jobs in general) work with Entities.ForEach in a JobComponentSystem (if at all supported)?

2 Likes

If you are using some data or things like ComponentDataFromEntity inside a lambda, I don’t see a reason why it won’t be possible to pass it to the generated job. Basically if you can write it, the code generator can as well. But I was also wondering and would like to hear from devs.

I would assume it works similar to how it does in a regular component system. Declare some variable outside the lambda your ComponentDataFromEntity in OnUpdate and then later use it in the ForEach.

Ah yes, you guys are right. I keep forgetting they are using code gen. On the back of my mind, Entities.ForEach always had some problems and gotchas. For example, GC allocations, and editor crash caused by this:

Entities.WithAllReadOnly<Element1, Foo1>().ForEach((Entity entity, DynamicBuffer<Element1> element1, ref Foo1 f1, ref Foo2 f2) =>
{

});

I remember there was a long thread about the shortcomings of the current ForEach API (can’t find it atm). So it’ll be really nice if Unity can fix all these in a near-future version for both JobComponentSystem and regular ComponentSystem.

1 Like

This announcement surprised me quite a bit. I’m not sure if I will like it. It seems the behavior is going to be really confusing if you use Entities.ForEach to do stuff like iterating over hybrid components or scheduling jobs to initialize prefab instances of a spawner or tagging the player in first. I do like the idea of lambdas and fluent API, but I also really don’t like assumptions.