Search Unity

Adding one int to another?

Discussion in 'Scripting' started by Studio_Akiba, Mar 5, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    I am looking to add an increment to an XP system we are in the early stages of developing, and essentially what I want to do, is for every level the player goes up, to add an amount to the required XP to level up again, I have most of this working, it is just the "adding an int to another" bit I can't quite work out.
    Anyone got a solution? Code posted below:

    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.Canvas;
    3.  
    4. var experiencePoints : int;
    5. var playerLevel : int;
    6.  
    7. var initialXP : int=1000;
    8. var XPVariation : int;
    9.  
    10. var UICanvas : Canvas;
    11. var expText : Text;
    12. var levelText : Text;
    13.  
    14. function Start () {
    15.     expText.text = "Exp: ";
    16.     levelText.text = "Level: ";
    17.     //UICanvas.RenderMode(RenderMode.WorldSpace);
    18. }
    19.  
    20. function Update ()
    21. {
    22.     playerLevel = Mathf.Abs(experiencePoints / initialXP);
    23.    
    24.     expText.text = "Exp: " + experiencePoints;
    25.     levelText.text = "Level: " + playerLevel;
    26.    
    27.     if(playerLevel +1)
    28.     {
    29.         initialXP + XPVariation; //THIS IS THE LINE THAT DOESN'T WORK
    30.     }
    31. }
     
  2. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    I don't use JavaScript, but I'm pretty sure your issue is that you're not actually assigning the value to anything. You're just adding initialXP to XPVariation, but that sum isn't going anywhere.

    Code (csharp):
    1. initialXP += XPVariation;
    Code (csharp):
    1.  initialXP = initialXP + XPVariation;
    Both the above should work.

    Does that line even compile? In C# an assignment is required.
     
  3. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    This works very well, now the only problem left, is when I add XP to the system, and the player goes up a level, the initial XP required to level up keeps going up, not just once, this obviously sets the player back to level as you cannot keep up with the ridiculous XP requirement, is there any way to get it to add the XP just once each time you level up?
     
  4. Gallantl33

    Gallantl33

    Joined:
    Mar 2, 2015
    Posts:
    12
    You need a way to check if your playerlevel has increased, and if it has, set your playerlevel to that number and run the function to increase experience gain. This function should only run once(one the occasion that playerlevel increases
     
  5. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    I have absolutely no idea how to do that, I am mainly an environments artist starting out in programming.
    I don't know how to check if I have levelled up once or how to stop it running multiple times.
     
  6. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    Anyone know how to do this?
     
  7. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,038
    You want one game object with a player script that is set up as a singleton, and make it into a prefab. Instantiate it if needed via a loader script, like in this tutorial: http://unity3d.com/learn/tutorials/projects/2d-roguelike/gamemanager

    One singleton object holding all the player variables is a nice solution, and you can put player-specific methods on it too, making them always available and knowing where to get the data without looking up further objects.

    Copying and pasting the two scripts in there and following the instruction, removing any game-specific parts, should get you pretty far along :)
     
  8. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    This is all in C#, and as I understand, a singleton cannot be used in Javascript, as we have created both JS and C# versions of this script (it's an open source project) everything we create needs to work in both these languages, and preferably, in each separate script, so I am also trying to avoid using multiple scripts for each system.
     
  9. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    What do you think this bit is doing?
    Code (JavaScript):
    1. if(playerLevel +1)
    If you're new to programming, the most important lesson for you to learn is that computers are stupid. They don't have commons sense, they don't understand context, and they will not pick up on the gist of what you're trying to do.

    If you tell a human being to eat whatever shows up on the plate in front of them, and then the ceiling collapses and dumps a pile of debris on their plate, the human being will realize that you did not anticipate this scenario and that eating the debris would be stupid. A computer will think "he said to eat anything that shows up on this plate; some debris just showed up on this plate; therefore, I should eat this debris."

    The expression "playerLevel + 1" means "calculate the number you get if you add together 'playerLevel' and '1', and then tell me what that number is".

    "if (expression)" means "do these things if that expression evaluates to TRUE".

    So if playerLevel is currently, say, "1", then that code ends up saying "do these things if 2 is TRUE".

    A human being reading your code would say "that's obviously not what he meant; he's probably trying to figure out whether the value stored in playerLevel has increased."

    A computer reading your code is not going to say that. The computer is going to ask "well, is 2 TRUE or not?"

    If you were writing in C#, the computer would say "2 is a number, and TRUE is a truth value, so this doesn't make sense", and give you a compile error. You wouldn't be able to run this code, but at least you would find out immediately that you made a mistake on this line.

    But JavaScript believes in letting you compare any two pieces of data you want, no matter how little sense it makes. It has a rule that the number zero counts as FALSE but that any other number counts as TRUE. (This is called "weak typing", and it is EVIL. I don't know why people think this makes it easier for newbies to learn to program.) So if playerLevel is currently negative one, it won't execute the code inside this "if", but if playerLevel is any other number, then it will.


    If you want to find out whether the value in a variable has changed, you need to remember the "old" value by storing it in another variable. Then you check whether the new value is equal to the old value, and if so, you do whatever you want to do when the value changes, and also update the old value so that you'll know if it changes again.

    However, I don't see anything in the code that you posted that causes playerLevel to change in the first place. Remember, the computer does not understand that you are creating an experience system and that most games with experience systems measure your progress in XP and then increase your level when you accumulate a certain amount. The computer is stupid. The computer just knows that you have some variables with numbers in them; it has absolutely no idea what they're for.

    If you want the computer to change the value of playerLevel based on the current value of experiencePoints, you need to give the computer minute, hand-holding instructions for exactly how to do that. Perhaps something like:
    Code (JavaScript):
    1. if (experiencePoints >= initialXP)
    2. {
    3.     playerLevel = playerLevel + 1;  // Increase the player level
    4.     experiencePoints = experiencePoints - initialXP;  // Take away enough XP to pay for the new level
    5.     initialXP = initialXP + XPVariation;  // Increase the amount of XP needed for the next level
    6. }
    Of course, "initialXP" is a somewhat misleading name, since you're really using it as "xpRequiredForNextLevel". That might confuse a human being who was trying to understand your code. But the computer doesn't care! It doesn't know what any of these words mean anyway!
     
    Kiwasi likes this.
  10. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    @Antistone with your code, when I add XP, the players level never goes up, it just stays at 0, and the InitialXP goes up, and so does experiencePoints, but another problem is that when the experiencePoints goes back to 0, InitialXP does NOT go up when experiencePoints increases the first time, like it thinks it's starting over.

    Code (JavaScript):
    1. #pragma strict
    2. import UnityEngine.Canvas;
    3.  
    4. var experiencePoints : int;
    5. var playerLevel : int;
    6.  
    7. var initialXP : int=1000;
    8. var XPVariation : int;
    9.  
    10. var UICanvas : Canvas;
    11. var expText : Text;
    12. var levelText : Text;
    13.  
    14. function Start () {
    15.     expText.text = "Exp: ";
    16.     levelText.text = "Level: ";
    17.     //UICanvas.RenderMode(RenderMode.WorldSpace);
    18. }
    19.  
    20. function Update ()
    21. {
    22.     playerLevel = Mathf.Abs(experiencePoints / initialXP);
    23.    
    24.     expText.text = "Exp: " + experiencePoints;
    25.     levelText.text = "Level: " + playerLevel;
    26.  
    27.     if (experiencePoints >= initialXP)
    28. {
    29.     playerLevel = playerLevel + 1;  // Increase the player level
    30.     experiencePoints = experiencePoints - initialXP;  // Take away enough XP to pay for the new level
    31.     initialXP = initialXP + XPVariation;  // Increase the amount of XP needed for the next level
    32. }
    33.    
    34. }
     
  11. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Sorry, I overlooked the part where you were setting playerLevel = Mathf.Abs(experiencePoints / initialXP);

    Try taking that line out.
     
  12. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    ah, that did it, I forgot about removing that. Thank you.
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Both languages have pretty close to identical in terms of capability. JavaScript can certainly do singletons.

    Why? If you need separation of systems use namespaces. You should use exactly as many scripts as each system needs to function efficiently.
     
  14. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    You have to jump through some hoops, and Javascript technically doesn't even support classes (a class in Javascript is actually a function that is used to behave like a class at its inner core) but if someone's comfortable using Javascript rather than C# no worries.
     
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    But (almost) no one on this forum is writing actual JavaScript. We are writing in UnityScript, C# and Boo. All of these languages are simply skins on top of .NET and IL. UnityScript has class at its core, just the same as C# and Boo. Its hidden in syntactic sugar. But its still there.

    So saying UnityScript doesn't support classes is massively misinformed.
     
  16. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    I said Javascript doesn't. :p LOL

    I can send you a cup of coffee to wake you up? :D

    (but yes, my mistake, Unity Script and Boo are not Javascript). Touche.
     
    Last edited: Mar 6, 2015
    Studio_Akiba and Kiwasi like this.
  17. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,426
    I am roughly aware of the limitations of JS, and C#, and yes I am aware that UScript is technically not JS, but a custom library (I have a little experience in both).

    I was trying to avoid using multiple scripts because this is an open source project, many of the people who are using this do not have programming backgrounds and I am trying to keep modifications and support for modifications as simple as possible.