question

Steven Gay avatar image
Steven Gay asked

How to send List to cloud script? Not a Map,How to send a list of objects using ExcuteCloudScript? (not a dictionary)

Hi

I am trying to send a list of objects to the server - not a map but I couldn't find how to read it on the server.

public class Skill 
{    
   string name;
   int level;
   int skillpoints;
}

// LIST not MAP as the same skill can be trained several times
public static List<Skill> skillsInTraining = new List<Skill>();

public static void SaveSkillTrainingQueue()
{
        var test = PlayFab.Json.JsonWrapper.SerializeObject(skillsInTraining);


        var request = new ExecuteCloudScriptRequest
        {
            FunctionName = "SaveSkillTrainingQueue",
            FunctionParameter = new
            {
                skillsInTraining = skillsInTraining.Count,
                skills = test,
                
            },
            GeneratePlayStreamEvent = true
        };
        
        PlayFabClientAPI.ExecuteCloudScript(request, OnSaveSkillTrainingQueueSuccess, PlayFabBridge.PlayFabErrorCallback);
}

How can I access each skill.name and level in the server?

function SaveSkillTrainingQueue(args)
{
    var count = args.skillsInTraining;
    var skills = args.skills;
    
    log.debug("SaveSkillTrainingQueue count= " + args.skillsInTraining); // OK
    log.debug(" " + JSON.stringify(skills)); 

    //for (var skill in args.skills)
    //{
    //    log.debug(" " + JSON.stringify(skill));
    //    log.debug(" " + skill.name);
    //}

    var tmp = JSON.stringify(args.skills);
    for (var i=0; i<count; i++)
    {
        log.debug(i + "=" + tmp[i]);
        log.debug("name = " + tmp[i].name);  // <--------- NOK      
    }    
}

,

Hi

I am trying to send a List - not a Dictionary - of objects to be executed by a script.

public struct Skill { string name; int level; int skillpoint; }

// NOT a map as the same skill can be multiple times
public List<Skill> skillInTraining = new List<Skill>(); 

public static void SaveSkillTrainingQueue() {
        var test = PlayFab.Json.JsonWrapper.SerializeObject(skillsInTraining);

        var request = new ExecuteCloudScriptRequest
        {
            FunctionName = "SaveSkillTrainingQueue",
            FunctionParameter = new
            {
                skillsInTraining = skillsInTraining.Count,
                skills = test,
            },
            GeneratePlayStreamEvent = true
        };
        PlayFabClientAPI.ExecuteCloudScript(request, OnSaveSkillTrainingQueueSuccess, PlayFabBridge.PlayFabErrorCallback);
}

How can I access the values such as the skill.name on the server?

My attempts so far:

function SaveSkillTrainingQueue(args)
{
    var count = args.skillsInTraining;
    var skills = args.skills;
    
    log.debug("SaveSkillTrainingQueue count= " + args.skillsInTraining); // ok
    log.debug(" " + JSON.stringify(skills));  // nok

    //for (var skill in args.skills)
    //{
    //    log.debug(" " + JSON.stringify(skill));
    //    log.debug(" " + skill.name);   not found
    //}

    var tmp = JSON.stringify(args.skills);
    for (var i=0; i<count; i++)
    {
        log.debug(i + "=" + tmp[i]);
        log.debug("name = " + tmp[i].name);  // not found
        
    }   
}
apisunity3ddata
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Steven Gay avatar image Steven Gay commented ·

humm... the text was send twice...

0 Likes 0 ·

1 Answer

·
v-humcin avatar image
v-humcin answered

Your method of passing in the list will work with a small change. The "skills" object is passed in as string, so you will need to use JSON.parse() instead of JSON.stringify() to turn it into a JSON object which will allow you to access the fields inside.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.