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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

[Solved]Is it safe to assume that Animator.StringToHash() should never return zero?

Discussion in 'Editor & General Support' started by neginfinity, Feb 1, 2018.

  1. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,316
    Is it safe to assume that Animator.StringToHash() would never return zero for a valid string?

    I'd expect this to be the case for non-null strings... but documentation does not guarantee it.

    I also cannot exactly execute IL code of this particular function...
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,822
    Animator.StringToHash uses CRC32. So, in theory, it is possible that it could return 0 as a hash.
    It will also return 0 if the string is null.
     
    rockin likes this.
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,316
    Thanks for the reply.

    I've re-checked CRC32 algorithm and it indeed can return zero.
    I've redesigned the code not to assume that some hash values are invalid.

    However.... I think this is an important detail and should be reflected on documentation. Currently documentation doesnt' say which hash algorithm is used by StringToHash.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,822
    Good point. We will add something to the docs.
     
  5. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,316
    Thanks. Marking the thread as solved.
     
    karl_jones likes this.