So I’ve been trying to add components and I saw in the latest update that you could now do it in Jobs without using Postupdatecommands. I used the below code but am kind of confused with things discussed in the Unity discord. Am I just queuing up the commands for them to be complete on the main thread by using the ECB and this below, is this not the correct way.
Allow structural changes to entities (add/remove components, add/destroy entities, etc.) while inside of `ForEach` lambda functions. This negates the need for using `PostUpdateCommands` inside of ForEach.
struct AssignUnemployedCompJob : IJobForEachWithEntity<PersonEntityInfo>
{
public EntityCommandBuffer.Concurrent commandsBuffer;
public void Execute(Entity currentent, int index, [ReadOnly] ref PersonEntityInfo currentcomp)
{
if(currentcomp.WorkPlaceEntityRef == Entity.Null)
{
commandsBuffer.AddComponent<Unemployed>(index, currentent);
}
}
}
Is ECB so inefficient and random inside a job that it takes roughly 60ms to iterate through 10,000 entities and add them all to a queue just for the main thread to complete the actual addcomponent operation basically instantly on a single thread.
I’m kind of confused and wondering what is happening and if I’m doing things wrong. Is there a more efficient way to add/remove components in a job.