Why Entity.ForEach() says the param is generic types can not use?!

this is from unity sample.

struct SpawnSettings : IComponentData, ISpawnSettings
{
    public Entity Prefab { get; set; }
    public float3 Position { get; set; }
    public quaternion Rotation { get; set; }
    public float3 Range { get; set; }
    public int Count { get; set; }
}
......
                                //here I change <T> to <SpawnSettings>
abstract partial class SpawnRandomObjectsSystemBase<SpawnSettings> : SystemBase where SpawnSettings : struct, IComponentData, ISpawnSettings
{
......
    protected override void OnUpdate()
    {
          EntityCommandBuffer ecb = InitEntityEcbS.CreateCommandBuffer();
          EntityCommandBuffer.ParallelWriter ecbParallel = ecb.AsParallelWriter();

line227     Entities
line228    .ForEach((Entity e, int entityInQueryIndex, ref SpawnSettings spawnSettings) =>
           {
                 //Initiate Entities.
                 ......
           }
    }
}

I got only these 3 errors, I modify the original code for compiled successfully.
Assets\Scripts\SpawnRandomObjectsAuthoring.cs(228,65): error DC0050: Type SpawnSettings cannot be used as an Entities.ForEach parameter as generic types and generic parameters are not currently supported in Entities.ForEach
Assets\Scripts\SpawnRandomObjectsAuthoring.cs(227,17): error DC0053: Entities.ForEach cannot be used in system SpawnRandomObjectsSystemBase as Entities.ForEach in generic system types are not supported.
Assets\Scripts\SpawnRandomObjectsAuthoring.cs(227,17): error DC0004: Entities.ForEach Lambda expression captures a non-value type ‘this’. This is only allowed with .WithoutBurst() and .Run()

You can’t use Entities.ForEach inside generic systems. You’d need to write the IJobEntityBatch yourself

1 Like

Uh, what is the so-called generic system? Is that OnUpdate()? And I will check about IJobEntityBatch,Thanks a lot.

I read the article “Using Entity Batch jobs” in the Unity official manual. The “IJobEntityBatch” just like I tried another method that is “IJobParallelFor” struct.
I’m just worried that “IJobEntityBatch” struct like “IJobParallelFor” struct can’t do structural change operations like: Create Entity, DestroyEntity, Add Component, Remove Component, Modify SharedComponent, etc.
So, is that “IJobEntityBatch” really works? When my Jobs need to Initialize a large number of entities? In my try I failed on IJobParallelFor’s EntityManager.Instantiate(…).

IJobEntityBatch can do structural changes but only through EntityCommandBuffer just like the Entities.ForEach() that you wrote.

3 Likes