Injected arrays deallocated after adding component to entity

public struct Worker : IComponentData
{
}

public class WorkerComponent : ComponentDataWrapper<Worker>
{
}


public class SomeSystem : ComponentSystem
{
    public struct SomeStructData
    {
        public int someValue;
    }

    public struct SomeGroup
    {
        public ComponentArray<SomeMonoBehaviourComponent> monoComponent;
        public SubtractiveComponent<WorkerComponent> notWorker;
        public EntityArray Entity;
        public int Length;
    }

    [Inject] public SomeGroup someGroup;

    public struct SomeJob : IJobParallelFor
    {
        public NativeQueue<SomeStructData>.Concurrent someData;

        public void Execute(int index)
        {
            //DO STUF
        }
    }

    protected override void OnUpdate()
    {
        NativeQueue<SomeStructData> someData = new NativeQueue<SomeStructData>(Allocator.Temp);
        var job = new SomeJob
        {
            someData = someData
        };
        job.Schedule(someGroup.Length, Mathf.CeilToInt(someGroup.Length / 8)).Complete();

        while (someData.Count > 0)
        {
            SomeStructData data = someData.Dequeue();
            for (int i = 0; i < data.someValue; i++)
            {
                if (someGroup.Length > 0)
                {
                    EntityManager.AddComponent(someGroup.Entity[0], typeof(WorkerComponent));
                   // ^^^^^^ after first adding, this place thrown error - NativeArray has been deallocated, as i know AddComponent in EntityManager is one of synch points
                }
            }
        }
    }
}

How i can add new components in one Update loop in hybrid ECS?
Also i tried to use EntityCommandBuffer:

public struct Worker : IComponentData
{
}

public class WorkerComponent : ComponentDataWrapper<Worker>
{
}


public class SomeSystem : ComponentSystem
{
    public struct SomeStructData
    {
        public int someValue;
    }

    public struct SomeGroup
    {
        public ComponentArray<SomeMonoBehaviourComponent> monoComponent;
        public SubtractiveComponent<WorkerComponent> notWorker;
        public EntityArray Entity;
        public int Length;
    }

    [Inject] public SomeGroup someGroup;

    public struct SomeJob : IJobParallelFor
    {
        public NativeQueue<SomeStructData>.Concurrent someData;

        public void Execute(int index)
        {
            //DO STUF
        }
    }

    protected override void OnUpdate()
    {
        NativeQueue<SomeStructData> someData = new NativeQueue<SomeStructData>(Allocator.Temp);
        var job = new SomeJob
        {
            someData = someData
        };
        job.Schedule(someGroup.Length, Mathf.CeilToInt(someGroup.Length / 8)).Complete();

        if (someData.Count > 0)
        {
            EntityCommandBuffer buff = new EntityCommandBuffer();
            while (someData.Count > 0)
            {
                SomeStructData data = someData.Dequeue();
                for (int i = 0; i < data.someValue; i++)
                {
                    if (someGroup.Length > 0)
                    {
                        buff.AddComponent(someGroup.Entity[0], new Worker());
                        // ^^^This place handle error every time - Null Reference
                        //Interesting moment - EntityManager.AddComponent and EntityCommandBuffer.AddComponent - different, why?!
                    }
                }
            }
            buff.Playback(EntityManager);
        }
 
    }
}

Any solutions?

take use of:

PostUpdateCommand.CreateEntity()
PostUpdateCommand.AddComponent(new SomeData())
...
1 Like

Hmm, PostUpdateCommands is also EntityCommandBuffer…but in my own EntityCommandBuffer - m_Data (in EntityCommandBuffer.cs) is null.
PostUpdateCommands basically does what it takes (as I imagined the work of the buffer), but in my case there is a nuance, I do not need to create Entity, I just add the component to the existing one:

PostUpdateCommands.AddComponent (m_Citizens.Entity [j], new Worker ());

But then the fun begins, after adding the EntityArray component is not updated correctly and in m_Citizens there are entities on which I added Worker, despite the presence:

public SubtractiveComponent NotWorker;

Oh, I’m sorry, I completely overlooked that I added the WorkerComponent wrapper as T in to SubtractiveComponent, not Worker: IComponentData :slight_smile: