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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

While Loop c#

Discussion in 'Scripting' started by DeeJayVee, Apr 7, 2018.

  1. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Fixed by Pagefile, for any one reading this the answer is the logic of the while loop.

    (While (Current_Exp >= Current_Exp_Max)

    Hey.

    I am working on an experience gain script. I feel like this should work but it seems to play an extra loop which
    I don't understand.

    Code (CSharp):
    1. int Exp_Curve()
    2.     {
    3.         return ((Level * 50) * (Level * exp_curve));
    4.     }
    This is the experience curve and for reference I've set "exp_curve" to 2.

    So Level 1 : 100 exp, Level 2 : 400 exp, Level 3 : 900 exp.

    the code I am using is ;
    Code (CSharp):
    1. public void Gain_Exp(int exp_gained)
    2.     {
    3.  
    4.         Current_Exp += exp_gained;
    5.      
    6.         while (Current_Exp >= Exp_Needed)
    7.         {
    8.        
    9.             Level += 1;
    10.             Debug.Log(Level);
    11.             Current_Exp -= Current_Exp_Max;
    12.             Current_Exp_Max = Exp_Curve();
    13.             Exp_Needed = Current_Exp_Max - Current_Exp;
    14.             Debug.Log("Current_Exp : " + Current_Exp);
    15.  
    16.         }
    17.  
    18.         if (Current_Exp < 0)
    19.         {
    20.             Current_Exp = 0;
    21.         }
    22.  
    23.  
    24.     }
    Level 1-2 works fine, but when it gets to level 3 itshould have level 3 with 400 exp to go but instead it makes the current exp -400 and changes the level to 4.
     
    Last edited: Apr 7, 2018
  2. Pagefile

    Pagefile

    Joined:
    Feb 10, 2013
    Posts:
    51
    Are you sure you want to subtract Current_Exp_Max from Current_Exp instead of Exp_Needed? If the player has 50 Exp, and you call Gain_Exp(50), they will level up, but the total Exp needed to level will be subtracted leaving them at -50. Any time a full level isn't gained all at once Exp is lost. And if you're going to only go one level at a time, you would just need to set Exp_Needed = Exp_Curve().
     
  3. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Well yeah, because the while loop should only run if Current_Exp is higher then or = Exp_Needed

    Before the while loop is executed the new experience gained is added onto current exp, so if the player has 50 exp and then gains 50 it would be equal to current max, so the while loop would then subtract 100 leaving 0.

    I'm not sure how that would equal a -50
     
  4. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    Just a friendly note that imho while loops cause crashes in unity. That’s not to say that you can’t use while but I’ve found that any malformed while loop even while testing can crash the editor. I suppose the infinite loop is the issue but I just tend to use alternatives and avoid them all together.
     
    taiyotagami likes this.
  5. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Thanks. I had read that infinite loops are an issue. But this isn't an infinite loop, it has an exit clause.
     
    NicBischoff likes this.
  6. Pagefile

    Pagefile

    Joined:
    Feb 10, 2013
    Posts:
    51
    Whoops, my mistake. Going up less than a level does work. It's fractional levels more than one that's the problem. Change the while to check for Current_Exp_Max
    Code (csharp):
    1. while(Current_Exp >= Current_Exp_Max)
    2. {
    3.                 Level += 1;
    4.                 Current_Exp -= Current_Exp_Max;
    5.                 Current_Exp_Max = Exp_Curve();
    6.                 Exp_Needed = Current_Exp_Max - Current_Exp;
    7. }
    You're values are all correct, but your logic wasn't. If you add 1200 Exp from level 1 at 0 exp, you'll end up with -200 because on the last iteration Current_Exp is 700 and Exp_Needed is 200. Those are both true, since the total Exp needed for the next level is 900, but since 700 is greater than 200 they are granted the level anyways and 900 is subtracted from the current Exp.
     
    DeeJayVee likes this.
  7. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Oh wow. You're right, that was the issue, man I was staring at that for ages, like I knew I was close.
    Thank you very much for pointing that out.

    It feels so obvious now :p

    I guess I also don't really need Exp_Needed. But I may keep it in there in case anything would reference it later on
     
    Last edited: Apr 7, 2018
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,399
    A while loop is just a loop, nothing special about it. You can make any loop an infinite loop. The thing is, don't do that. ;) While loops themselves never cause crashes; there is no reason to avoid them.

    --Eric
     
    DeeJayVee likes this.