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.

Question Change IComponentData in Job. Not working

Discussion in 'Entity Component System' started by Unique-Player, Dec 4, 2022.

  1. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    42
    I would like to change float2 Location variable, that is part of IComponentData, inside the job.
    I have two jobs, one to move the entity, another to check and change location.

    First job - To move entity
    Code (CSharp):
    1. public partial struct MoveJob : IJobEntity
    2. {
    3.     public void Execute(Worker worker)
    4.     {
    5.         Debug.Log("MOVING  TO " + worker.location);
    6.     }
    7. }
    Second job - Check and change location
    Code (CSharp):
    1. public partial struct ReachedJob : IJobEntity
    2. {
    3.     public int randomKey;
    4.  
    5.     public void Execute(Worker worker)
    6.     {
    7.             Debug.Log("BEFORE " + worker.location);
    8.             worker.location = roadPointsDOTS[randomKey];
    9.             Debug.Log("AFTER " + worker.location);
    10.     }
    11. }
    Location is being changed inside ReachedJob, i check that with debug.log. But debug.log inside moveJob indicates that "worker.location" is the same and not being modified.
    Am i missing something?
     
  2. FONTOoMas

    FONTOoMas

    Joined:
    Sep 26, 2015
    Posts:
    6
    you need pass component by ref
    Code (CSharp):
    1.  public void Execute(ref Worker worker)
    2.  {
    3. ...
    4.  }
     
    Unique-Player likes this.
  3. Unique-Player

    Unique-Player

    Joined:
    May 6, 2017
    Posts:
    42
    Yep, that was the problem.

    Can you tell me, please, why i can change the position of the entity by following code without referencing TransformAspect, but must reference custom entity IComponentData?
    Code (CSharp):
    1.     public void Execute(Worker worker, TransformAspect transformAspect)
    2.     {
    3.         transformAspect.Position += new float3(direction.x, 0, direction.y) * deltaTime * worker.currentPoint;
    4.  
    5.     }
     
  4. FONTOoMas

    FONTOoMas

    Joined:
    Sep 26, 2015
    Posts:
    6
    Because Transform Aspect is not like regular IComponentData.
    Aspects use "ref" under the hood, and turned into a regular queries by code generator.

    From Entities manual:
    https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/aspects-concepts.html
     
    Unique-Player likes this.