Search Unity

How can check component exit in entity?

Discussion in 'Entity Component System' started by jintaenate, Jul 20, 2019.

  1. jintaenate

    jintaenate

    Joined:
    Sep 12, 2018
    Posts:
    17
    I add component to entity.
    And in the job, how can i check that entity has component which i added?
     
  2. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    ComponentDataFromEntity<T> has a .Exist(Entity) method that will tell you if an entity has that type of component.
    https://docs.unity3d.com/Packages/c...Unity.Entities.ComponentDataFromEntity-1.html

    Moreover, you could include that component type in the EntityQuery you pass to your job, or even RequireForUpdate(EntityQuery) on the system itself.

    If this component is an empty tag, you could merely put a RequireComponentTag on that job.
     
  3. jintaenate

    jintaenate

    Joined:
    Sep 12, 2018
    Posts:
    17
    Do you have any example of componentDataFromEntity ??

    i am so confused to use this function in jobcomponentsystem
     
  4. digitaliliad

    digitaliliad

    Joined:
    Jul 1, 2018
    Posts:
    64
    ComponentDataFromEntity has good uses, but I find that most of the time EntityQuery is a better way to constrain job processing based on the presence, or lack, of specific components. For example, if you only want to process Disabled entities, you query for that component using EntityQuery, and pass it to your job, or instead perform some batch operation on the query, and forget the job.

    However, if you're processing an EntityQuery, and one of the components of this query is a reference to another entity, and you want to see if that second entity is Disabled to determine how to process the first entity, you could well use ComponentDataFromEntity, here:

    Code (CSharp):
    1. [BurstCompile]
    2. struct SomeJob : IJobForEachWithEntity<OtherEntity>
    3. {
    4.     [ReadOnly]public ComponentDataFromEntity<Disabled> disabled;
    5.     public void Execute(Entity entity, int index, [ReadOnly]ref OtherEntity otherEntity)
    6.     {
    7.         if (disabled.Exists(otherEntity.Value))
    8.         {
    9.             // so, if this other entity is disabled,
    10.             // that could affect what I want to do to this first entity.
    11.         }
    12.     }
    13. }
    14.  
    15. public override JobHandle OnUpdate(JobHandle inputDeps)
    16. {
    17.     jobHandle = new SomeJob
    18.     {
    19.         disabled = GetComponentDataFromEntity<Disabled>(true)
    20.     }.Schedule (someQuery, inputDeps);
    21.     return jobHandle;
    22. }