How to get / parse list of dictionaries from CloudCode using Unity/C#
M
Maria Mercedes Martinez
started a topic
almost 7 years ago
Hello,
I am practicing getting stuff from my collection in GS using CloudCode so that I can use parts of it in my Unity/C# code.
My CloudCode event getWhiteCards is working fine and here it is... I am getting my collection, pulling out a document and adding it to a list. I want that list in my response so I do this like so:
//create an empty list
var whiteCardsList = [];
var count = 0;
//get a random whitecard from the whitecards collection
var whiteCards = Spark.runtimeCollection("whiteCards");
var whiteCardslength = whiteCards.count() - 1;
//pull out a random card and add it to a list
while (count <= 6)
{
var randNumWhite = Math.floor(Math.random() * (whiteCardslength - 0 + 1));
//now we have a random card
var onewhiteCard = whiteCards.find().limit(-1).skip(randNumWhite).next();
//push this card into our list
whiteCardsList.push(onewhiteCard);
count ++;
}
//---------I want to get the whole enchilada, in case I want to use the IDs of the items in the arrays later...
Spark.setScriptData("myNewList", whiteCardsList);
This is the part I need help with... I call my event like this:
public void getWhiteCards()
{
new GameSparks.Api.Requests.LogEventRequest().SetEventKey("getWhiteCards")
.Send((response) => {
if(!response.HasErrors)
{
// GSData myList = response.ScriptData.GetGSDataList("myNewList"); // This didn't work - got errors :(
string example = response.ScriptData.GetString("myNewList"); // This has worked for a simple dict and I have no errors so I used it...
Debug.Log("I am in the getWhiteCards function " + example);
}
else
{
Debug.Log("There are errors " + response.Errors);
}
});
}
My Unity console shows that I am getting what I want...but how do I get this object so I can iterate over the list and get the text out, and fun stuff like that? Here's what is in my console when I run it....
My big document with "myNewList" of dicts that I want to play with in my Unity/C# shows up fine :
Any thoughts or docs you can point me to would be appreciated!
Best Answer
R
Ryan Fuller
said
almost 7 years ago
Well I'm not sure where in the docs that info it. Frankly the gamesparks docs are so typically outdated and spread apart I avoid them (I wish it wasn't so).
But if you can't tell by the method name what the return value type will be (even though it's in the name), most IDE's will show any docs when you hover over the methods. So if you're using monodevelop + Unity, hover over the GetGSDataList() call and it should say something like "public List<GSData> GetGSDataList()."
So that's how I knew the correct return type. Where you were trying to store it as a GSData object, it actually returns a list of GSData objects. Hopefully that makes sense.
Thanks Ryan, I will give this a try...where can I find this type of info in the docs? I searched GSData and I get very little.
R
Ryan Fuller
said
almost 7 years ago
Answer
Well I'm not sure where in the docs that info it. Frankly the gamesparks docs are so typically outdated and spread apart I avoid them (I wish it wasn't so).
But if you can't tell by the method name what the return value type will be (even though it's in the name), most IDE's will show any docs when you hover over the methods. So if you're using monodevelop + Unity, hover over the GetGSDataList() call and it should say something like "public List<GSData> GetGSDataList()."
So that's how I knew the correct return type. Where you were trying to store it as a GSData object, it actually returns a list of GSData objects. Hopefully that makes sense.
M
Maria Mercedes Martinez
said
almost 7 years ago
It does, I will try to use that as my guide.
Thanks again for taking the time to help me.
I read somewhere that GS was updating their docs and tutorials, lets hope this happens soon.
Maria Mercedes Martinez
Hello,
I am practicing getting stuff from my collection in GS using CloudCode so that I can use parts of it in my Unity/C# code.
My CloudCode event getWhiteCards is working fine and here it is... I am getting my collection, pulling out a document and adding it to a list. I want that list in my response so I do this like so:
//create an empty list
var whiteCardsList = [];
var count = 0;
//get a random whitecard from the whitecards collection
var whiteCards = Spark.runtimeCollection("whiteCards");
var whiteCardslength = whiteCards.count() - 1;
//pull out a random card and add it to a list
while (count <= 6)
{
var randNumWhite = Math.floor(Math.random() * (whiteCardslength - 0 + 1));
//now we have a random card
var onewhiteCard = whiteCards.find().limit(-1).skip(randNumWhite).next();
//push this card into our list
whiteCardsList.push(onewhiteCard);
count ++;
}
//---------I want to get the whole enchilada, in case I want to use the IDs of the items in the arrays later...
Spark.setScriptData("myNewList", whiteCardsList);
This is the part I need help with... I call my event like this:
public void getWhiteCards()
{
new GameSparks.Api.Requests.LogEventRequest().SetEventKey("getWhiteCards")
.Send((response) => {
if(!response.HasErrors)
{
// GSData myList = response.ScriptData.GetGSDataList("myNewList"); // This didn't work - got errors :(
string example = response.ScriptData.GetString("myNewList"); // This has worked for a simple dict and I have no errors so I used it...
Debug.Log("I am in the getWhiteCards function " + example);
}
else
{
Debug.Log("There are errors " + response.Errors);
}
});
}
My Unity console shows that I am getting what I want...but how do I get this object so I can iterate over the list and get the text out, and fun stuff like that? Here's what is in my console when I run it....
My big document with "myNewList" of dicts that I want to play with in my Unity/C# shows up fine :
GS: RECV:{"@class":".LogEventResponse","requestId":"635961666544215180_3","scriptData":{"myNewList":[{"_id":{ "$oid" : "570555f4f3939205e33b367f"},"text":"A balanced breakfast"},{"_id":{ "$oid" : "570556e947e69305e3b7ab5f"},"text":"The Donald Trump seal of approval"},{"_id":{ "$oid" : "570557e247e69305e3b7adc0"},"text":"Waterboarding"},{"_id":{ "$oid" : "57055734f3939205e33b3b95"},"text":"The Terrorists"},{"_id":{ "$oid" : "5705581b47e69305e3b7adc4"},"text":"The Force"},{"_id":{ "$oid" : "5705581b47e69305e3b7adc4"},"text":"The Force"},{"_id":{ "$oid" : "5705585b47e69305e3b7ae90"},"text":"Famine"}]}}
My debug.log :
UnityEngine.Debug:Log(Object)
other stuff:
GameSparks.Platforms.<DebugMsg>c__AnonStorey5:<>m__7() (at Assets/GameSparks/Platforms/PlatformBase.cs:144)
GameSparks.Platforms.PlatformBase:Update() (at Assets/GameSparks/Platforms/PlatformBase.cs:67)
Any thoughts or docs you can point me to would be appreciated!
Well I'm not sure where in the docs that info it. Frankly the gamesparks docs are so typically outdated and spread apart I avoid them (I wish it wasn't so).
But if you can't tell by the method name what the return value type will be (even though it's in the name), most IDE's will show any docs when you hover over the methods. So if you're using monodevelop + Unity, hover over the GetGSDataList() call and it should say something like "public List<GSData> GetGSDataList()."
So that's how I knew the correct return type. Where you were trying to store it as a GSData object, it actually returns a list of GSData objects. Hopefully that makes sense.
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstRyan Fuller
Hi again,
Should be:
Maria Mercedes Martinez
Thanks Ryan, I will give this a try...where can I find this type of info in the docs? I searched GSData and I get very little.
Ryan Fuller
Well I'm not sure where in the docs that info it. Frankly the gamesparks docs are so typically outdated and spread apart I avoid them (I wish it wasn't so).
But if you can't tell by the method name what the return value type will be (even though it's in the name), most IDE's will show any docs when you hover over the methods. So if you're using monodevelop + Unity, hover over the GetGSDataList() call and it should say something like "public List<GSData> GetGSDataList()."
So that's how I knew the correct return type. Where you were trying to store it as a GSData object, it actually returns a list of GSData objects. Hopefully that makes sense.
Maria Mercedes Martinez
It does, I will try to use that as my guide.
Thanks again for taking the time to help me.
I read somewhere that GS was updating their docs and tutorials, lets hope this happens soon.
-
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