Question: Enums and Animator Parameters

Is there a practical reason that you shouldn’t or aren’t using Enums to store Animator and other const/static strings?

Had the idea since using strings in method calling is bad, to store Animator Parameter names in an Enum in the animator controlling script. Took a little bit of research to convert them back to a string when calling an Animator Trigger or Bool but seemed like it would be better than just storing it as a Const string variable in the script.

It’s not really any different. Except that you have extra processing to convert the enum into a string. With a constant it’s already a string. I had this idea myself, before, but it doesn’t make any difference…

There’s seldom a specific reason why we do or don’t use a particular approach. There are many paths to the same goal.

All methods carry the exact same problem, avoiding a mismatch between the string value in the Animator and the string that is either hashed or passed into the Animator.Set methods.

It doesn’t really matter if you use

const int AttackHash = Animator.StringToHash("Attack");
const int ShootHash = Animator.StringToHash("Shoot");

or

enum Parameters
{
    Attack,
    Shoot
}

Animator.SetTrigger(Parameters.Attack.ToString());

If the parameter in the animator itself is doesn’t match then it doesn’t match.

Which is why I was asking if there was something a bit more obscure for using or not using that method.

The main problem with strings is that they are case sensitive and must 100% match, so one typo can cause problems and you could make a const public as well, I was just wondering if there was a reason for doing it that way compared to not.

Absolutely. The trick is that there’s no actual way to guarantee that the const string (public or not), or the enum values are going to be spelled the same way either.

In terms of why Hugo used consts instead of enums, there is no right or wrong way to manage this. Const strings, cached Animator.StringToHash values, or an enum. Which ever way works best for you, go ahead and use it. The same caveats apply in all cases, you have to make sure that the value in the Animator matches the value in the const or enum.

Privacy & Terms