Hi, I'm new to GameSparks, but so far, it seems very cool. I have what is probably a mostly C# question, but I can't seem to find a clear direction anywhere:
From the response of AccountDetailsRequest, I have no problem getting the values for e.g., Currency, etc. but I am having a difficult time determining the correct way to parse either Location or VirtualGoods. The docs indicate they are JSON (and they definitely look like JSON!), but I've had no luck with any JSON parsing tools in Unity C#. When I check the type of eg VirtualGoods, it indicates GSData.
1000's of thanks in advance if someone can provide an example of how to parse response.VirtualGoods!
Best Answer
A
Ayyappa R
said
about 8 years ago
response.VirtualGoods will return a JSON response, so you can get the details by using GetString method. Here is the sample.
Hello Karlo, I'm not a 100% sure this is the best way of doing it, but this is the way I'm doing it so here goes :D.
you can simply do a foreach(var item in response.VirtualGoods.BaseData ) to parse through all the virtual goods the player owns.
The BaseData will return a dictionary<string, object>, since the response returns the shortCode and the amount of virtual goods the player owns, you need to cast it to double. I hope the example below helps and isn't too unclear.
public class YourSelfMadePlayerDataContainerObject
{
public double ownedAmount;
public string city;
public string country;
public float Latitude;
public float Longitude;
}
public void GetPlayerDetailsInfo()
{
new AccountDetailsRequest().Send((response) =>
{
if (response.HasErrors)
{
Debug.Log(response.Errors);
}
else
{
YourSelfMadePlayerDataContainerObject obj;
foreach (var ownedItem in response.VirtualGoods.BaseData)
{
if(ownedStuff.TryGetValue(ownedItem.Key, out obj))
{
obj.ownedAmount = (double)ownedItem.Value;
}
}
}
}, CONNECTION_TIMEOUT);
}
For location, you can see that response.Location.Country/City return a string, and Latitude/Longitude return a nullable double. So you can set them directly.
-Ali
K
Karlo Kilayko
said
about 8 years ago
Ah, BaseData! Thank you so much, Ali, you are my hero! You have given me the key! :) This was driving me crazy.
K
Karlo Kilayko
said
about 8 years ago
Thanks very much, Ayyappa! I read in the docs that the return is supposed to be JSON, but I could not get any of the various JSON parsers to work. I like your simple solution.
Karlo Kilayko
Hi, I'm new to GameSparks, but so far, it seems very cool. I have what is probably a mostly C# question, but I can't seem to find a clear direction anywhere:
From the response of AccountDetailsRequest, I have no problem getting the values for e.g., Currency, etc. but I am having a difficult time determining the correct way to parse either Location or VirtualGoods. The docs indicate they are JSON (and they definitely look like JSON!), but I've had no luck with any JSON parsing tools in Unity C#. When I check the type of eg VirtualGoods, it indicates GSData.
1000's of thanks in advance if someone can provide an example of how to parse response.VirtualGoods!
response.VirtualGoods will return a JSON response, so you can get the details by using GetString method. Here is the sample.
public void ParseAccountDetailsResponse(AccountDetailsResponse response)
{
//Get location details
string city = response.Location.City;
string country = response.Location.Country;
string latitude = response.Location.Latitide;
string longitude = response.Location.Longditute;
//For getting virtual goods
string virtualGoodValue = response.VirtualGoods.GetString(YOUR_VIRTUAL_GOOD_NAME);
}
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstAyyappa R
response.VirtualGoods will return a JSON response, so you can get the details by using GetString method. Here is the sample.
public void ParseAccountDetailsResponse(AccountDetailsResponse response)
{
//Get location details
string city = response.Location.City;
string country = response.Location.Country;
string latitude = response.Location.Latitide;
string longitude = response.Location.Longditute;
//For getting virtual goods
string virtualGoodValue = response.VirtualGoods.GetString(YOUR_VIRTUAL_GOOD_NAME);
}
Ali Abdul-Karim
Hello Karlo, I'm not a 100% sure this is the best way of doing it, but this is the way I'm doing it so here goes :D.
you can simply do a foreach(var item in response.VirtualGoods.BaseData ) to parse through all the virtual goods the player owns.
The BaseData will return a dictionary<string, object>, since the response returns the shortCode and the amount of virtual goods the player owns, you need to cast it to double. I hope the example below helps and isn't too unclear.
For location, you can see that response.Location.Country/City return a string, and Latitude/Longitude return a nullable double. So you can set them directly.
-Ali
Karlo Kilayko
Ah, BaseData! Thank you so much, Ali, you are my hero! You have given me the key! :) This was driving me crazy.
Karlo Kilayko
Thanks very much, Ayyappa! I read in the docs that the return is supposed to be JSON, but I could not get any of the various JSON parsers to work. I like your simple solution.
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics