Search Unity

Change enum value without unity messing everything up?

Discussion in 'Scripting' started by Leonetienne500, Jul 25, 2018.

  1. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    130
    Hey, i have my enumeration for guns like:

    Code (CSharp):
    1. public enum GUN_ID
    2.     {
    3.         NIL,
    4.         TORCH,
    5.         SHOTGUN_GSG,
    6.         PISTOL_BURST_TAG3,
    7.         SMG_MOLACH,
    8.         PISTOL_M1911,
    9.         AR_AK47,
    10.         AR_AK420,
    11.         RIFLE_L96,
    12.         PISTOL_LILDETROIT,
    13.         AR_M16,
    14.         SMG_UMP,
    15.         SHOTGUN_MOSSBERG
    16.     }

    Now, i want to remove the damn torch. How can i do this without unity swapping every damn value of every gun?

    My TAG-3 pistol has, of course, the public GUN_ID PISTOL_BURST_TAG3.
    If i just were to remove the toch entry, it would shift one higher to SMG_MOLACH, just as every other gun.
    Then i have to reset every damn value on every damn script that needs the gun id!

    Can i somehow make unity to save the enum value as a string instead of a number?
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    You can assign values to enums:
    Code (CSharp):
    1. public enum GUN_ID
    2. {
    3.     NIL,
    4.     // TORCH,   <= removed
    5.     SHOTGUN_GSG = 2,
    6.     PISTOL_BURST_TAG3,
    7.     SMG_MOLACH,
    8.     PISTOL_M1911,
    9.     AR_AK47,
    10.     AR_AK420,
    11.     RIFLE_L96,
    12.     PISTOL_LILDETROIT,
    13.     AR_M16,
    14.     SMG_UMP,
    15.     SHOTGUN_MOSSBERG
    16. }
     
  3. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    130

    why havent i thought of this before?! thanks! :D
     
    Doug_B likes this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, but it should! Please vote for the feedback issue on this.
     
  5. DAh-Right

    DAh-Right

    Joined:
    Mar 9, 2015
    Posts:
    28
    You can convert enum from numeric to string by standart "build in" conversion to string like :
    Code (CSharp):
    1. YouEnum = GUN_ID.PISTOL_LILDETROIT;
    2. string Uotput = YouEnum.ToString(); // Uotput  = "PISTOL_LILDETROIT";
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You certainly can, but that doesn't make Unity do so, and really has nothing to do with what this thread is about.
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Really ? In that case, how to rename enum value without messing everything up?
     
    lordofduct likes this.
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Agreed, there's a work around for if you want to add/remove entries. You just assign the numbers explicitly.

    But if you had to rename, there's no work around if it saved by name.

    Go with the option that does the least harm.
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    As discussed above. You can assign explicit numeric values to your enums, and then inserting or deleting ones won't mess everything up. Renaming them already works fine, since Unity stores only the number, not the name.

    The feedback site is gone now, so I can't get on your case for not reading the feedback request. :) What they should do is this: store the name and number, matching by name whenever possible, and falling back to number otherwise. This would handle both cases, but in particular correctly handle the currently-disasterous case where you have not explicitly assigned values to your enums, and then innocently insert or remove one, causing incorrect enum assignments all over your project.
     
  10. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Have you thought of using scriptable objects for your enumetations instead?

    Code (CSharp):
    1. [CreateAssetMenu(fileName = "New Item Type", menuName = "My Stuff//Create New Item Type")]
    2. public class MyItemEnum : ScriptableObject
    3. {
    4. //whatever other data you want here.
    5. }
    Then you can create as many types as you want, with whatever additional data you want, and just compare the asset instance to the value your looking for.
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Mind you, and as you pointed out the request is missing, you said:

    To the statement:
    It wasn't a combined thing, but an either or.

    Do both string & number in the serialized data could give you both options. But that's not what was mentioned in the context we read. And I could think of many reasons why Unity probably wouldn't do that do to it requiring adhoc logic for enums specifically (mind you int falls out of it naturally because enums are really just integer types byte/short/int/long depending what you define it as, defaulting to int... so no adhoc logic to get that int value). But I mean... any overheads it created wouldn't necessarily hurt in the grand scheme of things and I wouldn't complain if it were done that way... I just wouldn't vote to do it that way.

    Regardless though, that's not what was said. As far as palex-nx and I could discern from the information made available.