1

How to make this shorter? I think I might an array, but I don't know how to set it up. Here is the code and this is what it needs to do when you get certain amount coins: it increases the health by 10 and saves the health and then it changes the text that displays how many coins you need for next upgrade.

void HpUpgradelvl()

 {

     if (PlayerControl.coins >= 30)
     {
         healthPoints = healthPoints + 10;
         PlayerPrefs.SetFloat("HealthUP", healthPoints);
         if (PlayerControl.coins >= 60)
         {
             txt.SetActive(false);
             txt1.SetActive(true);
             txt2_0.SetActive(false);
             txt2_1.SetActive(true);
             healthPoints = healthPoints + 10;
             PlayerPrefs.SetFloat("HealthUP", healthPoints);
             if (PlayerControl.coins >= 100)
             {
                 txt1.SetActive(false);
                 txt2.SetActive(true);
                 txt2_1.SetActive(false);
                 txt2_2.SetActive(true);
                 healthPoints = healthPoints + 10;
                 PlayerPrefs.SetFloat("HealthUP", healthPoints);
                 if (PlayerControl.coins >= 150)
                 {
                     txt2.SetActive(false);
                     txt3.SetActive(true);
                     txt2_2.SetActive(false);
                     txt2_3.SetActive(true);
                     healthPoints = healthPoints + 10;
                     PlayerPrefs.SetFloat("HealthUP", healthPoints);
                     if (PlayerControl.coins >= 210)
                     {
                         txt3.SetActive(false);
                         txt4.SetActive(true);
                         txt2_3.SetActive(false);
                         txt2_4.SetActive(true);
                         healthPoints = healthPoints + 10;
                         PlayerPrefs.SetFloat("HealthUP", healthPoints);
                         if (PlayerControl.coins >= 280)
                         {
                             txt4.SetActive(false);
                             txt5.SetActive(true);
                             txt2_4.SetActive(false);
                             txt2_5.SetActive(true);
                             healthPoints = healthPoints + 10;
                             PlayerPrefs.SetFloat("HealthUP", healthPoints);
                             if (PlayerControl.coins >= 360)
                             {
                                 txt5.SetActive(false);
                                 txt6.SetActive(true);
                                 txt2_5.SetActive(false);
                                 txt2_6.SetActive(true);
                                 healthPoints = healthPoints + 10;
                                 PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                 if (PlayerControl.coins >= 450)
                                 {
                                     txt6.SetActive(false);
                                     txt7.SetActive(true);
                                     txt2_6.SetActive(false);
                                     txt2_7.SetActive(true);
                                     healthPoints = healthPoints + 10;
                                     PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                     if (PlayerControl.coins >= 500)
                                     {
                                         txt7.SetActive(false);
                                         txt8.SetActive(true);
                                         txt2_7.SetActive(false);
                                         txt2_8.SetActive(true);
                                         healthPoints = healthPoints + 10;
                                         PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                         if (PlayerControl.coins >= 610)
                                         {
                                             txt8.SetActive(false);
                                             txt9.SetActive(true);
                                             txt2_8.SetActive(false);
                                             txt2_9.SetActive(true);
                                             healthPoints = healthPoints + 10;
                                             PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                             if (PlayerControl.coins >= 730)
                                             {
                                                 txt9.SetActive(false);
                                                 txt10.SetActive(true);
                                                 txt2_9.SetActive(false);
                                                 txt2_10.SetActive(true);
                                                 healthPoints = healthPoints + 10;
                                                 PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                                 if (PlayerControl.coins >= 860)
                                                 {
                                                     txt10.SetActive(false);
                                                     txt11.SetActive(true);
                                                     txt2_10.SetActive(false);
                                                     txt2_11.SetActive(true);
                                                     healthPoints = healthPoints + 10;
                                                     PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                                     if (PlayerControl.coins >= 1000)
                                                     {
                                                         txt11.SetActive(false);
                                                         txtmax.SetActive(true);
                                                         imgmax.SetActive(false);
                                                         txt2_11.SetActive(false);
                                                         txtmax2.SetActive(true);
                                                         imgmax2.SetActive(false);
                                                         healthPoints = healthPoints + 10;
                                                         PlayerPrefs.SetFloat("HealthUP", healthPoints);
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     else healthPoints = 100;
 }  
1
  • So, basically, you need to map the value of PlayControl.coins to a value to set healthPoints? Commented Dec 15, 2015 at 19:36

2 Answers 2

2

There are a few different ways to handle this, but perhaps the simplest would be to start with an array with your thresholds (make sure it's sorted smallest to highest):

var thresholds = new int[] { 30,60,100,150,210,...};

And then instead of that horribly nested set of ifs, you can just loop through your array:

int i = 0;
while (PlayerControl.coins < thresholds[i++])
{
    healthpoints += 10;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's a little difficult to understand the logic of what you are trying to do with them from your question, but you could certainly put them in an array too and activate them as needed.
0

I realized that the level requirements you set (up to 450 at least) follow a certain pattern. The requirement per level is always increasing by 10. I think this is a simple but effective way to manage XP-requirements.
One of the advantages of this pattern is, that it can be expressed by a quadratic function. So I wrote a method which generates and solves such a quadric function.
You feed it the essential function parameters (base, growth) and the current points (in this case gold), and it returns the level the player has reached.

To give you an example, the values I set in the constants below nearly match the requirements you first listed. They are:

  1. 30
  2. 70
  3. 130
  4. 200
  5. 280...

I also used a Property to keep the HP in sync with the PlayerPrefs. You could add a backing variable here, to keep from reading from the PlayerPrefs all the time.

public class HpManager : MonoBehaviour
{
    public int HealthPoints 
    { 
        get 
        { 
            PlayerPrefs.GetFloat("HealthUP")
        }
        set 
        { 
            PlayerPrefs.SetFloat("HealthUP", value); 
        } 
    }   

    private const uint BASE_HEALTH_POINTS = 100;            // Your HP for level 0
    private const uint HEALTH_POINTS_PER_LEVEL = 10;        // The amount the HP increase with each level

    private const uint BASE_LEVEL_REQUIREMENT = 30;     // The gold required to reach level 1
    private const uint PER_LEVEL_INCREASE = 10;         // The amount the level up requirement increases with every level
    private const uint MAX_LEVEL = 99;


    private void Start()
    {
        HealthPoints = BASE_HEALTH_POINTS;              // Set a base value for your HP
    }

    void HpUpgradelvl()
    {
        int level = GetLevel((uint)PlayerControl.coins, BASE_LEVEL_REQUIREMENT, PER_LEVEL_INCREASE);
        level = Mathf.Clamp(0, MAX_LEVEL);

        HealthPoints = BASE_HEALTH_POINTS + level * HEALTH_POINTS_PER_LEVEL;
    }   

    private int GetLevel(uint points, uint baseRequirement, uint increaseRequirement)
    {
        float a = 0.5f * increaseRequirement;
        float b = baseRequirement - a;

        // Convert quadratic equation "l = ax² + bx" into normalized form "0 = x² + px + q"
        float p = b / a;                                                
        float q = -points / a;                                          // Bring across and normalize
        float level = -p / 2f + Mathf.Sqrt((p * p) / 4f - q);               // Solve for positive value       

        return Mathf.FloorToInt(level);                                     // Return reached level
    }
}

I would need more info to help you with the texts.
As is, it seems overcomplicated. I can't imagine that you would actually need all those separate text-objects.
What exactly is it, that you are trying to do here?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.