Sign In Register

How can we help you today?

Start a new topic
Answered

How do I query an object in my NoSql database?

I have created a custom table in my NoSql database explorer.
This table contains general game and configuration data that I want to download to the client.


I am looking through the API and forums but cannot find how this data is queried.


I am using the Unity 5 SDK.


Thanks


Best Answer

Thanks for all the help, I found the following code on the forums that uses LogEventRequest to query any event from the Unity3D client...

 

new LogEventRequest().SetEventKey("GET_RACE_DATA")
            .SetEventAttribute("TRACK", "Track1")
            .Send(r =>
            {
                if(r.HasErrors)
                    Debug.LogError("Error getting GET_RACE_DATA : " + r.Errors.GetString("DETAILS"));
                else
                {
                    var gsData = new GSData(r.ScriptData);

                    object[] list = gsData.GetObjectList("fastestRaceData").ToArray();

                    foreach (var l in list)
                    {
                        var dic = (Dictionary<string, object>)l;
                        foreach(var key in dic.Keys)
                            Debug.Log("key = " + key.ToString());
                    }
                }
            });

 


Daniel check out Youtube and the GameSparks docs for tutorials on how to set those things up.

This sounds exactly like what I am looking for (query something from the pool of NoSQL-Datasets).

Could you help me on how to Setup the GameSpark-Side for the above code? Cloud Code, Events?

Thanks,

Daniel

Answer

Thanks for all the help, I found the following code on the forums that uses LogEventRequest to query any event from the Unity3D client...

 

new LogEventRequest().SetEventKey("GET_RACE_DATA")
            .SetEventAttribute("TRACK", "Track1")
            .Send(r =>
            {
                if(r.HasErrors)
                    Debug.LogError("Error getting GET_RACE_DATA : " + r.Errors.GetString("DETAILS"));
                else
                {
                    var gsData = new GSData(r.ScriptData);

                    object[] list = gsData.GetObjectList("fastestRaceData").ToArray();

                    foreach (var l in list)
                    {
                        var dic = (Dictionary<string, object>)l;
                        foreach(var key in dic.Keys)
                            Debug.Log("key = " + key.ToString());
                    }
                }
            });

 

Hi,

      To be frank I didn't try on this much as remove call on collection was not working earlier. You can refer to previous post on the problem started by Luis i guess. Sadly till now there was no response from GS team on that.


However,

 I had a quick test as not having enough time currently, sorry if its not much clear enough, but i'm sure it might help you for sure.

 

var loginTime       = Date.now();
var playerId        = Spark.getPlayer().getPlayerId();
var playerName      = Spark.getPlayer().getDisplayName();

//For saving
var playerLoginDataCollection = Spark.runtimeCollection('PlayerLoginDetails');

//For Query
var findQueryResult = playerLoginDataCollection.find({"playerId" : playerId});
Spark.setScriptData("findQueryResult", findQueryResult); //For debugging in Test harness

//For Removing entry
var removeQueryResult = playerLoginDataCollection.remove({"playerId" : playerId});
Spark.setScriptData("removeQueryResult", removeQueryResult);//For debugging in Test harness


//For Saving
playerLoginDataCollection.save({"playerId":playerId ,"playerName":playerName, "loginTime":loginTime});

 


Let me know if I can help you out.


Btw, the above is a cloud code i attached to an event. Just create an event with the required details and get the data by Spark.getData().ATTRIBUTE_NAME  and you can save the details in the corresponding cloud script similar to above what I did.


Thanks,

VoxelBusters.com

@voxelbusters

Do you have an example of how to call these cloud code events from the client?

I've looked everywhere but nothing in the docs explains how to do this.


Thanks 

Yes Spark API is cloud API.


I see only possibility passing via Cloud code trough events. I mean fetch the results in cloud code and send as result for an event api call.


How do I access that from the Unity client API? I only see SparkCursor as CloudCode.

I suppose you are asking for collections.


Custom Game Collections

Meta Collection Ref


Have a look at SparkCursor. It could be a good place to start.