access components in other entities inside the job system with ComponentDataArray

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-Technologies/EntityComponentSystemSamples/blob/master/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 )

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 )

You can’t just use an int for getting the Entity, you must have Index and Version (both ints).

Here is a simple example of accessing an attached entity’s parent

public class MySystem : JobComponentSystem {

  [BurstCompile]
  struct GatherParents : IJobProcessComponentData<Parent> {
    [ReadOnly]
    public ComponentDataFromEntity<LocalToWorld> LocalToWorldFromEntity;

    public void Execute(ref Parent parent) {
      var parentLocalToWorld = LocalToWorldFromEntity[parent.Value];
      // Do something with parent matrix
    }
  }

  protected override JobHandle OnUpdate(JobHandle inputDeps) {

    inputDeps = new GatherParents {
      LocalToWorldFromEntity = GetComponentDataFromEntity<LocalToWorld>(true)
    }.Schedule(this, inputDeps);

    return inputDeps;
  }
}

You can also have a job were you pass an entity to it but you must know that entity before hand not just guessig :slight_smile:

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?

But you can do it with GetComponentFromEntity()[entity].
I don’t get the problem.
The above was an example.of usage in a job, Parent.Value is an entity.

1 Like

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?

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.

public struct EdgeComponent : IComponentData{
  public Entity Source;
  public Entity Target
}

Then use IJobProcessComponentData
Works great for me!

1 Like