Search Unity

Do two identical "Entity" references have the same GetHashCode value?

Discussion in 'Entity Component System' started by Mr-Mechanical, Jan 23, 2019.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Code (CSharp):
    1. bool IsGetHashCodesEqual() {
    2.      Entity a = //initialize somehow;
    3.      Entity b = a;
    4.      return (a.GetHashCode() == b.GetHashCode());
    5. }
    Would this return true or false? How is GetHashCode implemented with "Entity"?
    Thank you. Help is always very useful.
     
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    If you look in the source (Entity code is in EntityManager.cs file):
    Code (CSharp):
    1. public override int GetHashCode()
    2. {
    3.     return Index;
    4. }
    So, in your code, it should be true.

    []'s
     
    Mr-Mechanical likes this.
  3. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Thank you for finding this!
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    That is absolutely not something you can rely on. (And will likely break in the futrue)
    In general GetHashCode () can not be used for exact comparison. you can use Equals or == to compare it...
     
    Antypodish and Mr-Mechanical like this.
  5. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Thank you for the clarification! This is extremely good to know.