So after trial/error and research I decided to post asking for advice on how to do this on Unity. The problem is that I have crated an array with the CloudScript provided by GameSparks and everything works fine on the Test Harness. However, I got very confused trying to turn the JS Object, that has JS Array in Unity into an Array. I can access it by calling
The thing is accessing the Data in Unity. I have tried making it into a List<GSObject>, <object>, <its own specific data type, which has the same variables as the JS object> and none of them worked. So I was wondering if there is an easier way and I'm just going all wrong about this :)? So essentially I'm wondering how to handle objects and lists that come from GS in Unity.
Additionally this question might be a little off topic, but while testing my code on the Test Harness I noticed, that I get major slowdowns in the debug more when stepping parts. Its with 1 specific function that takes 2 JS objects and makes a shallow copy of it. Also are there any built in JS libraries on GS Cloud Code, because I have been implementing underscore JS slowly :D.
Thanks for your time! :)
-Ali Abdul-Karim
Best Answer
C
Customer Support
said
over 6 years ago
Hi Ali,
So this is quite a complex JSON string, we try our best with the GSData return type but in cases like these we expose the raw JSON as a string so you can use a JSON plugin to better help you grab the data.
There are plenty of free JSON plugins for Unity which would serve you better than the code I wrote to dig down into this:
using UnityEngine;
using System.Collections;
using GameSparks.Api.Requests;
using GameSparks.Core;
using System.Collections.Generic;
public class TestScene : MonoBehaviour {
private string userId;
public List<object> aCostume = new List<object>();
Dictionary<string, object> isOwned;
public void Login () {
new AuthenticationRequest().SetUserName("shane").SetPassword("password").Send((response) =>
{
userId = response.UserId;
});
}
public void GetCostumes()
{
new LogEventRequest_getPlayerInventory().Set_userId(userId).Send((response) =>
{
Debug.Log(response.ScriptData.GetObject("playerInventory").GetObjectList("costumes")); //Got something!
foreach (Dictionary<string, object> costume in response.ScriptData.GetObject("playerInventory").GetObjectList("costumes"))
{
Debug.Log(costume["costume"]); //Got something!
aCostume = (List<object>)costume["costume"];
foreach (var dictionary in aCostume)
{
Debug.Log(dictionary); //Further down the rabbit hole!
}
//Debug.Log(aCostume[0]);
//Debug.Log(aCostume[1]);
//Debug.Log(aCostume[2]);
//Debug.Log(aCostume[3]);
}
});
}
}
You could also investigate our Virtual Goods API and consider making each costume part a "free virtual good" and storing their data in a collection or locally as it is effectively an inventory system, though the JSON route might be the quickest option for now.
Hello, since I noticed that thread and this one were dealing with very similar questions, I decided to expand on the my own questions based on the answers given there.
First of, you can't do getPlayerStateResponse.ScriptData.GetObjectList("playerState")[0].GetNumber("health").ToString(); , without Unity giving you System.Object does not contain definition for GetNumber, unless I'm missing something crucial here?
So in my game, I would like to save my game data in a JSON that looks like this :
So its an array, that has 4 objects where the last one is an array with 4 objects in it.
Of course the data structure is tentative, but for now I like it. Now when I try to access the data sent from my collection called playerInventoryCollection in unity. I use an event called getPlayerInventory and receive the data.
would solve my problems, but it gives me the error mentioned above.
-Ali Abdul-Karim
Customer Support
said
over 6 years ago
Answer
Hi Ali,
So this is quite a complex JSON string, we try our best with the GSData return type but in cases like these we expose the raw JSON as a string so you can use a JSON plugin to better help you grab the data.
There are plenty of free JSON plugins for Unity which would serve you better than the code I wrote to dig down into this:
using UnityEngine;
using System.Collections;
using GameSparks.Api.Requests;
using GameSparks.Core;
using System.Collections.Generic;
public class TestScene : MonoBehaviour {
private string userId;
public List<object> aCostume = new List<object>();
Dictionary<string, object> isOwned;
public void Login () {
new AuthenticationRequest().SetUserName("shane").SetPassword("password").Send((response) =>
{
userId = response.UserId;
});
}
public void GetCostumes()
{
new LogEventRequest_getPlayerInventory().Set_userId(userId).Send((response) =>
{
Debug.Log(response.ScriptData.GetObject("playerInventory").GetObjectList("costumes")); //Got something!
foreach (Dictionary<string, object> costume in response.ScriptData.GetObject("playerInventory").GetObjectList("costumes"))
{
Debug.Log(costume["costume"]); //Got something!
aCostume = (List<object>)costume["costume"];
foreach (var dictionary in aCostume)
{
Debug.Log(dictionary); //Further down the rabbit hole!
}
//Debug.Log(aCostume[0]);
//Debug.Log(aCostume[1]);
//Debug.Log(aCostume[2]);
//Debug.Log(aCostume[3]);
}
});
}
}
You could also investigate our Virtual Goods API and consider making each costume part a "free virtual good" and storing their data in a collection or locally as it is effectively an inventory system, though the JSON route might be the quickest option for now.
Shane
A
Ali Abdul-Karim
said
over 6 years ago
So when using the Virtual Goods API, I would still have to parse the JSON I get from the ListVirtualGoodsRepsonse ?
Ali Abdul-Karim
Hello,
So after trial/error and research I decided to post asking for advice on how to do this on Unity. The problem is that I have crated an array with the CloudScript provided by GameSparks and everything works fine on the Test Harness. However, I got very confused trying to turn the JS Object, that has JS Array in Unity into an Array. I can access it by calling
getPlayerCostumesResponse.ScriptData.GetObject("playerInventory").GetObjectList("costumes");
The thing is accessing the Data in Unity. I have tried making it into a List<GSObject>, <object>, <its own specific data type, which has the same variables as the JS object> and none of them worked. So I was wondering if there is an easier way and I'm just going all wrong about this :)? So essentially I'm wondering how to handle objects and lists that come from GS in Unity.
Additionally this question might be a little off topic, but while testing my code on the Test Harness I noticed, that I get major slowdowns in the debug more when stepping parts. Its with 1 specific function that takes 2 JS objects and makes a shallow copy of it. Also are there any built in JS libraries on GS Cloud Code, because I have been implementing underscore JS slowly :D.
Thanks for your time! :)
-Ali Abdul-Karim
Hi Ali,
So this is quite a complex JSON string, we try our best with the GSData return type but in cases like these we expose the raw JSON as a string so you can use a JSON plugin to better help you grab the data.
To get the JSON data:
There are plenty of free JSON plugins for Unity which would serve you better than the code I wrote to dig down into this:
You could also investigate our Virtual Goods API and consider making each costume part a "free virtual good" and storing their data in a collection or locally as it is effectively an inventory system, though the JSON route might be the quickest option for now.
Shane
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstAli Abdul-Karim
Hello, since I noticed that thread and this one were dealing with very similar questions, I decided to expand on the my own questions based on the answers given there.
First of, you can't do getPlayerStateResponse.ScriptData.GetObjectList("playerState")[0].GetNumber("health").ToString(); , without Unity giving you System.Object does not contain definition for GetNumber, unless I'm missing something crucial here?
So in my game, I would like to save my game data in a JSON that looks like this :
var costumes=
[
{"costume": [ { "isOwned": 0},
{ "name": "00"},
{ "description": ""},
{ "costumeParts": [ {"basePiece": { "isOwned": 0, "name": "", "description": "" }},
{"headPiece": { "isOwned": 0, "name": "", "description": "" }},
{"ornamentPiece": { "isOwned": 0, "name": "", "description": "" }},
{"tailPiece": { "isOwned": 0, "name": "", "description": "" }},
{"colorPiece": { "isOwned": 0, "name": "", "description": "" }} ] }]}
];
So its an array, that has 4 objects where the last one is an array with 4 objects in it.
Of course the data structure is tentative, but for now I like it. Now when I try to access the data sent from my collection called playerInventoryCollection in unity. I use an event called getPlayerInventory and receive the data.
Essentially doing this
getPlayerCostumesResponse.ScriptData.GetObject("playerState").GetObjectList("costumes")[0].GetObject("isOwned").ToString();
would solve my problems, but it gives me the error mentioned above.
-Ali Abdul-Karim
Customer Support
Hi Ali,
So this is quite a complex JSON string, we try our best with the GSData return type but in cases like these we expose the raw JSON as a string so you can use a JSON plugin to better help you grab the data.
To get the JSON data:
There are plenty of free JSON plugins for Unity which would serve you better than the code I wrote to dig down into this:
You could also investigate our Virtual Goods API and consider making each costume part a "free virtual good" and storing their data in a collection or locally as it is effectively an inventory system, though the JSON route might be the quickest option for now.
Shane
Ali Abdul-Karim
So when using the Virtual Goods API, I would still have to parse the JSON I get from the ListVirtualGoodsRepsonse ?
-
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 2486 topics