Ranking System Based on player's current level state. [Solved]

Hello I would like to add a ranking system based on the player’s current level. I could use if statement for each level, but I want to keep the code optimized and small.

I tried a foreach statement but I could not get that working.

Here is the code so far.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerLeveler : MonoBehaviour
{
// Stats
private int level = 1;
public int ExpCurrent = 0;
public int ExpMax;
public List ranks; // Name of ranks
public List levelToNextRankList; // Level to get next rank required
public string currentRank;
public int maxLevel = 1000;

// ToNextLevel
public List expNextMax;

private void Start()
{
currentRank = ranks[level - 1];
}

private void Update()
{
if(Input.GetKeyDown(KeyCode.P))
{
LevelUp();
}
}

public void IncreaseEXP(int amount)
{
ExpCurrent += amount;

if (ExpCurrent >= ExpMax)
LevelUp();

}

void LevelUp()
{
level++;
ExpCurrent = 0;
ExpMax = expNextMax[level - 1];

// Here I would want to check if the player’s level is for example equal to 5 to change the rank from private to private first class (without an if statement so the code would not contain 40 if else statements
}

void RankUpReward(int Points, int RP)
{
Debug.Log(Points + " " + RP);
}
}

void LevelUp()
{
level++;
ExpCurrent = 0;
ExpMax = expNextMax[level - 1];

// Here I would want to check if the player’s level is for example equal to 5 to change the rank from private to private first class (without an if statement so the code would not contain 40 if else statements

//i might have done this wrong, this is my idea, may be flawed because i haven’t actually tested in an IDE

for(int i = 0; i < levelToNextRankList; i++)
{
if(level == levelToNextRankList*)*
{
currentRank = ranks
}
}
}

1 Like

preferably your code snipplets be in code tags.

You could use a switch/case statement instead, having each level be a case to change the rank. While its not very different from the if-else statement version, it is at least less of a hassle to write, and not as taxing as the if-else version

switch(level){
    case 1:
        //private rank
        break;
    case 5:
        //private first class rank
        break;
    ...
    default:
        break;
}

Although I suspect the following isnt for your case, it might still be interesting to know; If every level is its own rank this actually becomes easier, you can have an array of strings(or what your target type is), each with the rank name, and use the level as the array index to get the relevant rank.

//with your variables
public string[] ranks;//populated in inspector

//get rank from level
string rank = ranks[level]

Last would be something slightly more advanced, using dictionaries. One problem with dictionaries is they do not have a default inspector display, and cannot be serialized, which means you need to populate them on awake/start. If you can do that though, you can use your level as the key to get the rank value.

//with your variables
private Dictionary<int, string> ranks;//in this case int is the level key, string is the rank value

private void Start(){
    ranks = new Dictionary<int string>();
    ranks.Add(1,"private");
    ranks.Add(5,"private first class");
   ...
}

//get rank
string rank = ranks[level];

As you might be able to tell, every method still offloads the data, linking of level to rank, somewhere. Switch/case remains exactly the same, array offloads to the inspector, dictionaries offload to the awake/start functions. So if you truly have 40 ranks linked to 40 different levels numbers, the key concern should rather be making it easier to access that data.

1 Like

Thanks for your replies! They all helped. I might explore other avenues that might be less taxing, but I’ll stick with these ones for now.

Btw there is no reason to do this calculation in Monobehaviour script you can make some standard c# script and from Monobehaviour ask only for result into that script

Answer: The answer is not pretty but this is what I did.

void LevelUp()
{
if (level == 1000)
return;
level++;
ExpCurrent = 0;
ExpMax = expNextMax[level - 1];

EXPSlider.maxValue = ExpMax;
EXPSlider.value = ExpCurrent;

CheckForRankUp();

leveledUpGO.SetActive(true);
levelUpToTxt.text = currentRank + " : Level " + level;

StartCoroutine(UnloadLevelUpGO());

levelTmp.text = currentRank.ToString() + " : Level " + level;
}

void CheckForRankUp()
{
if(level == levelToNextRankList[0])
{
currentRank = ranks[0];
}
if(level == levelToNextRankList[1])
{
currentRank = ranks[1];
}
if (level == levelToNextRankList[2])
{
currentRank = ranks[2];
}
if (level == levelToNextRankList[3])
{
currentRank = ranks[3];
}
if (level == levelToNextRankList[4])
{
currentRank = ranks[4];
}
if (level == levelToNextRankList[5])
{
currentRank = ranks[6];
}
if (level == levelToNextRankList[7])
{
currentRank = ranks[7];
}
if (level == levelToNextRankList[8])
{
currentRank = ranks[8];
}
if (level == levelToNextRankList[9])
{
currentRank = ranks[9];
}
if (level == levelToNextRankList[10])
{
currentRank = ranks[10];
}
if(level == levelToNextRankList[11])
{
currentRank = ranks[11];
}
if (level == levelToNextRankList[12])
{
currentRank = ranks[12];
}
if (level == levelToNextRankList[13])
{
currentRank = ranks[13];
}
if (level == levelToNextRankList[14])
{
currentRank = ranks[14];
}
if (level == levelToNextRankList[15])
{
currentRank = ranks[15];
}
if (level == levelToNextRankList[16])
{
currentRank = ranks[16];
}
if (level == levelToNextRankList[17])
{
currentRank = ranks[17];
}
if (level == levelToNextRankList[18])
{
currentRank = ranks[18];
}
if (level == levelToNextRankList[19])
{
currentRank = ranks[19];
}
if (level == levelToNextRankList[20])
{
currentRank = ranks[20];
}
if (level == levelToNextRankList[21])
{
currentRank = ranks[21];
}
if (level == levelToNextRankList[22])
{
currentRank = ranks[22];
}
if (level == levelToNextRankList[23])
{
currentRank = ranks[23];
}
}