Search Unity

RPG Class change script Help

Discussion in 'Scripting' started by Quiet_Apples_Studios, Aug 9, 2020.

  1. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    i've assigned class information (a name and strength value) to 2 scriptable object, I then used a different scriptable object to hold those "Classes" in an array.

    Using my script im capable of switching between those two classes by pressing the space button.

    The thing is when i switch to a class I add the new classes strength value to the players strength value but when I switch to a different class I'm still using the previous str value, switching repeatedly gives me infinite Strength, how do i fix this?
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Player : MonoBehaviour
    5. {
    6.     public int wantedIndex;
    7.     public Text text;
    8.     public ClassArray classArray;
    9.     public int Strength;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.         {
    21.             wantedIndex++;
    22.             NextClass();
    23.         }
    24.  
    25.     }
    26.  
    27.     void NextClass()
    28.     {
    29.         int currentClassIndex = wantedIndex % classArray.baseClasses.Length;
    30.         string currentClassName = classArray.baseClasses[currentClassIndex].Name;
    31.         int currentCLassesStrength = classArray.baseClasses[currentClassIndex].Str;
    32.         Strength += currentCLassesStrength;
    33.  
    34.         text.text = $"Current class Name: {currentClassName}, Strength value is {Strength}";
    35.     }
    36. }
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    What does "%" do, I have never seen that used before in C#?

    I think.....
    You need use replace + with %
    and for strength, it should be = not +=
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Integer modulo division. He's cycling through the available things, then back to zero.

    @Quiet_Apples_Studios : BUT: you should do the increment in the NextClass() method and you should also modulo-wrap the base number, because otherwise it will become negative, and when you do modulo division on a negative number, you will be unpleasantly surprised.

    You want to think of it as a diagram:

    - base strength
    - any and all mods
    - any other realtime changes

    these feed into a system that gives you an "effective strength" that is never stored anywhere, just computed from the above.
     
  4. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    That's cool!!
    Ill have to remember that one.
     
  5. Quiet_Apples_Studios

    Quiet_Apples_Studios

    Joined:
    Jan 27, 2020
    Posts:
    17
    I dont quiet get what you mean by modulo wrapping the base number could you show me an example?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Your code allows
    wantedIndex
    to grow unbounded. After about 2 billion iterations it will wrap to a huge negative number and make your % modulo computation wrong. It would take a LONG time to do that many iterations, but it's still a bug waiting to happen.

    By wrap it I mean iterate up from 0 to the max, then reset it to 0. That way it never can be negative.