Not fully understanding how to get data from cloudcode using Unity / C#
M
Maria Mercedes Martinez
started a topic
almost 7 years ago
I'm trying to learn to how to get data from my collections in GS. So I made a little game where (after the player signs in with FB), the player clicks a button and it will get the info and put it into console for me. The player sign in works fine...but I want the text from my collection to show up in console. This is an example from my collection called blackCards:
{ "_id": { "$oid": "570433f8f3939205e33327cb" }, "text": "I am the text you want to see." }
I want to print out the text into my console in unity (so I can then begin to display it in the game).
The code for the Event I created in CloudCode called getBlackCards:
var cards = Spark.runtimeCollection("blackCards"); //Find my collection blackCards
var theCard = cards.findOne(); // I want to avoid dealing with returning a mongo cursor for now. So just return one from the collection.
Spark.setScriptData("the_text", theCard.text) // So now I have created a dictionary to return to my game with the text
This is the code for the button:
public void getBlackCard()
{
new GameSparks.Api.Requests.LogEventRequest().SetEventKey("getBlackCard")
.Send((response) => {
if(!response.HasErrors)
{
GSData data = response.ScriptData.GetGSData("the_text");
Debug.Log("My Result IS: " + data.GetString("text"));
}
else
{
Debug.Log("There are errors " + response.Errors);
}
});
}
When you click the button, this is what I am getting in my Unity console:
GS: RECV:{"@class":".LogEventResponse","requestId":"635957312524975260_2","scriptData":{"the_text":"I am the text you want to see."}}
scriptData":{"the_text":"I am the text you want to see."}}
You don't want to call GetGSData("the_text"), because the the data is a string. So you would just call response.scriptData.GetString("the_text");
Basically, if the json key has a dictionary as its value, you would call GetGSData, if it has an array you call GetOBJECTList where OJECT is the type of object the array contains (e.g. GetStringList), and if its a root value (not dictionary or array) just call the corresponding get function for that object type (in your case, GetString).
Hope that helps.
M
Maria Mercedes Martinez
said
almost 7 years ago
Hi Ryan,
Thanks for this information, so far what you are saying makes sense about string vs data but I still can't get it to work...
In my button code that I had (you can see it again in my OP) I changed this line :
GSData data = response.ScriptData.GetGSData("the_text");
To:
GSData data = response.scriptData.GetString("the_text");
As you suggested. But its not working. I get this error:
GameSparks.Api.Responses.LogEventResponse' does not contain a definition for `scriptData' and no extension method `scriptData' of type GameSparks.Api.Responses.LogEventResponse' could be found (are you missing a using directive or an assembly reference?)
I also tried it like this:
GSData data = response.ScriptData.GetString("the_text");
But then I get this error:
Cannot implicitly convert type `string' to `GameSparks.Core.GSData'
Yes! This works! thanks so much... I will now make my collections a bit more complex and continue learn how to manipulate them with this new info you have given me.
Thank you SO much for your time on this. Greatly appreciate it!
Maria Mercedes Martinez
I'm trying to learn to how to get data from my collections in GS. So I made a little game where (after the player signs in with FB), the player clicks a button and it will get the info and put it into console for me. The player sign in works fine...but I want the text from my collection to show up in console. This is an example from my collection called blackCards:
{ "_id": { "$oid": "570433f8f3939205e33327cb" }, "text": "I am the text you want to see." }
I want to print out the text into my console in unity (so I can then begin to display it in the game).
The code for the Event I created in CloudCode called getBlackCards:
var cards = Spark.runtimeCollection("blackCards"); //Find my collection blackCards
var theCard = cards.findOne(); // I want to avoid dealing with returning a mongo cursor for now. So just return one from the collection.
Spark.setScriptData("the_text", theCard.text) // So now I have created a dictionary to return to my game with the text
This is the code for the button:
public void getBlackCard()
{
new GameSparks.Api.Requests.LogEventRequest().SetEventKey("getBlackCard")
.Send((response) => {
if(!response.HasErrors)
{
GSData data = response.ScriptData.GetGSData("the_text");
Debug.Log("My Result IS: " + data.GetString("text"));
}
else
{
Debug.Log("There are errors " + response.Errors);
}
});
}
When you click the button, this is what I am getting in my Unity console:
GS: RECV:{"@class":".LogEventResponse","requestId":"635957312524975260_2","scriptData":{"the_text":"I am the text you want to see."}}
UnityEngine.Debug:Log(Object)
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)
NullReferenceException: Object reference not set to an instance of an object
CardManager.<getBlackCard>m__14 (GameSparks.Api.Responses.LogEventResponse response) (at Assets/Resources/Scripts/CardManager.cs:28)
GameSparks.Core.GSTypedRequest`2+<>c__DisplayClass1[GameSparks.Api.Requests.LogEventRequest,GameSparks.Api.Responses.LogEventResponse].<Send>b__0 (GameSparks.Core.GSObject response)
GameSparks.Core.GSRequest+<>c__DisplayClass4.<Complete>b__1 ()
GameSparks.Platforms.PlatformBase.Update () (at Assets/GameSparks/Platforms/PlatformBase.cs:67)
GameSparks.Platforms.PlatformBase.OnDestroy () (at Assets/GameSparks/Platforms/PlatformBase.cs:88)
So what does this mean that I have a NullReferenceException: Object reference not set to an instance of an object
What am I missing here? Is there a tutorial you can point me to?
Thanks in advance!
Try:
- Oldest First
- Popular
- Newest First
Sorted by Oldest FirstRyan Fuller
Hi Maria,
Given your object here:
You don't want to call GetGSData("the_text"), because the the data is a string. So you would just call response.scriptData.GetString("the_text");
Basically, if the json key has a dictionary as its value, you would call GetGSData, if it has an array you call GetOBJECTList where OJECT is the type of object the array contains (e.g. GetStringList), and if its a root value (not dictionary or array) just call the corresponding get function for that object type (in your case, GetString).
Hope that helps.
Maria Mercedes Martinez
Hi Ryan,
Thanks for this information, so far what you are saying makes sense about string vs data but I still can't get it to work...
In my button code that I had (you can see it again in my OP) I changed this line :
GSData data = response.ScriptData.GetGSData("the_text");
To:
GSData data = response.scriptData.GetString("the_text");
As you suggested. But its not working. I get this error:
GameSparks.Api.Responses.LogEventResponse' does not contain a definition for `scriptData' and no extension method `scriptData' of type GameSparks.Api.Responses.LogEventResponse' could be found (are you missing a using directive or an assembly reference?)
I also tried it like this:
GSData data = response.ScriptData.GetString("the_text");
But then I get this error:
Cannot implicitly convert type `string' to `GameSparks.Core.GSData'
Any help is greatly appreciated!
Ryan Fuller
Try:
Maria Mercedes Martinez
Yes! This works! thanks so much... I will now make my collections a bit more complex and continue learn how to manipulate them with this new info you have given me.
Thank you SO much for your time on this. Greatly appreciate it!
Ryan Fuller
No worries! Glad I could help :)
-
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