Sign In Register

How can we help you today?

Start a new topic
Answered

Access runtime collection data using client API

If I had custom game data on NoSQL similar to Hearthstone example, how would I be able to retrieve this information on the client?


Would I have to create an event and write an associated Cloud Code?  If so how would the Cloud Code send the data back to the client?


I am missing something fundamental, but I can't seem to find this information in the docs or forums.


Best Answer

Hi Min Tsai,


You are correct, you would retrieve the data in cloud code (attached to an event) and return this data to the client by using the setScriptData() method:


Spark.setScriptData(name, value);


Please see the following tutorials which may help you (please note that these are from our new documentation site which is still in Beta)...


https://docs.gamesparks.com/getting-started/using-cloud-code/

https://docs.gamesparks.com/tutorials/database-access-and-cloud-storage/managing-data-persistence.html


Hope these help,


Jonathan.



Hi Min, 

You can add data to the response by using: Spark.setScriptData("key", "value");

Depending on the value type you the parse using getint() or getString() etc. 

Does this answer your question?


Best Regards, Patrick. 

Answer

Hi Min Tsai,


You are correct, you would retrieve the data in cloud code (attached to an event) and return this data to the client by using the setScriptData() method:


Spark.setScriptData(name, value);


Please see the following tutorials which may help you (please note that these are from our new documentation site which is still in Beta)...


https://docs.gamesparks.com/getting-started/using-cloud-code/

https://docs.gamesparks.com/tutorials/database-access-and-cloud-storage/managing-data-persistence.html


Hope these help,


Jonathan.


Thanks for pointing me to the documentation.

 

hi. i want to achieve same result but cant. i can json of my runtime collection but can't bind it to my object or can't get it using getString(). any help would be appreciated.

Thanks.

Hi Saad Anees,


As mentioned in Jonathan's solution, you would add the data to your event's response using:


Spark.setScriptData(key, value);


You can then retrieve this on the client end in your response code using:


GSData scriptData = response.ScriptData;

string myString = scriptData.GetString("myStringKey");


Note that GSData's various 'Get' functions will fail if the key specified isn't present, or if it contains a value not of the type specified by the 'Get' used. If you are having issues getting this to function, it may be worth logging the contents of your scriptData prior to attempting to retrieve individual fields to ensure that they are present and of the correct type.


Let us know how you get  on with this.


Regards,

Vinnie



Hi Vinnie,


I am doing the same.


var myCol = Spark.runtimeCollection('MyCollection');

Spark.setScriptData("MyCollection", myCol);


And i am retrieving it using:


response.ScriptData.GetGSData("MyCollection").JSON


It throws null exception.

And FYI I am getting my JSON string fine.

Hi Saad,


If possible could you provide us with the null exception you're getting?


You are returning the entire collection in scriptData? This would be an array as opposed to an object. As such you would need to use something like:


List<GSData> coll = response.ScriptData.GetGSDataList("MyCollection);


to retrieve it.


Regards,

Vinnie

Hi Vinnie,


Yes it was a list. Your method is another way to retrieve it. My concern was why couldn't I retrieve it from the JSON i got earlier.


Anyway I can mark this as a answer for others.


Thank you for the support.


Regards,

Saad Anees

Hi Saad,


Glad that worked for you. I believe the null exception you were receiving was due to the 'get' you were using (GetGSData), not matching the data type you were trying to retrieve (an array). To break down the code you were using:


response.ScriptData.GetGSData("MyCollection").JSON


response.ScriptData - would have worked as expected returninng a GSData type.

.GetGSData("MyCollection") - this would have returned null as the value in the 'MyCollection' field is an array, not an object.

.JSON - This is, I believe, where your exception would have been thrown because you're trying to get the JSON field of a null object (the return value from 'GetGSData').


I hope this helps clear things up for you.


Regards,

Vinnie


Login to post a comment