I have the following code running inside an IJobChunk, where I want to add a component when a bool in an other component is set to true. This is how I do it so far :
[BurstCompile]
public struct ComponentAdderJob : IJobChunk
{
public ArchetypeChunkComponentType<ComponentToListen> listenType;
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
{
NativeArray<ComponentToListen> listenComponents = chunk.GetNativeArray(listenType);
//loop through all the entities in the chunk
for (int i = 0; i < chunk.Count; i++)
{
ComponentToListen listener = listenComponents[i];
//addComponent would be a public bool, used like a trigger
if (listener.addComponent)
{
listener.addComponent = false;
EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
//here I want to get a reference to the entity that the listener is attached to, so I can add a component to it using the entityManager
ComponentToAdd newComponent = entityManager.AddComponent(entity, typeof(ComponentToAdd));
FillData(entity, newComponent); //we put some data in the newly added component
}
}
listenComponents.Dispose();
}
}
So as you can see my issue is that I don’t know how to get a reference to the current entity.
I tried to do it using Entites.ForEach() which uses a lambda expression, and I want to call another function to fill some data inside the new component, so it gives me this error :
on a AddComponentJobSystem which is a reference type.
This is only allowed with .WithoutBurst() and .Run().```
I then defined my functions as static, which deleted the previous error but gave me this one instead :
```static error DC0027: Entities.ForEach Lambda expression makes a structural change.
Use an EntityCommandBuffer to make structural changes or
add a .WithStructuralChanges invocation to the Entities.ForEach to allow for structural changes.
Note: LambdaJobDescriptionConstruction is only allowed with .WithoutBurst() and .Run().```
So as they say a possibility for me would be to not use Burst, but I really want to use it.
Thanks in advance