Search Unity

lol what do you call this? - flag or enum dealy

Discussion in 'Scripting' started by CaoMengde777, Nov 26, 2015.

  1. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    okay so sometimes I use this kinda thing .. i been calling it a "Flag" but i know thats not right lol is it?
    i guess its like an enum without names
    for example:
    Code (CSharp):
    1. public int stanceFlag = 1;    // 0 = dead, 1 = standing, 2 = crouched, 3 = prone, 4 = blindFire, 5 = lastStand
    like, a Flag is this kinda thing
    Code (CSharp):
    1. //bread, ham, lettuce
    2. public int sandwichFlag = 13;  //1 = bread, 2 = mayo, 4 = ham, 8 = lettuce, 16 = pickles, 32 = mustard
    but whats the other thing? enum? or what?

    hmm i guess i should just use enum .. some reason im used to writing like this and makes more sense to me like this haha...
    idk its easier to pass an int around, enum has like boxxing ?
    but yeah enum shows my comment info in intellisense... LOLs good thing iam not working with a team huh? :p

    enum is just a coding shortcutty tool thing? like, the compiler turns it into basically the same as i use??
    .. i mean, iam thinking using "just an integer" is being more optimal, but .. it doesnt matter??
    Code (CSharp):
    1. [Flags]
    2. public enum CraftTypes
    3. {
    4.     Flag00 = (1 << 0),
     
    Last edited: Nov 26, 2015
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    I'd call it a type code, a state or a mode depending on how it was being used..
    Typically I try to avoid them or refactor them away.

    Anyways, I don't think it matters what you call it as long as you get the meaning across. Flag might be wrong though since it has a well defined meaning (your second example)
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    If you're going to do this, you should use the enum.

    Enums are just ints. They are just as optimal. But they are far easier to read.

    Code (csharp):
    1.  
    2. //a general enum
    3. public enum Stance
    4. {
    5.     Dead = 0,
    6.     Standing = 1,
    7.     Crouched = 2,
    8.     Prone = 3,
    9.     BlindFire = 4,
    10.     LastStand = 5
    11. }
    12.  
    13.  
    14. //a flagging enum, note you can attribute it as flagging to suppress warnings
    15. [System.Flags()]
    16. public enum Sandwich
    17. {
    18.     Bread = 1,
    19.     Mayo = 2,
    20.     Ham = 4,
    21.     Lettuce = 8,
    22.     Pickles = 16,
    23.     Mustard = 32
    24. }
    25.  
    26.  
    27. //when you create a group of flagging enums
    28. //you can OR the elements that a flagged enum have together to create a composite enum value
    29. Sandwich sandwich = Sandwich.Bread | Sandwich.Ham | Sandwich.Lettuce;
    30.  
    31. //it's called a mask when you operate one binary statement (an int for example) on another
    32. //to find the intersecting parts, the mask is the enum value you're testing for
    33. Sandwich a = Sandwich.Bread | Sandwich.Ham | Sandwich.Lettuce;
    34. Sandwich b = Sandwich.Pickles | Sandwich.Lettuce;
    35.  
    36. //here b is the mask
    37. if(a & b != 0)
    38. {
    39.     //a contains some or all elements of b
    40. }
    41. else
    42. {
    43.     //a contains no elements of b
    44. }
    45.  
    46. if(a & b == b)
    47. {
    48.     //a contains all elements of b
    49. }
    50. else
    51. {
    52.     //a contains no or some elements of b
    53. }
    54.  
    55. //here we can even make a mask for stance to test if a stance is one of many
    56. //note this only works if stance values are from 0->31, since int only has 32 bits to perform logic on
    57. Stance stance = Stance.Prone;
    58. int stanceMaskReceiver = 1 << (int)stance;
    59. int stanceMask = (1 << (int)Stance.Prone | 1 << (int)Stance.BlindFire);
    60. if((stanceMaskReceiver & stanceMask != 0)
    61. {
    62.     //our stance is either prone or blindfire
    63. }
    64.  
    65.  
    66. //the reason enums are preferred is that it becomes more human readable...
    67. switch(stance)
    68. {
    69.     case Stance.Dead:
    70.         //do dead stuff
    71.         break;
    72.     case Stance.Standing:
    73.         //do Standing stuff
    74.         break;
    75.     case Stance.Crouched:
    76.         //do Crouched stuff
    77.         break;
    78.     case Stance.Prone:
    79.         //do Prone stuff
    80.         break;
    81.     case Stance.BlindFire:
    82.         //do BlindFire stuff
    83.         break;
    84.     case Stance.LastStand:
    85.         //do LastStand stuff
    86.         break;
    87. }
    88.  
    89. //as opposed to:
    90. switch(stance)
    91. {
    92.     case 0:
    93.         //do dead stuff
    94.         break;
    95.     case 1:
    96.         //do Standing stuff
    97.         break;
    98.     case 2:
    99.         //do Crouched stuff
    100.         break;
    101.     case 3:
    102.         //do Prone stuff
    103.         break;
    104.     case 4:
    105.         //do BlindFire stuff
    106.         break;
    107.     case 5:
    108.         //do LastStand stuff
    109.         break;
    110. }
    111.  
    112.  
    113. //the other good thing is that you can't set an enum to a value that doesn't logically make sense without forcing it
    114. Stance stance = 7; //doesn't work, you have to cast
    115. Stance stance2 = (Stance)4; //works fine
    116. Stance stance3 = (Stance)7; //we forced 7 to be a stance, this will work... but you forced it, your fault...
    117. Stance stance4 = Stance.Prone; //the appropriate way to set stance
    118.  
    119.  
    120. //And lastly, you can control the integer type of the enum
    121. public enum SmallEnum : byte
    122. {
    123.     A = 0,
    124.     B = 1,
    125.     C = 2
    126. }
    127.  
    128. public enum LargeEnum : long
    129. {
    130.     A = 0,
    131.     ...
    132.     Z = 1749837589327518934
    133. }
    134.  
     
    Last edited: Nov 26, 2015
    Deleted User and Fab4 like this.
  5. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    yeah i was thinking State

    thanks for the enum clarification!

    yeah Mask, ive seen it called bitmask (the flags)
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Usually flag means that you've flagged for some value.

    Mask is the filter through which you pass flags to test for what is flagged.


    it makes sense when you look at the operation:

    Code (csharp):
    1.  
    2. 000100 //the value
    3. 010100 & //the mask
    4. ----------
    5. 000100
    6.  
    7. 000100 //the value
    8. 011000 & //the mask
    9. ---------
    10. 000000
    11.  
    the mask has holes in it (where the ones are), that allow the value of the thing its masking through only where its holes are.

    You can only see the eyes (flag) of the wearer, if the holes (mask) are located where the eyes are.
     
    Fab4 likes this.
  7. Kamilche_

    Kamilche_

    Joined:
    Jan 11, 2015
    Posts:
    75
    I never use flags, they're fiddly and subject to overflow and you always need more of them. If your project structure can handle it, I suggest using enums instead, or even simple constants.
     
  8. DeathQuake

    DeathQuake

    Joined:
    Oct 24, 2012
    Posts:
    64
    I remember doing this 4 or 5 years back when i started programming, I have always felt that Enums are just integers with name and are useless.
    I feel so stupid for that now :D
     
  9. Kamilche_

    Kamilche_

    Joined:
    Jan 11, 2015
    Posts:
    75
    Well, bit manipulation was handy back when you could have a whopping 2 BYTES! Count 'em! 2! To store additional data. But you know we don't need to program like that any more.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Bit manipulation is great if you want some speedy testing.

    Boolean logic is one of the fastest operations a processor can perform. Furthermore it translates from program to program smoothly as it's a direct binary statement, there's not obtuse structure that is determined by some 'class' or 'struct'.

    This is why things like the physics layers use a int mask to determine what is collidable.