Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question DOTS get the entity insinde IJobChunk

Discussion in 'Scripting' started by ErwanTLG, May 17, 2020.

Thread Status:
Not open for further replies.
  1. ErwanTLG

    ErwanTLG

    Joined:
    Apr 15, 2018
    Posts:
    3
    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 :
    Code (CSharp):
    1.  
    2. [BurstCompile]
    3. public struct ComponentAdderJob : IJobChunk
    4. {
    5.     public ArchetypeChunkComponentType<ComponentToListen> listenType;
    6.     public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex)
    7.     {
    8.         NativeArray<ComponentToListen> listenComponents = chunk.GetNativeArray(listenType);
    9.  
    10.         //loop through all the entities in the chunk
    11.         for (int i = 0; i < chunk.Count; i++)
    12.         {
    13.             ComponentToListen listener = listenComponents[i];
    14.  
    15.             //addComponent would be a public bool, used like a trigger
    16.             if (listener.addComponent)
    17.             {
    18.                listener.addComponent = false;
    19.                EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    20.  
    21.                //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
    22.               ComponentToAdd newComponent = entityManager.AddComponent(entity, typeof(ComponentToAdd));
    23.               FillData(entity, newComponent); //we put some data in the newly added component
    24.            }
    25.         }
    26.  
    27.     listenComponents.Dispose();
    28.     }
    29. }
    30.  
    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 :

     error DC0002: Entities.ForEach Lambda expression invokes 'FillData'
    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
     
    Last edited: May 17, 2020
  2. tanmaythegreat

    tanmaythegreat

    Joined:
    Dec 14, 2020
    Posts:
    12
    did u fixed it ??
    i am also facing the same problem ,i want to know that how can this be fixed
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,523
    Please don't necro old posts like this, it's also the wrong forum. The DOTS forums are here and you can create your own post there.
     
Thread Status:
Not open for further replies.