Search Unity

How to get an Entiy to SharedComponentData Map

Discussion in 'Entity Component System' started by jsestini, Aug 26, 2018.

  1. jsestini

    jsestini

    Joined:
    May 15, 2017
    Posts:
    4
    I'm looking for the SharedComponentData equivalent of ComponentDataFromEntity<MyComponent>()
    or possibly a index derived from a group containing ComponentDataFromEntity to be used in SharedComponentData.

    This is for an infrequent lookup.
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    EntityManager.GetSharedComponentData<T>(entity)
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    That's not the same thing.

    I think the closest thing you can do is use the new Chunk approach, though this returns the index for shared components so you need to do a bit of a work around. Check out the new TransformSystem, I believe 1 section in there does it.
     
  4. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    It's same as GetComponentData in simple way - get data by entity. Of course, this is different from the usual GetComponentData because the Shared component data is a different thing and have architectural differences, but on "top level" for users its same action: "System, give me please data for this entity"
     
  5. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    He's not asking about GetComponentData though. He's asking about ComponentDataFromEntity.

    ComponentDataFromEntity (what ComponentDataFromEntity returns) can be passed to Jobs to allow you to conditionally execute code if an entity has a specific component.

    Code (CSharp):
    1.         private struct Job : IJobParallelFor
    2.         {
    3.             [ReadOnly] public EntityArray Entities;
    4.             [ReadOnly] public ComponentDataFromEntity<Initialized> IsInitialized;
    5.  
    6.             public void Execute(int index)
    7.             {
    8.                 if (IsInitialized.Exists(Entities[index]))
    9.                 {
    10.                     // Do somethibng
    11.                 }
    12.                 else
    13.                 {
    14.                     // Do something else
    15.                 }
    16.             }
    17.         }
    You can't do this with EntityManager.GetSharedComponentData

    If you're not using it in a job, then sure, you can just use HasComponent<T> and GetSharedComponentData<T>
     
    Last edited: Aug 28, 2018
  6. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    I know how it works and what limitations it has. We do not have enough context to understand whether he receives data in the job or not. Thus I described the simplest approach without jogs.
     
  7. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529