0

Currently to find my rank you have to go through this huge line of if statements, I was wondering if there a solution that could shrink this down in to just one pass, so instead of str, end, dex, ect.. being passed through in a bunch of if's, instead the a list called rank with a all the attributes going through and displaying the rank.

    [Range (0, 999)]
    public int strength;
    [Range (0, 999)]
    public int endurance;
    [Range (0, 999)]
    public int dexterity;
    [Range (0, 999)]
    public int agility;
    public int strengthRank;
    public int enduranceRank;
    public int dexterityRank;
    public int agilityRank;


    void Update () {
        Rank ();
    }

    void Rank () {
        strengthRank = (strength >= 0 && strength <= 99) ? 0 :
            (strength >= 100 && strength <= 199) ? 1 :
            (strength >= 200 && strength <= 299) ? 2 :
            (strength >= 300 && strength <= 399) ? 3 :
            (strength >= 400 && strength <= 499) ? 4 :
            (strength >= 500 && strength <= 599) ? 5 :
            (strength >= 600 && strength <= 699) ? 6 :
            (strength >= 700 && strength <= 799) ? 7 :
            (strength >= 800 && strength <= 899) ? 8 :
            (strength >= 900 && strength <= 999) ? 9 : 0;
        enduranceRank = (endurance >= 0 && endurance <= 99) ? 0 :
            (endurance >= 100 && endurance <= 199) ? 1 :
            (endurance >= 200 && endurance <= 299) ? 2 :
            (endurance >= 300 && endurance <= 399) ? 3 :
            (endurance >= 400 && endurance <= 499) ? 4 :
            (endurance >= 500 && endurance <= 599) ? 5 :
            (endurance >= 600 && endurance <= 699) ? 6 :
            (endurance >= 700 && endurance <= 799) ? 7 :
            (endurance >= 800 && endurance <= 899) ? 8 :
            (endurance >= 900 && endurance <= 999) ? 9 : 0;
        dexterityRank = (dexterity >= 0 && dexterity <= 99) ? 0 :
            (dexterity >= 100 && dexterity <= 199) ? 1 :
            (dexterity >= 200 && dexterity <= 299) ? 2 :
            (dexterity >= 300 && dexterity <= 399) ? 3 :
            (dexterity >= 400 && dexterity <= 499) ? 4 :
            (dexterity >= 500 && dexterity <= 599) ? 5 :
            (dexterity >= 600 && dexterity <= 699) ? 6 :
            (dexterity >= 700 && dexterity <= 799) ? 7 :
            (dexterity >= 800 && dexterity <= 899) ? 8 :
            (dexterity >= 900 && dexterity <= 999) ? 9 : 0;
        agilityRank = (agility >= 0 && agility <= 99) ? 0 :
            (agility >= 100 && agility <= 199) ? 1 :
            (agility >= 200 && agility <= 299) ? 2 :
            (agility >= 300 && agility <= 399) ? 3 :
            (agility >= 400 && agility <= 499) ? 4 :
            (agility >= 500 && agility <= 599) ? 5 :
            (agility >= 600 && agility <= 699) ? 6 :
            (agility >= 700 && agility <= 799) ? 7 :
            (agility >= 800 && agility <= 899) ? 8 :
            (agility >= 900 && agility <= 999) ? 9 : 0;

I'm wondering if I could get it to something like this

    [Range (0, 999)]
    public int strength;
    [Range (0, 999)]
    public int endurance;
    [Range (0, 999)]
    public int dexterity;
    [Range (0, 999)]
    public int agility;
    public int rank;


    void Update () {
        Rank ();
    }

    void Rank () {
        rank = (x >= 0 && x <= 99) ? 0 :
            (x >= 100 && x <= 199) ? 1 :
            (x >= 200 && x <= 299) ? 2 :
            (x >= 300 && x <= 399) ? 3 :
            (x >= 400 && x <= 499) ? 4 :
            (x >= 500 && x <= 599) ? 5 :
            (x >= 600 && x <= 699) ? 6 :
            (x >= 700 && x <= 799) ? 7 :
            (x >= 800 && x <= 899) ? 8 :
            (x >= 900 && x <= 999) ? 9 : 0;
Loris Securo
  • 7,538
  • 2
  • 17
  • 28
  • Isn't `rank` just the same as `x / 100`? What is it exactly you need help with? – Peter Duniho Oct 18 '20 at 23:21
  • I want my stats to update in Realtime so its expensive to run all these if statement at once, basically what's happening my Strength, Endurance, Dexterity, Agility and Magic stats 0-999 are being put there and finding what rank 1-9 that stat would be on. – RenderDev Oct 18 '20 at 23:27
  • There's a huge difference between _"how to do this?"_ and _"this is too slow"_. Fact is, I still don't see why you can't just divide by 100 (per the first duplicate listed). But if you have something that you require a _performance_ improvement on, you need to provide more details, including a [mcve], a detailed explanation of what your _measured_ performance is, what your expected performance is, what you've done so far to try to improve the performance, and what _specifically_ you need help with. – Peter Duniho Oct 18 '20 at 23:29

0 Answers0