Forum begins after the advertisement:


[Before part 15] Adding total experience earn

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Before part 15] Adding total experience earn

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #16335
    Geoffrey
    Level 5
    Participant
    Helpful?
    Up
    1
    ::

    I made this for you, code lovers <3 (only in pause screen for moment, but easy to adapte for in game screen)

    3 possibles ways with comment, earn only by gems, only by levels, or gems + level

    Step 1

    Step 2

    Change the name in inspector and change it names for whatever you want

    Step 3

    Minor change in the code:

    In game Manager.cs

    
    [Header("Current Stat Displays")]
    public Text currentExperienceTotal; // <= Add 
    public Text currentHealthDisplay;
    

    In CharacterScriptableObject.cs

    
    [SerializeField]
    float experienceTotal;
    public float ExperienceTotal { get => experienceTotal; private set => experienceTotal = value; }
    

    In PlayerStats.cs

    
    CharacterScriptableObject characterData;
    

    // Current Stats float currentExperienceTotal; <= Add this line float currentHealth; float currentRecovery; float currentMoveSpeed; float currentMight; float currentProjectileSpeed; float currentMagnet;

    
    public float CurrentExperienceTotal{
        get { return currentExperienceTotal; }
        set{
            // Check if the value has changed
            if(currentExperienceTotal != value){
                currentExperienceTotal = value;
                // Update the real time value of the stat
                if(GameManager.instance != null){
                    GameManager.instance.currentExperienceTotal.text = "Experience Total: " + currentExperienceTotal;
                }
                // Add any additional logic here that needs to be executed when values change
            }
        }
    }
    

    Add this lines in Awake() and Start ()

    
        void Awake(){
            characterData = CharacterSelector.GetData();
            CharacterSelector.instance.DestroySingleton();
    
        inventory = GetComponent<InventoryManager>();
    
        // Assign the variables
        CurrentExperienceTotal = characterData.ExperienceTotal; // <= Add
        CurrentHealth = characterData.MaxHealth;
        CurrentRecovery = characterData.Recovery;
        CurrentMoveSpeed = characterData.MoveSpeed;
        CurrentMight = characterData.Might;
        CurrentProjectileSpeed = characterData.ProjectileSpeed;
        CurrentMagnet = characterData.Magnet;
    
        // Spawn the starting weapon
        SpawnWeapon(characterData.StartingWeapon);
        SpawnWeapon(secondWeaponTest);
        SpawnPassiveItem(firstPassiveItemTest);
        SpawnPassiveItem(secondPassiveItemTest);
    }
    void Start(){
        // Initialize the experience cap as the first experience cap increase
        experienceCap = levelRanges[0].experienceCapIncrease;
        // Set the current Stats display
        GameManager.instance.currentExperienceTotal.text = "Experience Total: " + currentExperienceTotal; // <= Add
        GameManager.instance.currentHealthDisplay.text = "Health: " + currentHealth;
        GameManager.instance.currentRecoveryDisplay.text = "Recovery: " + currentRecovery;
        GameManager.instance.currentMoveSpeedDisplay.text = "Move Speed: " + currentMoveSpeed;
        GameManager.instance.currentMightDisplay.text = "Might: " + currentMight;
        GameManager.instance.currentProjectileSpeedDisplay.text = "Projectile Speed: " + currentProjectileSpeed;
        GameManager.instance.currentMagnetDisplay.text = "Magnet: " + currentMagnet;
        // Set the current chosen Character data
        GameManager.instance.AssignChosenCharacterUI(characterData);
    }

    Here is an introduction to earn money by gems
    With the exact following code, xp is only based on levels
    Comments can help to chose the method, need other code if you want separe gems and levels


    public void IncreaseExperience(int amount){
    // Adds gem experience if activated
    // Activate this line to only count gems in CurrentExperienceTotal
    // CurrentExperienceTotal += amount;
    experience += amount;
    LevelUpChecker();
    }

    void LevelUpChecker(){
        int experienceLvl = 0; // Used to calculate experience gained by level, if activated
        // Loop to handle multiple level-ups if experience exceeds the cap multiple times
        while(experience >= experienceCap){
            level++;
            // Adds level-based XP to CurrentExperienceTotal, if desired
            // Activate this line if CurrentExperienceTotal should only increase by level
            // CurrentExperienceTotal += level * 10;
            experienceLvl += level * 10; // Increments experienceLvl with level-based bonus
            experience -= experienceCap;
            int experienceCapIncrease = 0;
            foreach(LevelRange range in levelRanges){
                if(level >= range.startLevel && level <= range.endLevel){
                    experienceCapIncrease = range.experienceCapIncrease;
                    break;
                }
            }
            experienceCap += experienceCapIncrease;
        }
        // Updates CurrentExperienceTotal if you want it to increase with both gems and level
        // Activate this line to combine XP from gems and level
        // If you want only gem XP, you can comment out this line
        CurrentExperienceTotal = experienceLvl + experience;
    }

    To Enable or Disable XP Sources:

    • XP only with gems: Activate the line CurrentExperienceTotal += amount; in IncreaseExperience, and comment out CurrentExperienceTotal = experienceLvl + experience; in LevelUpChecker.
    • Combined XP from gems and level: Leave CurrentExperienceTotal = experienceLvl + experience; active in LevelUpChecker, and add CurrentExperienceTotal += amount; in IncreaseExperience.
    has upvoted this post.
    #16358
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Thank you Geoffrey.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: