Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using an enum item as an argument

Discussion in 'Scripting' started by ThePropagation, Oct 15, 2019.

  1. ThePropagation

    ThePropagation

    Joined:
    May 11, 2015
    Posts:
    21
    See title, I want a list of characters, I've made a custom Character class, and I want to determine which character is playable by switching an enum, and trying to pass that into the array as its index, but I get an error.

    Code (CSharp):
    1.     public enum Char
    2.     {
    3.         Blank = 0,
    4.         VoidChar = 1,
    5.         FireChar = 2,
    6.         EarthChar = 3,
    7.         WindChar = 4,
    8.         WaterChar = 5,
    9.         End = 6
    10.     }
    11.  
    12.     public enum Att
    13.     {
    14.         Name = 0,
    15.         TrueMaxHP = 1,
    16.         MaxHp = 2,
    17.         HP = 3,
    18.         TrueMaxMP = 4,
    19.         MaxMP = 5,
    20.         MP = 6,
    21.         HPCorrupt = 7,
    22.         MPCorrupt = 8,
    23.         Attack = 9,
    24.         Defense = 10,
    25.         Magic = 11,
    26.         JumpHeight = 12,
    27.         RunSpeed = 13,
    28.         Weight = 14,
    29.         End = 15
    30.     }
    31.  
    32.     public int[,] CharStat = new int[Char.End, Att.End];
    It says that Char.End and Att.End aren't ints, how do I pass them as ints?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Cast them to ints.
    public int[,] CharStat = new int[(int)Char.End, (int)Att.End];


    Or you could also avoid needing an "end" entry to count the total entries by using
    Enum.GetValues(typeof(MyEnum)).Length

    Code (CSharp):
    1. int charCount = Enum.GetValues(typeof(Char)).Length;
    2. int attCount = Enum.GetValues(typeof(Att)).Length;
    3. public int[,] CharStat = new int[charCount, attCount];
     
    Antypodish likes this.
  3. Ardenian

    Ardenian

    Joined:
    Dec 7, 2016
    Posts:
    313
    Instead of using an enumeration with constant values, a bit field enumeration might be better suited for your needs. It can be created by adding the FlagsAttribute to an enumeration.

    The huge advantage of this type of enumeration is that you can switch bits dependent on a player's available character classes, instead of having a list that containts constant enumeration values which you have to check against another constant value to see if a character class is allowed.

    Using HasFlag, you can easily check if a custom character is allowed for a player.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,615
    I think you simply need to cast the enum value to an int by writing "(int)" in front of it, like this:
    Code (csharp):
    1.  
    2. public int[,] CharStat = new int[(int)Char.End, (int)Att.End];
    3.  
    An enum value is basically just an int but technically it's a different type and C# is very strict about types (which is good because it prevents errors a lot of times). When you switch between similar types you usually have to do some explicit cast or conversion so that the compiler knows that you are converting the type on purpose.
     
  5. ThePropagation

    ThePropagation

    Joined:
    May 11, 2015
    Posts:
    21
    None of these solutions are working. I cant paste the whole code but when i type

    int CharCount = Enum.GetValues(typeof(globalVar.Char)).length;

    I get an error, under Enum saying it doesnt exist in the current context.

    Casting an enum as int works but thats only half the solution, is there a library im not "using"? I am using System.Collections, System.Collections.Generic, UnityEngine, UnityEngine.UI, and UnityEngine.SceneManagement
     
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,615
    Maybe I don't understand what you are trying to do, but what is it that you need beyond simply casting the enums to ints?
     
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Enum is part of the "System" namespace, so add "using System;" I would recommend using an IDE that can suggest the missing namespace for you, very handy.

    Length needs to be capitalized also.

    He could definitely just cast the enums to ints, but the reason he was doing that in the first place was to get the "End" entry value, which represents the total count of entries in the enum. So I suggested a better method that doesn't require maintenance every time you add an enum entry.
     
    Joe-Censored likes this.
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I'd say those enums are the wrong approach anyways.

    Instead, build some custom types. That'll make everything easier.
     
    LiterallyJeff likes this.
  9. ThePropagation

    ThePropagation

    Joined:
    May 11, 2015
    Posts:
    21
    Sorry, im making a custom class array, and theres a thread on that as well that im following, but when i type the exact code they do i get errors.

    I made a custom type called Character, and adding attributes like attack, hp, etc. That works fine.

    Im trying to make an array that i can use a button to quick change between players and the ui will display that characters stats and they have different run speeds and jump heights etc. I get how to do it but i keep getting the syntax wrong i think.

    I cant paste the code still (im on mobile right now, code is on pc). Making the custom class with multiple types as attributes again, that works good.

    My problem is making a character array, assigning all the values, and i want to use an enum with all the characters names so i can easily switch between them and know who im switching to. Like i can have a public int CurrentChar. I want CurrentChar to decide what character is playable right now, hitting the switch character button tolls through this variable.

    I have a list of characters (0 is blank in case a party member slot is empty, 1 - 5 are the characters). I want CurrentChar to be manipulated and whatever CurrentChar is to be passed into my Character class array so if CurrentChar is 1 - 5 it will use that number as the array index to find its hp, jumpheight, etc.