Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get entity by Component

Discussion in 'Entity Component System' started by Orimay, Apr 25, 2018.

Thread Status:
Not open for further replies.
  1. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    It states that Components do not get attached to Entities, but Entities do get attached to Components. Is there a way to get Entity from/by a Component?
     
  2. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Nope, a component is just a blob of Data.
    An entity just two numbers: a key into a table of pointers, where the pointer points to the "actual" storage location of all of it's components, and a Version number that's bumped when it's destroyed, used to check if a specific entity is still alive or not.

    Unless the Component in question has it's owning Entity value as its (or one of its) data values, there's no way to do a reverse lookup.
     
  3. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    Oh, that is sad. Thank you!
     
  4. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    789
    A component is not attached to an Entity, but the component knows the Entity-ID, It is technically possible
     
  5. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    How would I get it then?
     
  6. mike_acton

    mike_acton

    Unity Technologies

    Joined:
    Nov 21, 2017
    Posts:
    110
    Entity is not attached to *a* component but it is (implicitly) attached to a set of components. If you're processing a ComponentGroup you can get the corresponding Entity with EntityArray. Which you can either inject or GetEntityArray if you have the ComponentGroup.
     
    Orimay likes this.
Thread Status:
Not open for further replies.