I've created a leveling system for my game in Unity. I was able to store the level and XP amount in my Firebase database. I'm also able to display both the level and XP in gave and have it update in the database without having to manually save the data.
The problem is now that the xp bar I created that scales based on the amount of xp the user is not communicating with my database, only its self. It also resets all int variables to 0 when switching scenes or restarting the game.
My goal here is to have the progress bar update live based on the amount of xp that's stored in my database and to have it not reset after I switch scenes or reload the game.
Here's my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class LevelSystem
{
public event EventHandler OnExperienceChanged;
public event EventHandler OnLevelChanged;
private static readonly int[] experiencePerLevel = new[] {100, 120, 145, 175, 210, 250, 295, 345, 400, 460};
private int level;
private int experience;
private int experienceToNextLevel;
private TMP_Text xpText;
public LevelSystem()
{
level = 0;
experience = 0;
experienceToNextLevel = 100;
}
public void AddExperience(int amount)
{
if (!IsMaxLevel())
{
experience += amount;
while (!IsMaxLevel() && experience >= GetExperienceToNextLevel(level))
{
//Enough experience to level up
level++;
experience -= GetExperienceToNextLevel(level);
if(OnLevelChanged != null) OnLevelChanged(this, EventArgs.Empty);
}
if(OnExperienceChanged != null) OnExperienceChanged(this, EventArgs.Empty);
}
}
public int GetLevelNumber()
{
return level;
}
public int GetXpAmount()
{
return experience;
}
public float GetExperienceNormalized()
{
if (IsMaxLevel())
{
return 1f;
}
else
{
return (float)experience / GetExperienceToNextLevel(level);
}
}
public int GetExperience()
{
return experience;
}
public int GetExperienceToNextLevel()
{
return experienceToNextLevel;
}
public int GetExperienceToNextLevel(int level)
{
if (level < experiencePerLevel.Length)
{
return experiencePerLevel[level];
}
else
{
//Level invalid
Debug.Log("Level invalid: " + level);
return 100;
}
}
public bool IsMaxLevel()
{
return IsMaxLevel(level);
}
public bool IsMaxLevel(int level)
{
return level == experiencePerLevel.Length - 1;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Utilities.Mine;
public class LevelSystemAnimation
{
public event EventHandler OnExperienceChanged;
public event EventHandler OnLevelChanged;
private LevelSystem levelSystem;
private bool isAnimating;
private float updateTimer;
private float updateTimerMax;
private int level;
private int experience;
public LevelSystemAnimation(LevelSystem levelSystem)
{
SetLevelSystem(levelSystem);
updateTimerMax = .016f;
FunctionUpdater.Create(() => Update());
}
public void SetLevelSystem(LevelSystem levelSystem)
{
this.levelSystem = levelSystem;
level = levelSystem.GetLevelNumber();
experience = levelSystem.GetExperience();
levelSystem.OnExperienceChanged += LevelSystem_OnExperienceChanged;
levelSystem.OnLevelChanged += LevelSystem_OnLevelChanged;
}
private void LevelSystem_OnLevelChanged(object sender, System.EventArgs e)
{
isAnimating = true;
}
private void LevelSystem_OnExperienceChanged(object sender, System.EventArgs e)
{
isAnimating = true;
}
public void Update()
{
if(isAnimating)
{
updateTimer += Time.deltaTime;
while (updateTimer > updateTimerMax)
{
updateTimer -= updateTimerMax;
UpdateTypeAddExperience();
}
}
}
private void UpdateTypeAddExperience()
{
if(level < levelSystem.GetLevelNumber())
{
//Local level under target level
AddExperience();
}
else
{
//Local level equals the target level
if (experience < levelSystem.GetExperience())
{
AddExperience();
}
else
{
isAnimating = false;
}
}
}
public void AddExperience()
{
experience++;
if (experience >= levelSystem.GetExperienceToNextLevel(level))
{
level++;
experience = 0;
if(OnLevelChanged != null) OnLevelChanged(this, EventArgs.Empty);
}
if(OnExperienceChanged != null) OnExperienceChanged(this, EventArgs.Empty);
}
public int GetLevelNumber()
{
return level;
}
public int GetXpAmount()
{
return experience;
}
public float GetExperienceNormalized()
{
if (levelSystem.IsMaxLevel(level))
{
return 1f;
}
else
{
return (float)experience / levelSystem.GetExperienceToNextLevel(level);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class LevelFunction : MonoBehaviour
{
public static LevelFunction instance;
[SerializeField]
private TMP_Text levelText;
[SerializeField]
private Image experienceBarImage;
[SerializeField]
private TMP_Text xpText;
private LevelSystem levelSystem;
private LevelSystemAnimation levelSystemAnimation;
private void SetExperienceBarSize(float experienceNormalized)
{
experienceBarImage.fillAmount = experienceNormalized;
}
private void SetLevelNumber(int levelNumber)
{
levelText.text = "L" + (levelNumber + 1);
}
private void SetXpAmount(int xpAmount)
{
xpText.text = "" + xpAmount;
}
public void SetLevelSystem(LevelSystem levelSystem)
{
this.levelSystem = levelSystem;
}
public void SetLevelSystemAnimation(LevelSystemAnimation levelSystemAnimation)
{ //Set the LevelSystem object
this.levelSystemAnimation = levelSystemAnimation;
//Update the starting values
SetLevelNumber(levelSystemAnimation.GetLevelNumber());
SetExperienceBarSize(levelSystemAnimation.GetExperienceNormalized());
SetXpAmount(levelSystemAnimation.GetXpAmount());
//Subscribe to the chnaged events
levelSystemAnimation.OnExperienceChanged += LevelSystemAnimation_OnExperienceChanged;
levelSystemAnimation.OnLevelChanged += LevelSystemAnimation_OnLevelChanged;
}
private void LevelSystemAnimation_OnLevelChanged(object sender, System.EventArgs e)
{
//Level changed, update text
SetLevelNumber(levelSystemAnimation.GetLevelNumber());
}
private void LevelSystemAnimation_OnExperienceChanged(object sender, System.EventArgs e)
{
//Experience changed, update bar size
SetExperienceBarSize(levelSystemAnimation.GetExperienceNormalized());
SetXpAmount(levelSystemAnimation.GetXpAmount());
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class GainXpManager : MonoBehaviour
{
public static GainXpManager instance;
[SerializeField]
private Button collectButton;
[SerializeField]
private TMP_Text levelText;
[SerializeField]
private Image experienceBarImage;
[SerializeField]
private TMP_Text xpText;
public GameObject endGameResults;
private LevelSystem levelSystem;
private LevelSystemAnimation levelSystemAnimation;
private int level;
private int experience;
private void SetExperienceBarSize(float experienceNormalized)
{
experienceBarImage.fillAmount = experienceNormalized;
}
private void SetLevelNumber(int levelNumber)
{
levelText.text = "L" + (levelNumber + 1);
}
private void SetXpAmount(int xpAmount)
{
xpText.text = "" + xpAmount;
}
public void SetLevelSystem(LevelSystem levelSystem)
{
this.levelSystem = levelSystem;
level = levelSystem.GetLevelNumber();
experience = levelSystem.GetExperience();
}
public void SetLevelSystemAnimation(LevelSystemAnimation levelSystemAnimation)
{ //Set the LevelSystem object
this.levelSystemAnimation = levelSystemAnimation;
//Update the starting values
SetLevelNumber(levelSystemAnimation.GetLevelNumber());
SetExperienceBarSize(levelSystemAnimation.GetExperienceNormalized());
SetXpAmount(levelSystemAnimation.GetXpAmount());
//Subscribe to the chnaged events
levelSystemAnimation.OnExperienceChanged += LevelSystemAnimation_OnExperienceChanged;
levelSystemAnimation.OnLevelChanged += LevelSystemAnimation_OnLevelChanged;
}
private void LevelSystemAnimation_OnLevelChanged(object sender, System.EventArgs e)
{
//Level changed, update text
SetLevelNumber(levelSystemAnimation.GetLevelNumber());
FirebaseManager.instance.ActivateXPUpdate();
}
private void LevelSystemAnimation_OnExperienceChanged(object sender, System.EventArgs e)
{
//Experience changed, update bar size
SetExperienceBarSize(levelSystemAnimation.GetExperienceNormalized());
SetXpAmount(levelSystemAnimation.GetXpAmount());
FirebaseManager.instance.ActivateXPUpdate();
}
public void AddXpToAccount()
{
endGameResults.SetActive(false);
levelSystem.AddExperience(25);
}
}
There's more code but I think the problem can be solved with these. Any suggestions would help.