0
\$\begingroup\$

I want to integrate Firebase to my android/iOS game. So far in production I used JSON to save a class into the devices PersistentDataPath and load them accordingly. I did follow the firebase instructions , I implemented analytics and installed the necessary packages but I'm having some trouble understanding how to convert my save system to work with it.

I'm using this class to store the information i need saved

 [System.Serializable]
    public class LevelData
    {
        [Header("Primary Set")]
        public int lastUnlockedLevel = 0;
        public LevelItem[] levelItemArray;
        [Header("Animals Set")]
        public int lastUnlockedLevelAnimals = 0;
        public LevelItem[] levelItemArrayAnimals;
        [Header("Architecture Set")]
        public int lastUnlockedLevelArch = 0;
        public LevelItem[] levelItemArrayArch;
        [Header("Technology Set")]
        public int lastUnlockedLevelTech = 0;
        public LevelItem[] levelItemArrayTech;
        [Header("Vehicles Set")]
        public int lastUnlockedLevelVehicles = 0;
        public LevelItem[] levelItemArrayVehicles;
        [Header("Nature Set")]
        public int lastUnlockedLevelNature = 0;
        public LevelItem[] levelItemArrayNature;

        [Header("Variables")]
        public int difficulty = 1;
        public int animalSet;
        public int architectureSet;
        public int technologySet;
        public int vehiclesSet;
        public int natureSet;
        public int allSets;
        public bool noAds = false;
    }

My SaveData function is pretty simple as it reads a string which is converted into JSON and saved

 public void SaveData()
        {
            string levelDataString = JsonUtility.ToJson(LevelSystemManager.Instance.LevelData);
            try
            {
                System.IO.File.WriteAllText(Application.persistentDataPath + "/LevelData.json", levelDataString);
                Debug.Log("Data Saved");

            }
            catch (System.Exception e)
            {
                Debug.Log("Error Saving Data:" + e);
                throw;
            }
        }

And my LoadData uses the same principles with the addition of checking if null and then setting each value one by one

 private void LoadData()
        {
            try
            {
                string levelDataString = System.IO.File.ReadAllText(Application.persistentDataPath + "/LevelData.json");
                LevelData levelData = JsonUtility.FromJson<LevelData>(levelDataString);
                if (levelData != null)
                {
                    // Primary Set //
                    LevelSystemManager.Instance.LevelData.levelItemArray = levelData.levelItemArray;
                    LevelSystemManager.Instance.LevelData.lastUnlockedLevel = levelData.lastUnlockedLevel;
                    // Animals Set//
                    LevelSystemManager.Instance.LevelData.levelItemArrayAnimals = levelData.levelItemArrayAnimals;
                    LevelSystemManager.Instance.LevelData.lastUnlockedLevelAnimals = levelData.lastUnlockedLevelAnimals;
                    // Architecture Set//
                    LevelSystemManager.Instance.LevelData.levelItemArrayArch = levelData.levelItemArrayArch;
                    LevelSystemManager.Instance.LevelData.lastUnlockedLevelArch = levelData.lastUnlockedLevelArch;
                    // Technology Set//
                    LevelSystemManager.Instance.LevelData.levelItemArrayTech = levelData.levelItemArrayTech;
                    LevelSystemManager.Instance.LevelData.lastUnlockedLevelTech = levelData.lastUnlockedLevelTech;
                    // Vehicles Set//
                    LevelSystemManager.Instance.LevelData.levelItemArrayVehicles = levelData.levelItemArrayVehicles;
                    LevelSystemManager.Instance.LevelData.lastUnlockedLevelVehicles = levelData.lastUnlockedLevelVehicles;
                    // nature Set//
                    LevelSystemManager.Instance.LevelData.levelItemArrayNature = levelData.levelItemArrayNature;
                    LevelSystemManager.Instance.LevelData.lastUnlockedLevelNature = levelData.lastUnlockedLevelNature;

                    // variables //
                    LevelSystemManager.Instance.LevelData.difficulty = levelData.difficulty;
                    LevelSystemManager.Instance.LevelData.animalSet = levelData.animalSet;
                    LevelSystemManager.Instance.LevelData.architectureSet = levelData.architectureSet;
                    LevelSystemManager.Instance.LevelData.technologySet = levelData.technologySet;
                    LevelSystemManager.Instance.LevelData.vehiclesSet = levelData.vehiclesSet;
                    LevelSystemManager.Instance.LevelData.natureSet = levelData.natureSet;
                    LevelSystemManager.Instance.LevelData.allSets = levelData.allSets;
                }
                    Debug.Log("Data Loaded");
            }
            catch (System.Exception e)
            {
                Debug.Log("Error Loading Data:" + e);
                throw;
            }
        }

What i would like is for someone to help me understand how to implement this into firebase. Reading the documentation i did try to initialize it and by following along i came across this part

private void writeNewUser(string userId, string name, string email) {
    User user = new User(name, email);
    string json = JsonUtility.ToJson(user);

    mDatabaseRef.Child("users").Child(userId).SetRawJsonValueAsync(json);
}

which i did try to replicate , unsuccessful obviously

string levelDataString = JsonUtility.ToJson(LevelSystemManager.Instance.LevelData);
reference.Child("users").Child(levelDataString).SetRawJsonValueAsync(levelDataString);

So i would be grateful for some hints or a tutorial on how to save and load each value. Thank you!

edit!! i did manage to save my variables but am still unable to load them into the in game variables. i am trying it like this.

 public void LoadDataFromFirebase(string userID)
        {

            FirebaseDatabase.DefaultInstance
          .GetReference(userID)
          .GetValueAsync().ContinueWith(task => {
              if (task.IsFaulted)
              {
                  // Handle the error...
              }
              else if (task.IsCompleted)
              {
                  DataSnapshot snapshot = task.Result;
                  var diff = LevelSystemManager.Instance.LevelData.difficulty;
                  diff = snapshot.Child("Difficulty").Value.ToString();
                  Debug.Log("firebase load complete");
              }
          });
}

But since my variable is an integer it throws an error, i did try it with a text element and it showed it just fine but i am unable to take that snapshot and turn it into an int.

\$\endgroup\$
2
  • \$\begingroup\$ Unrelated: you shouldn’t need to assign each field manually when loading. You could use JsonUtility.FromJsonOverwrite instead. \$\endgroup\$ Commented Apr 22, 2021 at 13:59
  • \$\begingroup\$ well i did manage to filter my json and format it to send it over but im still having trouble with the loading. i do try the firebase documentation but i cant get the snapshot that i want and set it to my variables, im trying with this one for now LevelSystemManager.Instance.LevelData.difficulty. DataSnapshot snapshot = task.Result; var diff = LevelSystemManager.Instance.LevelData.difficulty; diff = snapshot.Child("Difficulty").Value.ToString(); \$\endgroup\$ Commented Apr 22, 2021 at 15:17

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.