Enum Optimisation

Hey everyone
Need a little help with this one please, this script works fine but its a pain when i have to implement more things into the script. I wondered is there a way to optimise the script using the enum to but directly into an int without having to set to string each time?

public enum chosenitem
{
  Zero, One, two, three, four, five, six, seven, eight, nine, ten, eleven
}

public class Droppeditem : MonoBehaviour
{

  public chosenitem chitem;
  public string chositem;
  public int itemid;
  itemdatabase database;

  void Start()
  {
  chositem = chitem.ToString();
  database = GameObject.FindGameObjectWithTag("Itemdatabase").GetComponent<itemdatabase>();
  if (chositem == "Zero")
  {
  itemid = 0;
  }
  if (chositem == "One")
  {
  itemid = 1;
  }
  if (chositem == "Two")
  {
  itemid = 2;
  }
  if (chositem == "Three")
  {
  itemid = 3;
  }
  if (chositem == "Four")
  {
  itemid = 4;
  }
  if (chositem == "Five")
  {
  itemid = 5;
  }
  if (chositem == "Six")
  {
  itemid = 6;
  }
  if (chositem == "Seven")
  {
  itemid = 7;
  }

Thank you for looking :slight_smile:

http://stackoverflow.com/questions/483794/convert-enum-to-string

public static String convertToString(this Enum eff)
{
    return Enum.GetName(eff.GetType(), eff);
}

public static EnumType converToEnum<EnumType>(this String enumValue)
{
    return (EnumType) Enum.Parse(typeof(EnumType), enumValue);
}

Just cast the enum to an int.

itemid = (int)chosenitem;

However, I don’t really why you’re using an enum in the first place. The point of enums is to avoid magic numbers, but your enum is just spelling the numbers out, so there doesn’t seem to be much point.

–Eric

2 Likes

Yes perfect thank you guys, I used your version eric it works.

Basically so i can select a object type in the inspector on an object which links to a list, mainly for inspector use tbh not sure how else to create a drop down for the inspector :slight_smile:

Edit - I also made a mistake in my question was meant to say convert to int not string glad you understood what i meant

be aware changing the order in the enum list will change the idtemid.

1 Like

Why are you not using the enum directly for comparison? Am I missing something? Why do you need to convert it into a string?

    public enum chosenitem
    {
        Zero, One, two, three, four, five, six, seven, eight, nine, ten, eleven
    }

    public class Droppeditem : MonoBehaviour
    {
        public chosenitem chitem;

        void Start()
        {
            if (chitem == chosenitem.Zero)
            {
                // blah
            }
        }
    }
2 Likes

My thoughts, too…

Thanks for the replys people, i think mainly the issue due to my lack of knowledge on enums tbh I havent used them much. I didnt realised you could do .Zero so I was converting it to a string and working it that way, a long winded way I know but its much better now.

So now when I use the itemid = (int)chosenitem; and select the item i need in the inspector before runtime it puts the correct itemID in and it searches my list and so on…

See I needed a quicker way as everytime I added an item to my list I had to add the conversion in the string you see…

Make sense now?

:slight_smile:

Nope, still not making sense. Why would you convert your enum into anything?

The only time I could see converting the enum is.

  1. serialization - you serialize your enum as an integer. Your serialize method should handle the conversion back and forth in an encapsulated place so that it is typed ‘int’ for only that part of the process.

  2. inspector/labels/etc - you want to show a dropdown box, or list, or something visual of the names of each enum value. Say in the inspector (of course unity supplies a function that does this for you, so you shouldn’t really need to convert to string unless writing a highly specialized editor/inspector)

  3. to perform some arithmetic or boolean logic on the enum for some reason. This would mean your enum is probably non-sequential, and highly specialized.

Other than that though, I have a hard time coming up with off cases that it might be necessary. The whole point of an enum is so that you don’t have to keep referring to the value as it’s integer value. So you don’t even need to KNOW the related integer value. It just works by name.

Did you consider a Dictionary? You will encounter some serialisation issues this way, but there are ways around that too.

enum MyEnum {a, b, c, d}
public MyEnum myEnum;

Dictionary <MyEnum, Item> myDictionary;

// Inside a relevant method
Item myItem = myDictionary[myEnum];

You’ve identified the issue, so why not rectify it? It won’t take long to learn about enumerations, and once you’ve learned about them you’ll not only be better equipped to solve this problem, but you’ll also know how to better use a tool that will continue to be useful in the future.

You’ll need to do this kind of spot learning all the time if you’re going to do much programming.

1 Like