Home > other >  Is there any other way to do this?
Is there any other way to do this?

Time:11-24

I want to check if a player's XP is a certain amount, it will change the player's rank. Is there a more efficient way to do this?

Here is the code:

currentLevelText.text = currentLevel;

    if (currentLevel == "Rookie")
        xp  = Random.Range(3, 10);
    else if (currentLevel == "Survivor")
        xp  = Random.Range(5, 12); 
    else if (currentLevel == "Adventurer")
        xp  = Random.Range(7, 14); 
        
    // Please send help 
    if (xp <= 0 || xp ==   1 || xp ==  2 || xp == 3 || 
        xp == 4 || xp == 5 ||xp ==  6 || xp == 7 || xp == 8 ||
        xp == 9 || xp == 10 ||xp ==  11 || xp == 12 || xp == 13 || xp == 14 || 
        xp == 15 || xp == 16 || xp == 17 || xp == 18 || xp == 19 || xp ==  20 || xp == 
         21 
        || xp == 22 || xp == 23 || xp == 24) 
        
        currentLevel = "Rookie";
    
    
    else if (xp == 25 || xp == 26 || xp == 27 || xp == 28 || xp == 29 || xp == 30 )  
        currentLevel = "Survivor";
    
    else if (xp == 31 || xp == 32 || 
             xp == 33 || xp == 34 || xp == 35 
             || xp == 36 || xp == 37 || xp == 38 ||
             xp == 39 || xp == 40)
        
        currentLevel = "Adventurer";
    else if (xp < 100) {
        currentLevel = "Veteran"; 
    }

Thank you.

CodePudding user response:

You'll want to start out by understanding what < and > and <= and >= do. You don't need to check for every possible case of the integer value, just the range. You can likely accomplish this with a method like this:

    public string GetLevel(int xp)
    {
        if (xp <= 24) return "Rookie";
        if (xp <= 30) return "Survivor";
        if (xp <= 40) return "Adventurer";
        return "Veteran";
    }

CodePudding user response:

As seen in other answers you should write a function that calculates the rank from the xp value.

/// <summary>
/// Define the rank from the given xp.
/// </summary>
public static PlayerRank GetRankFromXp(int xp)
{
    // Novice: 1..15
    if (xp>0 && xp < 16) return PlayerRank.Novice;
    // Rookie: 16..23
    if (xp < 24) return PlayerRank.Rookie;
    // Survivor: 24..29
    if (xp < 30) return PlayerRank.Survivor;
    // Adventurer: 30..39
    if (xp < 40) return PlayerRank.Adventurer;
    // Veteran: 40..99
    if (xp < 100) return PlayerRank.Veteran;
    // God: >=100
    if (xp >= 100) return PlayerRank.God;
    // None: =0
    return PlayerRank.None;
}

but what is PlayerRank above? It is an enum which associates a name with some constant value.

public enum PlayerRank
{
    None,
    Novice,
    Rookie,
    Survivor,
    Adventurer,
    Veteran,
    God,
}

This way you don't have to deal with string values and accidentally trying to compare "Rookie" to "Rokie" or any other typo that might show up.

In addition to the above you should make a function that returns some random xp value based on the rank. This would be an excellent place for a switch() statement that checks against specific ranks.

/// <summary>
/// Returns a random amount of xp based on the rank.
/// </summary>
public static int RandomXp(PlayerRank rank)
{
    switch (rank)
    {
        case PlayerRank.Novice:
            return Random.Range(1, 8);
        case PlayerRank.Rookie:
            return Random.Range(3, 10);
        case PlayerRank.Survivor:
            return Random.Range(5, 12);
        case PlayerRank.Adventurer:
            return Random.Range(7, 14);
        case PlayerRank.Veteran:
            return Random.Range(9, 16);
        case PlayerRank.God:
            return Random.Range(11, 20);
        default:
            return 0;
    }
}

and some sample usage of the above would be in the function LevelUp() below:

public class Player
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Player"/> class.
    /// </summary>
    public Player()
    {
        Xp = 0;
        Rank = PlayerRank.None;
    }
    /// <summary>
    /// Gets or sets the xp.
    /// </summary>
    public int Xp { get; set; }
    /// <summary>
    /// Gets or sets the player rank.
    /// </summary>
    public PlayerRank Rank { get; set; }

    /// <summary>
    /// Levels up the player.
    /// </summary>
    public void LevelUp()
    {
        Xp  = RandomXp(Rank);
        Rank = GetRankFromXp(Xp);
    }

}

Note that when displaying an enum on the screen it automatically converts the name into the corresponding characters. For example PlayerRank.Novice.ToString() => "Novice"

CodePudding user response:

You can use this if-else block instead.

            if (xp <= 24 )
                currentLevel = "Rookie";
            else if (xp<=30)
                currentLevel = "Survivor";
            else if (xp<=40)
                currentLevel = "Adventurer";
            else                
                currentLevel = "Veteran";
            

there is no need to check each individual value, and there is no need to check previous conditions if you are in an "else" clause

  • Related