Sign In Register

How can we help you today?

Start a new topic
Answered

Collection Query Results To Unity

Hey there! Basically, trying to take a .find statement in Cloud Code that could return anywhere from 1 to 1000 results and get all that data back in to Unity in a smooth way.


Cloud Code:

Spark.setScriptData('items', Spark.runtimeCollection('playerItems').find({ "itemowner" : Spark.getPlayer().getPlayerId() }, {"_id": 0}));


This code queries the playerItems table to get results for items that belong to the specific player. (I feel this a good approach as all items have highly randomized stats and will be tradable later on)


Results example in Test Harness:

{

 "@class": ".LogEventResponse",

 "scriptData": {

  "items": [

   {

    "item": 6,

    "itemname": "Greatsword",

    "currenthp": 20,

    "hitpoints": 1,

    "attack": 0,

    "defense": 0,

    "speed": 0,

    "crit": 1,

    "dodge": 0,

    "lifesteal": 0,

    "regen": 0,

    "available": 0,

    "prestige": 0,

    "itemowner": "58e59"

   },

   {

    "item": 7,

    "itemname": "Shield",

    "currenthp": 28,

    "hitpoints": 0,

    "attack": 0,

    "defense": 5,

    "speed": 0,

    "crit": 0,

    "dodge": 0,

    "lifesteal": 0,

    "regen": 0,

    "available": 0,

    "prestige": 0,

    "itemowner": "58e59"

   },

   {

    "item": 2,

    "itemname": "Staff",

    "currenthp": 11,

    "hitpoints": 0,

    "attack": 0,

    "defense": 8,

    "speed": 25,

    "crit": 0,

    "dodge": 0,

    "lifesteal": 47,

    "regen": 1,

    "available": 0,

    "prestige": 0,

    "itemowner": "58e59"

   },

   {

    "item": 10,

    "itemname": "Shortsword",

    "currenthp": 16,

    "hitpoints": 0,

    "attack": 0,

    "defense": 0,

    "speed": 15,

    "crit": 0,

    "dodge": 1,

    "lifesteal": 0,

    "regen": 7,

    "available": 0,

    "prestige": 0,

    "itemowner": "58e59"

   }

  ]

 }

}


The 'item:10' is just an identifier to know what item type it is.


I got the idea of using an Array form another QA, just curious how one should go about creating, sending, then deconstructing the array once it's back in to Unity. (https://support.gamesparks.net/support/discussions/topics/1000072731)


Any help turning this Cloud Code result in to an Array that can be sent to Unity, then separated with a loop to build the player's inventory would be greatly appreciated.


Best Answer

 Hey Kamron,

Try the following code and let us know if it works for you...


 

        new GameSparks.Api.Requests.LogEventRequest()
            .Send((response) => {

                if(!response.HasErrors)
                {
                    List<GSData> items = response.ScriptData.GetGSDataList("items");
                    foreach(GSData item in items)
                    {
                        int itemNo = item.GetInt("item").Value;
                        string itemName = item.GetString("itemname");
                        int currentHP = item.GetInt("currenthp").Value;
                        int hitPoints = item.GetInt("hitpoints").Value;
                        int attack = item.GetInt("attack").Value;
                        int defence = item.GetInt("attack").Value;
                        int speed = item.GetInt("defense").Value;
                        int crit = item.GetInt("crit").Value;
                        int dodge = item.GetInt("dodge").Value;
                        int lifeSteal = item.GetInt("lifesteal").Value; 
                        int regen = item.GetInt("regen").Value;
                        int available = item.GetInt("available").Value;
                        int prestige = item.GetInt("prestige").Value;
                        string itemOwner = item.GetString("itemowner");
                    }
                }
                else
                {
                    Debug.LogWarning("GSM| Error Fetching Balance /n "+response.Errors.JSON);
                }
            });

 


Thanks,
Sean


Answer

 Hey Kamron,

Try the following code and let us know if it works for you...


 

        new GameSparks.Api.Requests.LogEventRequest()
            .Send((response) => {

                if(!response.HasErrors)
                {
                    List<GSData> items = response.ScriptData.GetGSDataList("items");
                    foreach(GSData item in items)
                    {
                        int itemNo = item.GetInt("item").Value;
                        string itemName = item.GetString("itemname");
                        int currentHP = item.GetInt("currenthp").Value;
                        int hitPoints = item.GetInt("hitpoints").Value;
                        int attack = item.GetInt("attack").Value;
                        int defence = item.GetInt("attack").Value;
                        int speed = item.GetInt("defense").Value;
                        int crit = item.GetInt("crit").Value;
                        int dodge = item.GetInt("dodge").Value;
                        int lifeSteal = item.GetInt("lifesteal").Value; 
                        int regen = item.GetInt("regen").Value;
                        int available = item.GetInt("available").Value;
                        int prestige = item.GetInt("prestige").Value;
                        string itemOwner = item.GetString("itemowner");
                    }
                }
                else
                {
                    Debug.LogWarning("GSM| Error Fetching Balance /n "+response.Errors.JSON);
                }
            });

 


Thanks,
Sean

Long time no talk! This did actually work, but unfortunately with the removal of the Runtime Collections, I now need to do basically this exact same thing with the Data Types.


1 person likes this
Login to post a comment