Sign In Register

How can we help you today?

Start a new topic
Answered

Get Dynamic GSDataList

Hello,


I have an event which responds with something like:


 

{
  "@class": ".LogEventResponse",
  "scriptData": {
    "UserOwns": {
      "oil": 110,
      "flour": 100,
      "donut": 40
    }
  }
}

 The key-value pairs are virtual goods that the user has so they can be anything. Therefore GSDataList("string) won't work cause it means I would have to have over 30 variables and calls for each good available and the user won't have many of them so it's unecessary.


What is the best way to get all the info from this response into a Dictionary<string,int> in Unity C#?


Many thanks!


Alex


Best Answer

 Hey Alex,

You can use GetGSData() and GetInt() to get that data from the response.
Check out the script below and let us know if that works for you.


 

new GameSparks.Api.Requests.LogEventRequest()
            .Send((response) => {

                if(!response.HasErrors)
                {
                    GSData userData = response.ScriptData.GetGSData("UserOwns");
                    int oil = userData.GetInt("oil").Value;
                    int flour = userData.GetInt("flour").Value;
                    int donut = userData.GetInt("donut").Value;
                }
                else
                {
                    Debug.LogWarning("GSM| Error /n "+response.Errors.JSON);
                }
            });

 


Thanks,
Sean



1 Comment

Answer

 Hey Alex,

You can use GetGSData() and GetInt() to get that data from the response.
Check out the script below and let us know if that works for you.


 

new GameSparks.Api.Requests.LogEventRequest()
            .Send((response) => {

                if(!response.HasErrors)
                {
                    GSData userData = response.ScriptData.GetGSData("UserOwns");
                    int oil = userData.GetInt("oil").Value;
                    int flour = userData.GetInt("flour").Value;
                    int donut = userData.GetInt("donut").Value;
                }
                else
                {
                    Debug.LogWarning("GSM| Error /n "+response.Errors.JSON);
                }
            });

 


Thanks,
Sean



Login to post a comment