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]
- This topic has 6 replies, 2 voices, and was last updated 1 month ago by Geoffrey.
-
AuthorPosts
-
November 15, 2024 at 12:56 am #16336::
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
<code> [Header("Current Stat Displays")] public Text currentExperienceTotal; // <= Add public Text currentHealthDisplay; </code>
In CharacterScriptableObject.cs
<code> [SerializeField] float experienceTotal; public float ExperienceTotal { get => experienceTotal; private set => experienceTotal = value; } </code>
In PlayerStats.cs
<code> CharacterScriptableObject characterData; // Current Stats float currentExperienceTotal; <= Add this line float currentHealth; float currentRecovery; float currentMoveSpeed; float currentMight; float currentProjectileSpeed; float currentMagnet; </code>
<code> 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 } } } </code>
Add this lines in Awake() and Start ()
<code> 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); } </code>
Only with levels
<code> 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; } } </code>
Only with gems
<code> 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 } </code>
Gems + levels
<code> 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 } </code>
and 1 other person have upvoted this post. November 15, 2024 at 12:05 pm #16340November 15, 2024 at 12:12 pm #16341November 15, 2024 at 10:28 pm #16357::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?
November 16, 2024 at 2:47 pm #16362November 17, 2024 at 3:43 pm #16375::There was an issue the blog had with detecting some Imgur links properly. It’s been fixed.
Thanks again for sharing your codes!
November 18, 2024 at 11:08 am #16390 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: