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);
}
}
}