Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can i GetComponentData from another entity within jobs?

Discussion in 'Entity Component System' started by RamboLambert, Mar 23, 2023.

  1. RamboLambert

    RamboLambert

    Joined:
    Dec 4, 2021
    Posts:
    2
    I am having trouble to access component data from another entity, entityManager.GetComponentData and entityManager.SetComponentData always thowing out error message like
    (InvalidOperationException: The ComponentTypeHandle<DOTS.Scripts.Belt.BeltComponent> has been declared as [WriteOnly] in the job, but you are reading from it.)

    Do i have to use EntiyCommandBuffer for that? Or is it because something wrong with how i set up my component.
     
  2. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    204
    You need to show the source code of your job.
     
  3. RamboLambert

    RamboLambert

    Joined:
    Dec 4, 2021
    Posts:
    2
    Yes , sorry , thought it was a generic question , here are the related code.

    Code (CSharp):
    1.     public readonly partial struct BeltAspect : IAspect
    2.     {
    3.         private readonly TransformAspect _transformAspect;
    4.         private readonly DynamicBuffer<Child> _childBuffer;
    5.         private readonly RefRW<BeltComponent> _belt;
    6.  
    7. public void MoveItemOnBelt(float deltaTime)
    8.         {
    9.             var entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
    10.             var childArray = ChildArray;
    11.  
    12.             for (var i = 1; i < childArray.Length; i++)
    13.             {
    14.                 var itemOnBelt = entityManager.GetComponentData<ItemOnBelt>(childArray[i].Value);
    15.                 itemOnBelt.Offset = _belt.ValueRO.ItemOffset;
    16.                 if (i == 1)
    17.                 {
    18.                     // calculate progress
    19.                         }
    20.                     }
    21.                     else
    22.                     {
    23.                         var nextBelt = entityManager.GetComponentData<BeltComponent>(_belt.ValueRO.NextBelt);
    24.                         if (nextBelt.EndingPosToItemDistance <= 0f)
    25.                         {
    26.                             if (itemOnBelt.Progress <= 1.0f)
    27.                             {
    28.                                 itemOnBelt.Progress += _belt.ValueRO.Speed * deltaTime;
    29.                             }
    30.                             else
    31.                             {
    32.                                 var parent = entityManager.GetComponentData<Parent>(childArray[i].Value);
    33.                                 parent.Value = _belt.ValueRO.NextBelt;
    34.                                 entityManager.SetComponentData(childArray[i].Value, parent);
    35.                                 itemOnBelt.Progress = 0f;
    36.                                 // TODO set parent
    37.                             }
    38.                         }
    39.                         else
    40.                         {
    41.                             if (itemOnBelt.Progress >= 1.0 &&
    42.                                 nextBelt.EndingPosToItemDistance > nextBelt.SpaceFromItemsOnBelt)
    43.                             {
    44.                                 var parent = entityManager.GetComponentData<Parent>(childArray[i].Value);
    45.                                 parent.Value = _belt.ValueRO.NextBelt;
    46.                                 entityManager.SetComponentData(childArray[i].Value, parent);
    47.                                 itemOnBelt.Progress = 0f;
    48.                                 // TODO set parent
    49.                             }
    50.  
    51.                             if (nextBelt.EndingPosToItemDistance + _belt.ValueRO.StartingPosToItemDistance >=
    52.                                 _belt.ValueRO.SpaceFromItemsOnBelt)
    53.                             {
    54.                                 itemOnBelt.Progress += _belt.ValueRO.Speed * deltaTime;
    55.                             }
    56.                         }
    57.                     }
    58.                 }
    Code (CSharp):
    1.     public partial struct MoveItemOnBeltJob : IJobEntity
    2.     {
    3.         public float DeltaTime;
    4.  
    5.         [BurstCompile]
    6.         public void Execute(BeltAspect beltAspect)
    7.         {
    8.             beltAspect.MoveItemOnBelt(DeltaTime);
    I just realized the problem might be that I put all calculation in IAspect, maybe i should use Isystem directly?