CommandBuffer.AppendToBuffer<>() doesn't execute before the next job call

I noticed while using CommandBuffer.AppendToBuffer<>() there’s a chance that the buffer wasn’t appended before the next call to the job. This causes my code to create unpredictable segments. Is this intended or am i supposed to do something else to make sure the IBufferElement is added before the next call to the job?

There’s not really enough information here to help you. If you’re scheduling your jobs the normal way this shouldn’t be possible. You’d need to show the code.

I have a normal IJob and initialize it with this.

var MoveSpawnJobBHandle = new MoveSpawnJobB
                    {
                        BufferHandle = GetBufferFromEntity<EntitySectionBuffer3V>(true),
                        collisionWorld = collisionWorld,
                        commandBuffer = commandBuffer,
                        PhysicsVelocityGroup = GetComponentDataFromEntity<PhysicsVelocity>(true),
                        MoveIdentifierGroup = GetComponentDataFromEntity<MoveIdentifier>(true),
                        MovesToSpawn = MovesToSpawn, // <- premade native list
                        TranslationGroup = GetComponentDataFromEntity<Translation>(true),
                        entities = MoveQuery.ToEntityArray(Allocator.TempJob),
                        debug = true,
                        verbose = true,
                        spawnLimit = 2
                    }.Schedule(Dependency);
                    MoveSpawnJobBHandle.Complete();
                    m_EndSimulationEntityCommandBufferSystem.AddJobHandleForProducer(MoveSpawnJobBHandle);

the job loops through the entities and appends to the buffer using an EntityCommandBuffer. i use the CommandBuffer grabbed from the EndSimulationEntityCommandBufferSystem system.

The job creates an entity and a joint that joins the newly created entity to the entity at the end of the buffer. the result is a rope-like object connected by joints. The code works fine (I’ve tested it on the main thread using EntityManager instead of EnittyCommandBuffer).

here’s the code i use to append the buffer

commandBuffer.AppendToBuffer( entities[i], new EntitySectionBuffer3V
                                                    {
                                                        EntityA = data.sender, //entity grabbed from the native list MovesToSpawn
                                                        EntityB = CurrentStringShotEntity, // entity crated using the command buffer
                                                        value = data.spawnType, // int value
                                                        EntityC = joint // joint entity created using the command buffer
                                                    });

I did some more tests and it really does seem that the 2 jobs are executing on the same frame causing them to grab the same data and act according causing a duplicate.

6678319--765274--upload_2021-1-1_17-13-3.png

I made the code name the entity StringShotBodyX where X is the length of the buffer when the job executes so something is definitely fishy.

for now i’ll try using commandBuffer.Playback(EntityManager) and see if there’s a difference

Just tested using commandBuffer.Playback(EntityManager) and it seems to work fine.