Sign In Register

How can we help you today?

Start a new topic
Answered

How to get the object ID in code

I have the following code I use to get the values out of some scriptData... 

var list = new GSData(r.ScriptData).GetObjectList("playerCustomData").ToArray();

foreach (var l in list) 
{
    var dic = (Dictionary<string, object>)l;
    var name = dic["name"].ToString();
}

 This works fine, but what I am trying to do now is get the actual object ID stored in the $oid field in the collection, but I'm not sure how to do this.


Best Answer

Hi Greg,


You can set your own id using:


Spark.runtimeCollection("playerLives").insert({"_id" : "yourCustomId", "playerId":Spark.getPlayer().getPlayerId()});



Alternatively, to make the it more readable for Unity:


//find a document

activeDocument = activeCollection.findOne({"_id" : {"$oid" : idToFind}});


//Clean up the id so it's easier to read

    activeDocument.Id = activeDocument._id.$oid;

    delete activeDocument._id;


instead of

{

 "@class": ".LogEventResponse",

 "scriptData": {

  "object": {

   "_id": { $oid : "55cc930de4b0098175ad8388" }

  }

 }

}




To generate a GUID you would use something like this,

 

var myGuid = guid();

 

function guid() { 

       function s4() {

           return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);

       }

       return s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4();

   }


Thanks,

Liam


1 person has this question

You can create an event and pass back to your application any data using Spark.setScriptError( "report", {key:"someString"} ); 

From your app you can request event by logEventRequest.


Script Error looks like thing to avoid, but it's good to use for returning some data to app after requests.


Use this reference for info about cloud code usage:

https://docs.gamesparks.net/documentation/gamesparks-service-cloud-code-api

Also how do I generate a GUID in cloud code? 

Answer

Hi Greg,


You can set your own id using:


Spark.runtimeCollection("playerLives").insert({"_id" : "yourCustomId", "playerId":Spark.getPlayer().getPlayerId()});



Alternatively, to make the it more readable for Unity:


//find a document

activeDocument = activeCollection.findOne({"_id" : {"$oid" : idToFind}});


//Clean up the id so it's easier to read

    activeDocument.Id = activeDocument._id.$oid;

    delete activeDocument._id;


instead of

{

 "@class": ".LogEventResponse",

 "scriptData": {

  "object": {

   "_id": { $oid : "55cc930de4b0098175ad8388" }

  }

 }

}




To generate a GUID you would use something like this,

 

var myGuid = guid();

 

function guid() { 

       function s4() {

           return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);

       }

       return s4() + s4() + s4() + s4() + s4() + s4() + s4() + s4();

   }


Thanks,

Liam


1 person likes this
Login to post a comment