Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

access components in other entities inside the job system with ComponentDataArray

Discussion in 'Entity Component System and C# Job system' started by SocialSimulator, Feb 14, 2019 at 10:48 AM.

  1. SocialSimulator

    SocialSimulator

    Joined:
    Sep 19, 2017
    Posts:
    16
    I have a IJobProcessComponentDataWithEntity job and wish to access components in other entities inside the job system.

    Many of the examples out there use ComponentDataArray, but they are now deprecated.

    I understand the new way to do it is using GetComponentDataFromEntity (https://github.com/Unity-Technologi.../Documentation~/component_data_from_entity.md), but I cannot figure out how to put it together.

    Does anyone have a simple worked example of how the entire JobComponentSystem fits together with this?

    Many thanks in advance )
     
  2. SocialSimulator

    SocialSimulator

    Joined:
    Sep 19, 2017
    Posts:
    16
    I'm also trying:
    var value = GetComponentDataFromEntity<myComponent>(false)[anotherEntity];

    I have the id of anotherEntity in the form of an int, but then do not know how to get the Entity in the job:
    Entity anotherEntity = does anyone know the answer (anotherEntityId) 


    No idea if this is the right approach, or what other code I need to write. So really grateful for any help. Thanks )
     
  3. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    155
    You can't just use an int for getting the Entity, you must have Index and Version (both ints).
     
  4. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    155
    Here is a simple example of accessing an attached entity's parent
    Code (CSharp):
    1. public class MySystem : JobComponentSystem {
    2.  
    3.   [BurstCompile]
    4.   struct GatherParents : IJobProcessComponentData<Parent> {
    5.     [ReadOnly]
    6.     public ComponentDataFromEntity<LocalToWorld> LocalToWorldFromEntity;
    7.  
    8.     public void Execute(ref Parent parent) {
    9.       var parentLocalToWorld = LocalToWorldFromEntity[parent.Value];
    10.       // Do something with parent matrix
    11.     }
    12.   }
    13.  
    14.   protected override JobHandle OnUpdate(JobHandle inputDeps) {
    15.  
    16.     inputDeps = new GatherParents {
    17.       LocalToWorldFromEntity = GetComponentDataFromEntity<LocalToWorld>(true)
    18.     }.Schedule(this, inputDeps);
    19.  
    20.     return inputDeps;
    21.   }
    22. }
    You can also have a job were you pass an entity to it but you must know that entity before hand not just guessig :)
     
  5. SocialSimulator

    SocialSimulator

    Joined:
    Sep 19, 2017
    Posts:
    16
    I'm not trying to access a parent. I'd just like to access a certain type of component in a job and refer to it by entity. It used to be that you could do this with ComponentDataArray. But, how do you do it now?
     
  6. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    155
    But you can do it with GetComponentFromEntity<type>()[entity].
    I don't get the problem.
    The above was an example.of usage in a job, Parent.Value is an entity.
     
    SocialSimulator likes this.
  7. SocialSimulator

    SocialSimulator

    Joined:
    Sep 19, 2017
    Posts:
    16
    That makes sense, GilCat.
    I realise now the other part of my problem is getting the entity.
    What I want to do is have an entity with a component on it that refers to some other entity (like an edge in a graph),
    That's what I'm having trouble with in implementing. Any ideas?
     
  8. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    155
    I use exactly that. I have and Edge component that has reference to source entity and target entity and from there i can access whatever components i want both on source and target.
    Code (CSharp):
    1.  
    2. public struct EdgeComponent : IComponentData{
    3.   public Entity Source;
    4.   public Entity Target
    5. }
    Then use IJobProcessComponentData<EdgeComponent>
    Works great for me!
     
    SocialSimulator likes this.