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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

StringToHash pattern

Discussion in 'Scripting' started by ekt, May 30, 2017.

  1. ekt

    ekt

    Joined:
    Jul 9, 2012
    Posts:
    28
    Am I the only one feeling uneasy with hard coding strings that point to things that might later be renamed without any warning at compile/build time?

    Code (CSharp):
    1. animator.SetFloat(Animator.StringToHash("some_parameter"),  42);
    2. animator.Play(Animator.StringToHash("Base Layer.my_clip"));
    To me sounds like a bad practice, error prone, and hard to mantain in the long run
    But no one is complaining, so I'm genuinely interested to understand if I'm missing something

    cheers
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    You're not the only one. I try to avoid strings where ever possible, but in some cases it's unavoidable.

    For those cases, I like to put strings into some static Constants class, then reference those where needed. This way, I only need to change the string in one place, which automatically updates everything that depends on it. Or build some dictionary where it makes sense. Or whatever it takes to avoid hardcoding strings as much as I can.

    But, yes, it's problematic that you need to make sure the name syncs with whatever you've done in the Unity editor.
     
  3. ekt

    ekt

    Joined:
    Jul 9, 2012
    Posts:
    28
    I think unity could auto generate some strongly-typed resource class, like

    Code (CSharp):
    1. internal class Resources
    2. {
    3.     internal static string BaseLayer_my_clip{get{return "Base Layer.my_clip";}}
    4. }
    now that I think of, it should be possible to write some custom editor scripts that monitor file changes and does it on the fly
    but I still think it is unity ide responsibility
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
  5. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
  6. ekt

    ekt

    Joined:
    Jul 9, 2012
    Posts:
    28
    @Ironmax: thanks but I don't see how any of those points solves the problem i'm describing

    anyway, assets from exiguous is the solution for me (though i still think it should be built in in unity)
    btw: i found many github projects which try to fix the same issue, but all of them deal with stuff like layer and tags, but misses other resources (like animation clips)

    thanks all!
     
  7. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    ok dont forget to check the profiler now and then, if scripting is adding allot of purples, your not doing to well.

    String-O-Matic is using allot of strings all over the place to act as a wrapper (Bad idea) what i try to explain in my posts
    are a more memory friendly approach, using Enums or checking if last input string is equal to new input, this way you
    overcome overheads. Taking in to account what you wrote in your first post. Read again what blackPete here wrote..