Sign In Register

How can we help you today?

Start a new topic
Answered

Unity, How to get child property values from response scriptData

Hello,


I'm stuck and can't find any documentation on this even though it must be pretty common.  I need to get child properties from my scriptData responses.  In my code below I can get all the top level property values, but I can't dig down and get their child properties.  The code I get to pull the main property values is below, and I also showed some example data of what I need to retrieve.


GSData data = response.ScriptData;
 object[] levelsList = data.GetObjectList("playerCreatedLevels").ToArray();

 foreach(var find in levelsList){
 var dic = (Dictionary<stringobject>)find;
 string levelName = dic["playerName"].ToString();  //This works good for getting a top level properties, but how do I get a child of playerName or any other main property if exists?


 "_id": {
  "$oid": "#####" <-----Need this but, don't know how to get
 },
 "playerId": "#####", <------Already able to get this
 "playerName": "example user", <------Already able to get this
  "Contents": {
   "Size": {
    "Width": "64",  <-----Need this but, don't know how to get
    "Height": "64"  <-----Need this but, don't know how to get
},

Best Answer

Get getobject and getobjectlist are deprectaed and you should switch to using getgsdata and getgsdatalist, and access values with the gsdata "get" methods. In your case it would be something like this:

GSData data = response.ScriptData;
List<GSData> levelsList = data.GetGSDataList("playerCreatedLevels");

foreach(GSData level in levelsList){
    string playerName = level.GetString("playerName");
    GSData contents = level.GetGSData("Contents");
    GSData size = contents.GetGSData("Size");
    float width = size.GetFloat("Width");
    float height = size.GetFloat("Height");
}

    

1 Comment

Answer

Get getobject and getobjectlist are deprectaed and you should switch to using getgsdata and getgsdatalist, and access values with the gsdata "get" methods. In your case it would be something like this:

GSData data = response.ScriptData;
List<GSData> levelsList = data.GetGSDataList("playerCreatedLevels");

foreach(GSData level in levelsList){
    string playerName = level.GetString("playerName");
    GSData contents = level.GetGSData("Contents");
    GSData size = contents.GetGSData("Size");
    float width = size.GetFloat("Width");
    float height = size.GetFloat("Height");
}

    

Login to post a comment