Search Unity

There HAS to be an easier way to do this that I can get my beginner head around... C#

Discussion in 'Scripting' started by Roughrider, May 24, 2016.

  1. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    Warning massive very simple function incoming :) I've been digging and digging trying to find out how to grab a range of integers within a single if statement. I'm coming up short applicable to my situation. Hopefully someone can point me in the right direction. Also looking for a clamp at the end of this thing (see comment.)

    Thanks folks!
    Code (CSharp):
    1.     private void ToHitRoll()
    2.     {
    3.         //zero modifier, natural roll only
    4.         D20 = Random.Range (1,21);
    5.         hitRoll = D20;
    6.  
    7.         if (hitRoll == 1 )
    8.         {
    9.             toHitFumble = true;
    10.             hitCondString = "FUMBLE";
    11.             fumbleTime = 2; //the next attack takes 2x the weapondelay to happen
    12.         }
    13.         if (hitRoll == 20) {
    14.             toHitCrit = true;
    15.             hitCondString = "CRITICAL";
    16.         }
    17.            
    18.         //modifiers added
    19.         else
    20.         {
    21.             hitRoll = D20 + playerStats.toHit;
    22.         }
    23.  
    24.         if (hitRoll == 2)
    25.         {
    26.             toHitMiss = true;
    27.             hitCondString = "...a swing and a miss...";
    28.         }
    29.         if (hitRoll == 3)
    30.         {
    31.             toHitMiss = true;
    32.             hitCondString = "...a swing and a miss...";
    33.         }
    34.         if (hitRoll == 4)
    35.         {
    36.             toHitMiss = true;
    37.             hitCondString = "...a swing and a miss...";
    38.         }
    39.         if (hitRoll == 5)
    40.         {
    41.             toHitMiss = true;
    42.             hitCondString = "...a swing and a miss...";
    43.         }
    44.         if (hitRoll == 6)
    45.         {
    46.             toHitMiss = true;
    47.             hitCondString = "...a swing and a miss...";
    48.         }
    49.         if (hitRoll == 7)
    50.         {
    51.             toHitMiss = true;
    52.             hitCondString = "...a swing and a miss...";
    53.         }
    54.         if (hitRoll == 8)
    55.         {
    56.             toHitGlancing = true;
    57.             hitCondString = "... a glancing blow...";
    58.         }
    59.         if (hitRoll == 9)
    60.         {
    61.             toHitGlancing = true;
    62.             hitCondString = "... a glancing blow...";
    63.         }
    64.         if (hitRoll == 10)
    65.         {
    66.             toHitGlancing = true;
    67.             hitCondString = "... a glancing blow...";
    68.         }
    69.         if (hitRoll == 11)
    70.         {
    71.             toHitGlancing = true;
    72.             hitCondString = "... a glancing blow...";
    73.         }
    74.         if (hitRoll == 12)
    75.         {
    76.             toHitGlancing = true;
    77.             hitCondString = "... a glancing blow...";
    78.         }
    79.         if (hitRoll == 13)
    80.         {
    81.             toHitHit = true;
    82.             hitCondString = "...a good strike...";
    83.         }
    84.         if (hitRoll == 14)
    85.         {
    86.             toHitHit = true;
    87.             hitCondString = "...a good strike...";
    88.         }
    89.         if (hitRoll == 15)
    90.         {
    91.             toHitHit = true;
    92.             hitCondString = "...a good strike...";
    93.         }
    94.         if (hitRoll == 16)
    95.         {
    96.             toHitHit = true;
    97.             hitCondString = "...a good strike...";
    98.         }
    99.         if (hitRoll == 17)
    100.         {
    101.             toHitHit = true;
    102.             hitCondString = "...a good strike...";
    103.         }
    104.         if (hitRoll == 18)
    105.         {
    106.             toHitSolid = true;
    107.             hitCondString = "...a solid hit...";
    108.         }
    109.         if (hitRoll == 19)
    110.         {
    111.             toHitSolid = true;
    112.             hitCondString = "...a solid hit...";
    113.  
    114.             //how do I clamp the range here if the "hitRoll" + "playerStats.toHit" exceeds 19?
    115.         }
    116.     }
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You probably simply want > and <. Try this for size.

    Code (CSharp):
    1.     private void ToHitRoll()
    2.     {
    3.         //zero modifier, natural roll only
    4.         hitRoll = Random.Range (1,21);
    5.  
    6.         if (hitRoll == 1 )
    7.         {
    8.             toHitFumble = true;
    9.             hitCondString = "FUMBLE";
    10.             fumbleTime = 2; //the next attack takes 2x the weapondelay to happen
    11.         }
    12.         else if (hitRoll == 20) {
    13.             toHitCrit = true;
    14.             hitCondString = "CRITICAL";
    15.         }
    16.         else
    17.         {
    18.             hitRoll += playerStats.toHit;
    19.         }
    20.  
    21.         if (hitRoll <= 7)
    22.         {
    23.             toHitMiss = true;
    24.             hitCondString = "...a swing and a miss...";
    25.         }
    26.         else if (hitRoll <= 12)
    27.         {
    28.             toHitGlancing = true;
    29.             hitCondString = "... a glancing blow...";
    30.         }
    31.         else if (hitRoll <= 17)
    32.         {
    33.             toHitHit = true;
    34.             hitCondString = "...a good strike...";
    35.         }
    36.         else
    37.         {
    38.             toHitSolid = true;
    39.             hitCondString = "...a solid hit...";
    40.         }
    41.     }
    Note this is simply explaining how to use else if and <=. For better code you might want to look up enums as well.

    Also note as written the values assigned to hitCondString on a natural 1 or 20 will be overwritten by the next if statement.
     
  3. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    Thank BM. I was certain I tried this exact code, except unity kept tossing me errors saying I couldn't use < or > operands on integers. I got this error again after injecting this code, until I closed down and reopened monodevelop. GRRR. This certainly shortens it up, but now I'm stuck without a crit or fumble (just the string val) unless D20 hits it on the very first call to the function, just as you said :(
    Any ideas?
     
    Last edited: May 24, 2016
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Simplest way would be to return as soon as a critical hit or fumble occurs.
     
  5. Roughrider

    Roughrider

    Joined:
    Jul 17, 2011
    Posts:
    73
    Got it. Could someone double check me? I still have to eval a natural fumble, but was super excited that I got my natural 20 to fire while still clamping the last condition. Yea, beginner excitement - while the rest of you guys roll your eyes :)
    Code (CSharp):
    1. private void ToHitRoll()
    2.     {
    3.         //zero modifier, natural roll only
    4.         toHitFumble = false ;
    5.         toHitMiss = false ;
    6.         toHitGlancing = false ;
    7.         toHitHit = false ;
    8.         toHitSolid = false ;
    9.         toHitCrit = false ;
    10.         naturalTwenty = false;
    11.  
    12.         hitRoll = Random.Range (1,21);
    13.  
    14.         if (hitRoll == 20)
    15.         {
    16.             naturalTwenty = true;
    17.         }
    18.         else
    19.         {
    20.             naturalTwenty = false;
    21.         }
    22.          
    23.         hitRoll += playerStats.toHit;
    24.  
    25.         if (naturalTwenty)
    26.         {
    27.             toHitCrit = true;
    28.             hitCondString = "CRITICAL";
    29.         }
    30.         else if (hitRoll <= 1)
    31.         {
    32.             toHitFumble = true;
    33.             hitCondString = "FUMBLE";
    34.             fumbleTime = 2;
    35.         }
    36.         else if (hitRoll <= 7)
    37.         {
    38.             toHitMiss = true;
    39.             hitCondString = "...a swing and a miss...";
    40.         }
    41.         else if (hitRoll <= 12)
    42.         {
    43.             toHitGlancing = true;
    44.             hitCondString = "... a glancing blow...";
    45.         }
    46.         else if (hitRoll <= 17)
    47.         {
    48.             toHitHit = true;
    49.             hitCondString = "...a good strike...";
    50.         }
    51.         else
    52.         {
    53.             toHitSolid = true;
    54.             hitCondString = "...a solid hit...";
    55.         }
    56.     }