Sign In Register

How can we help you today?

Start a new topic
Answered

Loading Variables in Unity


 I followed the save load tutorial and I am able to save and load the variables.

 

        new GameSparks.Api.Requests.LogEventRequest().SetEventKey("LOAD_PLAYER").Send((response) => {
            if (!response.HasErrors)
            {
                Debug.Log("Received Player Data From GameSparks...");
                GSData data = response.ScriptData.GetGSData("player_Data");
                Global_Variables.Player_ID = data.GetString("playerID");

 

However, I do not know how to parse the json or convert it into integers.. Everything that I have tried has given me an "InvalidOperationException: Nullable object must have a value." error.


I want to be able to save variables and then reload them the next time the player returns.


Thank you.


Best Answer

 Hey Alex,


If you are getting a nullable object exception there is a good chance there is no data there or you are referencing the wrong key.

If you had the following JSON ....

"scriptData" : {
      "playerData" : {

               "myInt" : 100

        }
}

You should be able to parse this out like this...
( you can check if there is a value using '.HasValue' boolean)


if(response.ScriptData.GetGSData("player_Data").GetInt("myInt").HasValue)
{
     int myInt = response.ScriptData.GetGSData("player_Data").GetInt("myInt") 

}

Hope that helps,
Sean

1 Comment

Answer

 Hey Alex,


If you are getting a nullable object exception there is a good chance there is no data there or you are referencing the wrong key.

If you had the following JSON ....

"scriptData" : {
      "playerData" : {

               "myInt" : 100

        }
}

You should be able to parse this out like this...
( you can check if there is a value using '.HasValue' boolean)


if(response.ScriptData.GetGSData("player_Data").GetInt("myInt").HasValue)
{
     int myInt = response.ScriptData.GetGSData("player_Data").GetInt("myInt") 

}

Hope that helps,
Sean

Login to post a comment