Have a nice day, guys.
I'd like to ask how to get component of an entity in a job? I instantiated the entity in the job and would like to access its component.
I have tried using BufferLookup but get the error "A component can't have IComponentData and IBufferElementData".
I have tried ComponentLookup:
- At the first attempt, I used it like in the Unity's example (https://docs.unity3d.com/Packages/com.unity.entities@1.2/manual/systems-looking-up-data.html) but get the error when using EntityQuery.
- After that, I tried assigning
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
EntityCommandBuffer.ParallelWriter ecb =
GetEntityCommandBuffer(ref state);
ProcessSpawnerJob spawnerJob = new ()
{
ElapsedTime = SystemAPI.Time.ElapsedTime,
Ecb = ecb,
CharacterLookup = state.GetComponentLookup<CharacterData>(true)
};
spawnerJob.ScheduleParallel();
}
And used it in a job
Entity newEntity = Ecb.Instantiate(chunkIndex, spawner.Prefab);
float3 spawnPosition = spawner.SpawnPosition;
spawnPosition.x = i - positionInColumn;
Ecb.SetComponent(chunkIndex, newEntity,
LocalTransform.FromPosition(spawnPosition));
Ecb.SetComponent(chunkIndex, newEntity, new CharacterData{
ID = spawner.ID,
Destination = new float3
(i - positionInColumn, 1.0f, int.MaxValue),
MoveSpeed = CharacterLookup[newEntity].MoveSpeed
});
but get the error "All entities created using EntityCommandBuffer.CreateEntity must be realized via playback()" when using CharacterLookup[newEntity].
I also tried adding ECB.Playback() in the System but get a bunch of other errors too.
Thank you for your time.