Forum begins after the advertisement:


[Part 9/10] Adding experience earn [Small code]

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 9/10] Adding experience earn [Small code]

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

    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

    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);
        }
    

    Only with levels

    
        public void IncreaseExperience(int amount){
            // CurrentExperienceTotal += amount; // Add this line if you want increase total experience only by gems
            experience += amount;
            LevelUpChecker();
        }
    
        void LevelUpChecker(){
            float experienceLvl = CurrentExperienceTotal; // Delete if you want increase experience only by gems
            // While instead of if to prevent multiple levels upgrade and causing bug when upgrade items in future
            while(experience >= experienceCap){
                level++;
                // CurrentExperienceTotal += level * 10; // Add if you want experience only by level
                experienceLvl += level * 10; // Ajout de l'expérience selon le niveau, linéairement
                CurrentExperienceTotal = experienceLvl; // Delete if you want experience only by gems
                experience -= experienceCap;
                
                int experienceCapIncrease = 0;
                foreach(LevelRange range in levelRanges){
                    if(level >= range.startLevel && level <= range.endLevel){
                        experienceCapIncrease = range.experienceCapIncrease;
                        break;
                    }
                }
                experienceCap += experienceCapIncrease;
            }
        }
    

    Only with gems

    
    public void IncreaseExperience(int amount){
        // Adds gem experience if activated
        // Activate this line to only count gems in CurrentExperienceTotal
        CurrentExperienceTotal += amount;  // Add XP from gems
    
        // No level-based experience is added here
        experience += amount;  // Add experience to the current experience pool (if necessary for internal calculations)
        LevelUpChecker();  // Check if a level-up occurs
    }
    
    void LevelUpChecker(){
        float experienceLvl = CurrentExperienceTotal;  // Used to calculate experience gained by level, if activated
        while(experience >= experienceCap){  // Handle level-ups if experience exceeds cap
            level++;
            experienceLvl += level * 10;  // Adds level-based XP to the total XP
            experience -= experienceCap;
    
            int experienceCapIncrease = 0;
            foreach(LevelRange range in levelRanges){
                if(level >= range.startLevel && level <= range.endLevel){
                    experienceCapIncrease = range.experienceCapIncrease;
                    break;
                }
            }
            experienceCap += experienceCapIncrease;  // Update experience cap after level-up
        }
    
        // No level-based experience is added to CurrentExperienceTotal here
        // CurrentExperienceTotal remains based solely on gem XP
    }
    

    Gems + levels

    
    public void IncreaseExperience(int amount){
        // Adds gem experience if activated
        // Activate this line to count both gems and levels in CurrentExperienceTotal
        CurrentExperienceTotal += amount;  // Add XP from gems
    
        // Adds level-based experience if activated
        experience += amount;  // Add experience to the current experience pool
        LevelUpChecker();  // Check if a level-up occurs
    }
    
    void LevelUpChecker(){
        float experienceLvl = CurrentExperienceTotal;  // Used to calculate experience gained by level, if activated
        while(experience >= experienceCap){  // Handle level-ups if experience exceeds cap
            level++;
            experienceLvl += level * 10;  // Adds level-based XP to the total XP
            experience -= experienceCap;
    
            int experienceCapIncrease = 0;
            foreach(LevelRange range in levelRanges){
                if(level >= range.startLevel && level <= range.endLevel){
                    experienceCapIncrease = range.experienceCapIncrease;
                    break;
                }
            }
            experienceCap += experienceCapIncrease;  // Update experience cap after level-up
        }
    
        // Updates CurrentExperienceTotal with both gems and level XP
        CurrentExperienceTotal = experienceLvl + experience;  // Combine both XP sources
    }
    
    and 1 other person have upvoted this post.
    #16340
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Thanks for sharing Geoffrey, much appreciated.

    #16341
    Geoffrey
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    I just don’t undestand why links don’t show images, is there a tip ?

    #16357
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Yeah, I’m not sure why your Imgur links are missing the /a/ section. Only Imgur links with the /a/ in their URL get embedded, e.g. https://imgur.com/a/example.

    Which page are you using to do your uploads?

    #16362
    Geoffrey
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    I use the link in the small tutorial you made.

    #16375
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    There was an issue the blog had with detecting some Imgur links properly. It’s been fixed.

    Thanks again for sharing your codes!

    #16390
    Geoffrey
    Level 5
    Participant
    Helpful?
    Up
    1
    ::

    Thanks, pleasure to see my beautiful screenshots :)

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

Go to Login Page →


Advertisement below: