# Get enum value “string”, and get int value of that

Sorry if the title is confusing, but I literally don't even know how to summarise this into a title. So here's the problem:

I have a list of combat moves for a character. I have an animation for each of those possible moves. When the "combat manager" (instantiated to manage each individual fight) works out what move the enemy and your person will make, it should tell the Animator to play the animation specific to the selected move. I'm really not sure how to go about this so:

The player's Animator has a state for each possible move, containing that move's animation. The script controlling the Animator has a public enum "CombatMove" containing every possible move (ie. CombatMove.WhicheverMove). So the Combat Manager will get the name of the move (eg. "Right Middle Jab"), remove the spaces and search for an enum value whose name is that string (eg. CombatMove.RightMiddleJab). Now it has the corresponding enum value, it retrieves the int value of that (eg. 0). It then sends that int value to the Animator, where each move's transition will occur if the int value equals the move's int value on the enum. So summed up:

move's string name, turned into an enum value name, turned into its int value, used to specify which move animation should be played.

The questions:

1) How do you find an enum value whose name equals a given string?

2) How do you get the int value of that enum value?

If this is confusing, please comment what makes no sense and I will try to explain, I'm honestly so lost that I don't even know how to ask what I need to ask.

## EDIT:

IF anyone is wondering what I mean by 'int value', Fafase's answer at http://answers.unity3d.com/questions/764091/using-c-enums-to-control-an-animator.html says that each enum value has an int value by default, which can be used in animation conditions; however I have no idea how to access them, which part 2 of my problem.

Would this do it?

enum CombatMove
{
RightMiddleJab,
LeftMiddleJab,
RightUpperJab,
LeftUpperJab,

LastCombatMove,
Unknown
}


call example..

        CombatMove move;
if( findCombatMove("RightUpperJab", out move) == true )
{
// do something with move
int intValue = (int)(move);
}


.

    static bool findCombatMove( String text, out CombatMove move )
{
for( int c = 0; c < (int)CombatMove.LastCombatMove; ++c)
{
String enumAsString = ((CombatMove)(c)).ToString();

if (enumAsString == text)
{
move = (CombatMove)(c);
return true;
}
}

move = CombatMove.Unknown;
return false;
}


It would be a lot of string compares though. Could you not pass the enum directly instead of the string ?

• What do you mean pass the enum directly? Animator parameters don't accept enums, if that's what you mean...? – DJpotato Apr 13 '16 at 10:19
• Unfortunately I don't know about the systems that you've mentioned. And so I'm not aware of the data types they create or consume. But usually when I see conversions/comparing enums & strings my programmer brain starts nagging me about inefficiencies ( all those string compares ) and maintenance ( change an enum and you need to ensure the equivalent string generation is updated also ). Without more info I cant really comment. I guess if you really have to use strings then one quick optimization would be to pre-build the enum strings into a dictionary which uses the enum & String as key,value. – RobM Apr 13 '16 at 18:37
• Well the method I devised was by no means efficient... I'd been doing a coding marathon for 10 hours and my brain was a bit messed up, so I would love any recommendations u might have on how to optimise it (or even overhaul it) - just tell me what information you need, I'll edit the question – DJpotato Apr 14 '16 at 4:42
• The question is fine I think. Best leave it as is as it may help others that have a similar issue. Was just trying to help, didn;t mean ot offend any coding methods :) – RobM Apr 15 '16 at 8:36
• Haha none taken, after implementing your idea, I realised that it would be much simpler to add an 'intValue' variable to each move, which corresponds to a animation parameter (eg if moveInt == 1, play right middle jab, where moveInt is assigned in the combat manager). But I also realised that you answered my question perfectly, so thank you very much – DJpotato Apr 26 '16 at 2:42

You can override the size of the enum, if needed:

enum CombatMove : int //implied, 32-bits, max value 2147483647
{
RightMiddleJab = 0, //0
LeftMiddleJab = 1,  //1
RightUpperJab = 3,  //3
LeftUpperJab,       //4
}


And since you can cast them, there is probably no reason to refer to them by name:

CombatMove combatMove = CombatMove.LeftUpperJab;
int combatMoveValue = (int)combatMove;


I don't know exactly what you are trying to index, both types are arbitrary:

using System.Collections.Generic;
Dictionary<CombatMove, AnimationClip> AnimationClipsByEnum;
AnimationClipsByEnum[CombatMove.RightMiddleJab].ClearCurves();


Strings

If you really do have a good reason, you can use the System.Enum class:

string[] CombatMoveNames = System.Enum.GetNames(typeof(CombatMove));


The dictionary for strings:

using System.Collections.Generic;
Dictionary<string, AnimationClip> AnimationClipsByName;
AnimationClipsByName["RightMiddleJab"].ClearCurves();


Parsing a string back into to an enum:

    string combatString = "RightMiddleJab";
try
{
CombatMove combatMove =
(CombatMove)System.Enum.Parse(typeof(CombatMove), combatString);
}
catch (System.ArgumentException)
{
Debug.Log("Invalid enum string: " + combatString);
throw;
}

• Thanks for your input and sorry for such an incredibly late reply, as I said in RobM's answer, I figured out a far better way of doing this than comparing strings or using enums, but your answer was useful, thanks – DJpotato Apr 26 '16 at 2:47