Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

EXP/Level system help fix code [SOLVED]

Discussion in 'Scripting' started by Collin__Patrick, Apr 2, 2016.

  1. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    I am trying to create a level system for my game, but i quickly found out I cannot "math" today and have no idea why this is not working.

    Code (CSharp):
    1. public static void ZepherAddExp(int EXP){
    2.         CharacterInformation.ZepherCurrentEXP += EXP;
    3.         Debug.Log ("Zepher gained " + EXP + " EXP!");
    4.         for (int i = CharacterInformation.ZepherCurrentEXP; i >= CharacterInformation.ZepherNeededEXP; i -= CharacterInformation.ZepherNeededEXP) {
    5.             CharacterInformation.ZepherLevel ++;
    6.             Debug.Log("Zepher leveled up to " + CharacterInformation.ZepherLevel);
    7.             CharacterInformation.ZepherCurrentEXP -= CharacterInformation.ZepherNeededEXP;
    8.             CharacterInformation.ZepherNeededEXP += 50;
    9.         }
    10.     }
    In the end after adding 10,000 EXP to test my character reaches level 18 starting from 1 and NeededEXP starting at 100. My final result is 1,500 stored in CurrentEXP and 950 stored in NeededEXP.

    I have been sitting here for bout 2 hours hitting my head on my keyboard. I am sure that it is a simple fix and any help is appreciated.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,903
    Changing it to while loop goes to lvl19,
    with 1000 in neededExp and 550 in currentExp.

    Code (CSharp):
    1. while (ZepherCurrentEXP>= ZepherNeededEXP)
    2. {
    3. ..
    4. }
     
  3. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Can i suggest just setting neededexp to 0 instead first off.

    And i feel like youll end up with some exp not being used as you dont add the remainder to anything after youve calculated the amount of levels?

    Edit: hold on, im totally wrong. I see what youre doing here.

    I will be stealing this code when its fixed, very elegant solution. ;p
     
    Last edited: Apr 2, 2016
  4. Collin__Patrick

    Collin__Patrick

    Joined:
    May 20, 2015
    Posts:
    188
    No problem. There wouldn't be much of a community if people kept all of their code to themselves.

    here is the fixed and working version:

    Code (CSharp):
    1. public static void ZepherAddExp(int EXP){
    2.         CharacterInformation.ZepherCurrentEXP += EXP;
    3.         Debug.Log ("Zepher gained " + EXP + " EXP!");
    4.         while (CharacterInformation.ZepherCurrentEXP >= CharacterInformation.ZepherNeededEXP) {
    5.             CharacterInformation.ZepherLevel ++;
    6.             Debug.Log("Zepher leveled up to " + CharacterInformation.ZepherLevel);
    7.             CharacterInformation.ZepherCurrentEXP -= CharacterInformation.ZepherNeededEXP;
    8.             CharacterInformation.ZepherNeededEXP += 50;
    9.         }
    10.     }
     
    HAlbera likes this.
  5. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Outstanding!

    Thanks for the follow-up!