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

Warning massive very simple function incoming :slight_smile: 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!

    private void ToHitRoll()
    {
        //zero modifier, natural roll only
        D20 = Random.Range (1,21);
        hitRoll = D20;

        if (hitRoll == 1 )
        {
            toHitFumble = true;
            hitCondString = "FUMBLE";
            fumbleTime = 2; //the next attack takes 2x the weapondelay to happen
        }
        if (hitRoll == 20) {
            toHitCrit = true;
            hitCondString = "CRITICAL";
        }
           
        //modifiers added
        else
        {
            hitRoll = D20 + playerStats.toHit;
        }

        if (hitRoll == 2)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        if (hitRoll == 3)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        if (hitRoll == 4)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        if (hitRoll == 5)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        if (hitRoll == 6)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        if (hitRoll == 7)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        if (hitRoll == 8)
        {
            toHitGlancing = true;
            hitCondString = "... a glancing blow...";
        }
        if (hitRoll == 9)
        {
            toHitGlancing = true;
            hitCondString = "... a glancing blow...";
        }
        if (hitRoll == 10)
        {
            toHitGlancing = true;
            hitCondString = "... a glancing blow...";
        }
        if (hitRoll == 11)
        {
            toHitGlancing = true;
            hitCondString = "... a glancing blow...";
        }
        if (hitRoll == 12)
        {
            toHitGlancing = true;
            hitCondString = "... a glancing blow...";
        }
        if (hitRoll == 13)
        {
            toHitHit = true;
            hitCondString = "...a good strike...";
        }
        if (hitRoll == 14)
        {
            toHitHit = true;
            hitCondString = "...a good strike...";
        }
        if (hitRoll == 15)
        {
            toHitHit = true;
            hitCondString = "...a good strike...";
        }
        if (hitRoll == 16)
        {
            toHitHit = true;
            hitCondString = "...a good strike...";
        }
        if (hitRoll == 17)
        {
            toHitHit = true;
            hitCondString = "...a good strike...";
        }
        if (hitRoll == 18)
        {
            toHitSolid = true;
            hitCondString = "...a solid hit...";
        }
        if (hitRoll == 19)
        {
            toHitSolid = true;
            hitCondString = "...a solid hit...";

            //how do I clamp the range here if the "hitRoll" + "playerStats.toHit" exceeds 19?
        }
    }

You probably simply want > and <. Try this for size.

    private void ToHitRoll()
    {
        //zero modifier, natural roll only
        hitRoll = Random.Range (1,21);

        if (hitRoll == 1 )
        {
            toHitFumble = true;
            hitCondString = "FUMBLE";
            fumbleTime = 2; //the next attack takes 2x the weapondelay to happen
        }
        else if (hitRoll == 20) {
            toHitCrit = true;
            hitCondString = "CRITICAL";
        }
        else
        {
            hitRoll += playerStats.toHit;
        }

        if (hitRoll <= 7)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        else if (hitRoll <= 12)
        {
            toHitGlancing = true;
            hitCondString = "... a glancing blow...";
        }
        else if (hitRoll <= 17)
        {
            toHitHit = true;
            hitCondString = "...a good strike...";
        }
        else
        {
            toHitSolid = true;
            hitCondString = "...a solid hit...";
        }
    }

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.

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 :frowning:
Any ideas?

Simplest way would be to return as soon as a critical hit or fumble occurs.

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 :slight_smile:

private void ToHitRoll()
    {
        //zero modifier, natural roll only
        toHitFumble = false ;
        toHitMiss = false ;
        toHitGlancing = false ;
        toHitHit = false ;
        toHitSolid = false ;
        toHitCrit = false ;
        naturalTwenty = false;

        hitRoll = Random.Range (1,21);

        if (hitRoll == 20)
        {
            naturalTwenty = true;
        }
        else
        {
            naturalTwenty = false;
        }
          
        hitRoll += playerStats.toHit;

        if (naturalTwenty)
        {
            toHitCrit = true;
            hitCondString = "CRITICAL";
        }
        else if (hitRoll <= 1)
        {
            toHitFumble = true;
            hitCondString = "FUMBLE";
            fumbleTime = 2;
        }
        else if (hitRoll <= 7)
        {
            toHitMiss = true;
            hitCondString = "...a swing and a miss...";
        }
        else if (hitRoll <= 12)
        {
            toHitGlancing = true;
            hitCondString = "... a glancing blow...";
        }
        else if (hitRoll <= 17)
        {
            toHitHit = true;
            hitCondString = "...a good strike...";
        }
        else
        {
            toHitSolid = true;
            hitCondString = "...a solid hit...";
        }
    }