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 Is Entity.Index safe to use as the ID of an entity?

Discussion in 'Entity Component System' started by davenirline, Aug 10, 2023.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    941
    Would there be cases where two entities have the same Index value?
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    To ensure 100% uniqueness you'd need to have both Index & Version or "Entity" as a key.
    From code:
    Code (CSharp):
    1.         /// <summary>
    2.         /// The ID of an entity.
    3.         /// </summary>
    4.         /// <value>The index into the internal list of entities.</value>
    5.         /// <remarks>
    6.         /// Entity indexes are recycled when an entity is destroyed. When an entity is destroyed, the
    7.         /// EntityManager increments the version identifier. To represent the same entity, both the Index and the
    8.         /// Version fields of the Entity object must match. If the Index is the same, but the Version is different,
    9.         /// then the entity has been recycled.
    10.         /// </remarks>
    11.         public int Index;
    12.  
    13.         /// <summary>
    14.         /// The generational version of the entity.
    15.         /// </summary>
    16.         /// <remarks>The Version number can, theoretically, overflow and wrap around within the lifetime of an
    17.         /// application. For this reason, you cannot assume that an Entity instance with a larger Version is a more
    18.         /// recent incarnation of the entity than one with a smaller Version (and the same Index).</remarks>
    19.         /// <value>Used to determine whether this Entity object still identifies an existing entity.</value>
    20.         public int Version;
    Just Index will mean any reused entity will match.
     
    davenirline likes this.
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,556
    In the same world no two entities can have same index. But in multiple worlds yes. However, as mentioned by previous poster, entities version is used also, to identify, if it is new unique entity, when is reused.

    So if you destroy let's say lots of projectiles in a small game, over duration of game, you may be using for example only first 1000 indexes for entities (not guaranteed however). So the index of entities is not incremented indefinitely. But version of entity will, after each new entity is created and is reusing entity.

    Side note:
    Entity version always start from 1, and can not be less.
    ECB when instantiating entities, uses negative index of entities. After ECB playback, next valid (positive) index of entity is assigned.
     
    Last edited: Aug 10, 2023
    davenirline likes this.
  4. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    383
    If you're sure that two Entities exist (created & not destroyed), then they would have different indices.

    Otherwise, they might have the same index.
     
    davenirline likes this.