0
\$\begingroup\$

i was wondering if you could help me with an issue i have with firebase and databases! I have a class that saves variables into an array. i have managed to send that json file to firebase and i can debug it to see its correct when i load the games database but i cannot set those variables to the game. the class is like this :

   [System.Serializable]
   public class LevelItem
   {
       public bool unlocked;
       public int starAchieved;
   }
   [System.Serializable]
   public class LevelData
   {
       [Header("Primary Set")]
       public int lastUnlockedLevel = 0;
       public LevelItem[] levelItemArray;
   }

to save it to firebase i do this

string levels = JsonUtility.ToJson(LevelSystemManager.Instance.LevelData);
reference.Child(data).Child("Sets").Child("Art").SetValueAsync(levels);

and i am trying to retrieve it and set those values (lastUnlocked , star achieved , unlocked and the levelItemArray) back to the game. my other variables like difficulty work fine with this :

LevelSystemManager.Instance.Bundles.difficulty = int.Parse(snapshot.Child("Settings").Child("Difficulty").GetRawJsonValue());

but i cannot load the whole levelData class back to the game! any tips or help? when i was saving the json localy i was doing it like this to load the values

string levelDataString = System.IO.File.ReadAllText(Application.persistentDataPath + "/LevelData.json");
LevelData levelData = JsonUtility.FromJson<LevelData>(levelDataString);
if (levelData != null)
     {
LevelSystemManager.Instance.LevelData.levelItemArray = levelData.levelItemArray;
LevelSystemManager.Instance.LevelData.lastUnlockedLevel = levelData.lastUnlockedLevel;
}

but this approach wont work with firebase

i did come close i think by doing this , in which the debug does show the json in the console.

var json = snapshot.Child("Sets").Child("Art").GetRawJsonValue();
LevelData levelData = JsonUtility.FromJson<LevelData>(json);
Debug.Log(json);
 
LevelSystemManager.Instance.LevelData.levelItemArray = levelData.levelItemArray;
LevelSystemManager.Instance.LevelData.lastUnlockedLevel = levelData.lastUnlockedLevel;

but this gives an error ArgumentException: JSON must represent an object type. but when i do save it localy everything works out

The JSON that gets saved localy is this one

{"lastUnlockedLevel":1,"levelItemArray":[{"unlocked":true,"starAchieved":1},{"unlocked":true,"starAchieved":0},{"unlocked":false,"starAchieved":0},{"unlocked":false,"starAchieved":0},{"unlocked":false,"starAchieved":0},{"unlocked":false,"starAchieved":0},{"unlocked":false,"starAchieved":0},{"unlocked":false,"starAchieved":0},{"unlocked":false,"starAchieved":0},{"unlocked":false,"starAchieved":0}]}

and this is the one that gets saved on firebase

{
  "Purchases" : {
    "allsets" : 0,
    "animalSet" : 0,
    "architecture" : 0,
    "nature" : 0,
    "noAds" : 0,
    "technology" : 0,
    "vehicles" : 0
  },
  "Sets" : {
    "Art" : "{\"lastUnlockedLevel\":1,\"levelItemArray\":[{\"unlocked\":true,\"starAchieved\":3},{\"unlocked\":true,\"starAchieved\":0}]}"
  },
  "Settings" : {
    "Difficulty" : 1
  }
}

As you can see i do have more classes saved there which like my difficulty example work just fine . my whole issue is that i cannot load back the entire LevelData Class and set it to the games variables.Thank you!

EDIT! so far i did manage to make it load by giving the raw json to the string but this is not good for actually loading things

string json = snapshot.Child("Sets").Child("Art").GetRawJsonValue(); // levelDataString
LevelData levelData = JsonUtility.FromJson<LevelData>("{\"lastUnlockedLevel\":1,\"levelItemArray\":[{\"unlocked\":true,\"starAchieved\":1},{\"unlocked\":false,\"starAchieved\":1}]}");
Debug.Log(json);

LevelSystemManager.Instance.LevelData.levelItemArray = levelData.levelItemArray;
LevelSystemManager.Instance.LevelData.lastUnlockedLevel = levelData.lastUnlockedLevel;

I still need to be able to parse that JSON as it is and i dont know why im still getting errors when i try it like this

string json = snapshot.Child("Sets").Child("Art").GetRawJsonValue();
LevelData levelData = JsonUtility.FromJson<LevelData>(json);
\$\endgroup\$
1
  • \$\begingroup\$ Shouldn't 'LevelData levelData = JsonUtility.FromJson<LevelData>(levelDataString);` really be LevelData levelData = JsonUtility.'**FromJsonOverwrite**'<LevelData>(levelDataString); ?? \$\endgroup\$ – Robert French Apr 24 at 21:42
0
\$\begingroup\$

Not 100% sure, but shouldn't the line

LevelData levelData = JsonUtility.FromJson<LevelData>(levelDataString);

really be

LevelData levelData = JsonUtility.FromJsonOverwrite<LevelData>(levelDataString);

i.e. .FromJsonOverwrite not .FromJson

\$\endgroup\$
1
  • \$\begingroup\$ thanks for the reply but i think that is the local way of saving it which worked, i did update my question if you could take a look! \$\endgroup\$ – stratos la Apr 24 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.