Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Pointer to component data

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

  1. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    74
    I'm looking to have one entity reference another entity's component data, currently I store the target entity in a component, then use GetComponent to get that entities data. Is it possible to store a reference to the target entity's component data in a component so I don't have to use GetComponent?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    Not persistently. Whenever a structural change happens, entities and their components can be shuffled around in memory.
     
    Opeth001 likes this.
  3. slushieboy99

    slushieboy99

    Joined:
    Aug 29, 2014
    Posts:
    74
    So the most efficient way to do this would be to have a reference to an entity and use GetComponent every time you want to get the component?
     
  4. Enzi

    Enzi

    Joined:
    Jan 28, 2013
    Posts:
    909
    You can save pointer references but you need to check for chunk.DidOrderChange for the archetype and then update all references from the chunk. Even if just one changes, you'll never know which one.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,984
    There are several different APIs to do effectively the same thing in different contexts. But that's the idea.