I have a class that saves two 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
EDIT :
i did come close i think by doing this
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.
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
}
}
i did manage to break out of the error by this code, but this code even though it debugs the json it doesnt pass over to the levelData var
string json = snapshot.Child("Sets").Child("Art").GetRawJsonValue(); // levelDataString
LevelData levelData = JsonUtility.FromJson<LevelData>("{\"type\":" + json + "}");//(json); ////
Debug.Log(json);
Debug.Log(levelData.lastUnlockedLevel);
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!