Search Unity

Question Variable Component Type in ECS Generic Methods

Discussion in 'Entity Component System' started by slushieboy99, Dec 6, 2022.

  1. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    I'd like to check if a queried entity has a certain component, but I'd like that component to be variable. Is there a way to do this? Something like HasComponent<T>(entity) but T needs to be variable.

    Essentially I'm trying to write code which uses physicsWorld.OverlapBox to query all nearby entities, then check if the entities have some variable IComponentData or ComponentType.
     
  2. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
  3. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    88
    The docs for this in Entities 0.51 seem pretty bare. Do you have any examples of how this is used or what it does?

    Also, is this only usable in Entities 1.0?
     
  4. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    I haven't touched ECS for months as I'm still working on other things, but AFAIK you can use Chunks to check if it contains a specific ComponentDataType or a DynamicComponentType.
    if you're not iterating using chunks but in a non-linear way like raycasts... you can access the chunk from its entity using GetEntityStorageInfoLookup.
    I'm not sure, but I believe there are better ways to check if an entity contains a DynamicComponentType.


    I already used the DynamicComponentTypeHandle in previous versions.
     
    slushieboy99 likes this.