#Strange EntityCommandBuffer behaviour

1 messages · Page 1 of 1 (latest)

drifting meadow
#

Hi, I have the following loop which resides in OnUpdate in a System.
It makes an EntityCommandBuffer, loops over a certain set of entities, uses the buffer to remove two components and add one, does other stuff, and then plays back the ecb after the loop.
The strange behaviour im encountering, is the ECB actions are only applying for the very first iteration of the loop, on the very first frame. The entities still get looped over, and everything else in the loop (all the mesh stuff) still happens and works like normal, but only the very first entity on the very first frame gets it's ChunkUnrenderedTag and ChunkQueuedForRenderTag removed and ChunkRenderedTag added. Am I using the entitycommandbuffer wrong here?

Code:

 //register meshes
 EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.Temp);
 foreach ((RefRO<Chunk> chk, RefRO<ChunkUnrenderedTag> cut, RefRO<ChunkQueuedForRenderTag> cqfrt, RefRO<ChunkMeshData> CMD, RefRW<MaterialMeshInfo> mmi) in SystemAPI.Query<RefRO<Chunk>, RefRO<ChunkUnrenderedTag>, RefRO<ChunkQueuedForRenderTag>, RefRO<ChunkMeshData>, RefRW<MaterialMeshInfo>>())
 {
     ecb.RemoveComponent<ChunkUnrenderedTag>(chk.ValueRO.selfEntity);
     ecb.RemoveComponent<ChunkQueuedForRenderTag>(chk.ValueRO.selfEntity);

     Mesh mesh = new Mesh();

     List<Vector3> vertices = new List<Vector3>();
     List<int> triangles = new List<int>();

     for (int i = 0; i < CMD.ValueRO.vertices.Length; i++)
     {
         vertices.Add(new Vector3(CMD.ValueRO.vertices[i].x, CMD.ValueRO.vertices[i].y, CMD.ValueRO.vertices[i].z));
     }

     for (int i = 0; i < CMD.ValueRO.triangles.Length; i++)
     {
         triangles.Add(CMD.ValueRO.triangles[i]);
     }

     mesh.vertices = vertices.ToArray();
     mesh.triangles = triangles.ToArray();

     Material material = new Material(Shader.Find("Universal Render Pipeline/Lit"));

     EntitiesGraphicsSystem egs = state.World.GetExistingSystemManaged<EntitiesGraphicsSystem>();
     BatchMeshID meshIndex = egs.RegisterMesh(mesh);
     BatchMaterialID materialIndex = egs.RegisterMaterial(material);

     Debug.Log($"bmid {meshIndex}");

     mmi.ValueRW.Mesh = (int)meshIndex.value;
     mmi.ValueRW.Material = (int)materialIndex.value;

     ecb.AddComponent<ChunkRenderedTag>(chk.ValueRO.selfEntity);
 }

 ecb.Playback(state.EntityManager);
#

for additional context, every frame 4 new chunks get their ChunkQueuedForRenderTag (already have their ChunkUnrenderedTag)
And that's how I know, as the entities queried each frame grows by 4 each frame, except for the first frame (3, as the first one works), when they should instead be getting dealt with immediately by the logic in the query

obsidian crag
#

I presume the value of Chunk.selfEntity is originally on a prefab entity, referring to itself. This won’t be changed when instantiating the entity. The appropriate way to identify the current entity is by using SystemAPI.Query’s WithEntityAccess.

drifting meadow
#

let me try changing that