Hello everyone,
so far I have a very simple PlayerDataSO which I use to easily transport player data from scene to scene.
public class PlayerDataSO : ScriptableObject
{
[Header("Variable Values")]
[SerializeField] private string playerName;
[SerializeField] private string playerDBUserId;
[Header("Fixed Values")]
[SerializeField] private float _baseSidewayForce;
Now I try to save it to my realtime database (Firebase) and load it again.
My test methods look like this:
public void SavePlayerData(PlayerDataSO playerData)
{
string jsonNet = JsonConvert.SerializeObject(playerData);
Debug.Log("SavePlayerData - jsonNet - " + jsonNet);
DBreference.Child("PlayerData").SetRawJsonValueAsync(jsonNet);
LoadPlayerDataTest();
}
private void LoadPlayerDataTest()
{
PlayerDataSO tmpPlayerSO = null;
DBreference.Child("PlayerData").GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
Debug.Log("OH OH, ERROR");
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
Debug.Log("LoadPlayerDataTest - snapshot - to string " + snapshot.Child("PlayerName").Value.ToString());
Debug.Log("LoadPlayerDataTest - snapshot - JSON --> " + snapshot.GetRawJsonValue());
// evil line of code coming:
tmpPlayerSO = JsonConvert.DeserializeObject<PlayerDataSO>(snapshot.GetRawJsonValue());
string tmpJson = JsonConvert.SerializeObject(tmpPlayerSO);
Debug.Log("LoadPlayerDataTest - snapshot - JSON --> " + tmpJson);
}
});
}
The debug output is this:
SavePlayerData - jsonNet - {“PlayerName”:“oetzi”,"PlayerDBUserId…[shortened]
LoadPlayerDataTest - snapshot - to string oetzi
LoadPlayerDataTest - snapshot - JSON → {“BaseSidewayForce”:50,"Player…[shortened]
As you can see the last debug output is missing and with some testing I found out that the following line causes a wierd behaviour.
tmpPlayerSO = JsonConvert.DeserializeObject<PlayerDataSO>(snapshot.GetRawJsonValue());
The execution of the code simply stops. No exception is thrown, nothing.
So I have 2 questions:
First: How can I reach my actual goal, to load my data from the database back into my scriptable object?
Second: Why does Unity behave that strange?
@Llama_w_2Ls Thanks for your reply. While trying to follow your hints I stepped into another curious problem, where again an exception was hidden. At least your hint with the try catch block helped me to locate it. I postet it here: https://answers.unity.com/questions/1840676/firebasedatabasedefaultinstancerootreference-null.html ---------- I still do not understand why exceptions are hidden by Unity if the code runs on another thread. I mean it is not an exceptional case to run something in a thread, is it?!
– XoetziX