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 Look up arbitrary data, question about doc code example

Discussion in 'Entity Component System' started by vildauget, Apr 4, 2023.

  1. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    120
    Just to clarify if I get this correctly or not. Trying to get back into the game...

    Studying example code for ComponentLookup at https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/systems-looking-up-data.html , I wonder if the actual data from the lookup is somehow magically used, or if the job only uses the lookup to check that the linked entity still exist, and already has saved the target position in its "local entity" using another system.

    If there isn't any magic I'm missing, in a real world example, wouldn't it be better to not keep the linked entity's posistion, and rather use the lookup to grab the position, replacing

    Code (CSharp):
    1. float3 targetPosition = entityPosition.Position;
    with

    Code (CSharp):
    1. float 3 targetPosition = EntityPositions[targetEntity].Position;
    The job from the page:

    Code (CSharp):
    1.     [BurstCompile]
    2.     private partial struct MoveTowardsJob : IJobEntity
    3.     {
    4.  
    5.         // Read-only data stored (potentially) in other chunks
    6.         [ReadOnly]
    7.         public ComponentLookup<LocalToWorld> EntityPositions;
    8.  
    9.         // Non-entity data
    10.         public float deltaTime;
    11.  
    12.         public void Execute(ref LocalTransform transform, in Target target, in LocalToWorld entityPosition)
    13.         {
    14.             // Get the target Entity object
    15.             Entity targetEntity = target.entity;
    16.  
    17.             // Check that the target still exists
    18.             if (!EntityPositions.HasComponent(targetEntity))
    19.                 return;
    20.  
    21.             // Update translation to move the chasing entity toward the target
    22.             float3 targetPosition = entityPosition.Position;
    23.             float3 chaserPosition = transform.Position;
    24.  
    25.             float3 displacement = targetPosition - chaserPosition;
    26.             transform.Position = chaserPosition + displacement * deltaTime;
    27.         }
    28.     }
    29.  
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,653
    It's a typo in docs.
    Code (CSharp):
    1. float3 targetPosition = entityPosition.Position;
    Should be
    Code (CSharp):
    1. float3 targetPosition = EntityPositions[targetEntity].Position;
     
    vildauget likes this.
  3. vildauget

    vildauget

    Joined:
    Mar 10, 2014
    Posts:
    120
    Thank you, eizenhorn.

    That's what puzzled me - it's not a simple typo, as they include component used (entityPosition) in the Execute parameters:

    Code (CSharp):
    1. public void Execute(ref LocalTransform transform, in Target target, in LocalToWorld entityPosition)
    2.