Use const string instead of writing the parameter name

I recommend to define a static const string instead of writing “IsWalking” into the functions. Makes it much less error prone if the value is used multiple times and easier to maintain.

2 Likes

Or even

static int _isWalkingHash;
void Start()
{
    _isWalkingHash = Animator.StringToHash("IsWalking");
}

and then use the hash instead. You could make a const for the “IsWalking” here, but you won’t be using it again after this line

4 Likes

Yup using the hash method is indeed the recommended approach to avoid strings everywhere.

1 Like

should that be written on awake instead of start to avoid any future errors?
also, why are you not referencing “unitAnimator” instead of just Animator?
lastly, what is the difference between static and const?

thanks!

1 Like

I don’t like any of those approaches since it leaves you with pretty much the same problem, if you want to change a parameter you still need to go to the code and change it, not a friendly approach, especially if your animator or designer is scared of coding, it also creates a bunch of statics, which I kinda hate.

What I often do is far simpler but friendlier, have a single script handle all of the animations and expose a bunch of strings in the editor. This avoids the overuse of statics and is friendly towards non-coders.

4 Likes

That would be like a centralized Animation track registry?
I would think a reasonably good way to handle keeping the definitions outside script code would be using Scriptable Objects, so you could have one for an animation state that could be instantiated and assigned instead… if the state was renamed, this could easily be fixed in the SO instance. OTOH bear in mind the code would be adressing the animation state and not necessarily the animation track’s name. So as long as the states stay the same, as was already shown it’s easy to change the assigned animation track.
However, refactoring the animation states would likely require changes to the scripts that use the animator anyway, so if the animator added more parameters, the coder would have to update the script to make it work…

@AstraGamesStudios The difference between static and const is that one (const) means “this value cannot be changed”, while the other one (static) means “this value belongs to all instances of this class just the same, it is shared among all instances”.

What this means is that from outside the class you don’t have to have a reference to an instance of the class, you can just access the value directly using the class name instead of an object. Inside the class it just means that the value can be changed from any instance and all others would have the new one immediately. (This is often seen when one has a material and changing its colour changes it on all objects that have this material).

For now I keep it reasonably simple, though…

private static readonly int IS_WALKING = Animator.StringToHash("IsWalking");

(Note: it can’t be const because the value is the result of StringToHash() which is not defined to return a const value. Using readonly works just as fine…)

Privacy & Terms