question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

How can I make this API call?

Hello everyone,

I am new to cloudscripting, and I just want to ask a basic question to learn most things. I put comment lines starting with "QUESTION" in the code. If someone can fill those gaps, I think, I can learn the most basic things.

Here's what I am trying to do: I will get an integer and a string value from the client. On the server side, I will increase the integer value and change the string value. Afterwards, I will return the new values to the client. The code doesn't make any sense; however, it will help me a lot if I can fill these gaps.

Unity C# code:

public void APICallExample()
    {
        int i = 5;
        string s = "How are you?";

        ExecuteCloudScriptRequest request = new ExecuteCloudScriptRequest()
        {
            FunctionName = "Example",
            FunctionParameter = // QUESTION: How can I send those 2 parameters (i and s) to the server?
        };

        PlayFabClientAPI.ExecuteCloudScript(request, OnSuccess, OnFail);
    }

    public void OnSuccess(ExecuteCloudScriptResult result)
    {
        // QUESTION: How can I read the parameters from the result?
    }

    public void OnFail(PlayFabError error)
    {
        // Print message
    }

Cloudscript:

handlers.Example = function (args) {
    // QUESTION: Is this correct way to check if the parameters "i" and "s" are null?
    if(args && args.i && args.s) { 
        //  QUESTION: How can I return constant int value and string value (0 and "WrongParameter") ?
        // return 0 and "WrongParameter" ?
    }
    
    // QUESTION: Is this correct way to read parameters?
    var i = args.i;
    ++i;
    
    var s = args.s;
    if(s == "How are you?") {
        s = "I'm fine";
    }
    else {
        s = "What do you mean?";
    }
    
    // QUESTION: How can I return i and s ?
    // return i and s ?
};<br>
10 |1200

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

1 Answer

·
brandon@uprootstudios.com avatar image
brandon@uprootstudios.com answered

We do it like this:

public void APICallExample() {
    int i = 5;
    string s = "How are you?";

    PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest{
        FunctionName = "Example",
	FunctionParameter = new {
	    I = i,
	    S = s
	}
    },
    result => {
        //do something with the result
    },
    error => {
        //do something with the error
    });
} 


Then you can access it in the cloudscript like this:

handlers.Example = function(args, context) {
    if (args && args.I && args.S) {
        //do something with the args
    }
}

Hope this helps!

3 comments
10 |1200

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

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

Thank you for the answer.

There is 2 more questions that I need to find out how to make it.

- How can I return the new of values of "I" and "S" from the cloudscript?

- After returning values from the cloudscript, how can I read them in Unity? (FunctionResult variable is an object, and I am trying to return 2 values from the cloudscript.)

0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Ozan Yilmaz commented ·

@Ozan Yilmaz >> How can I return the new of values of "I" and "S" from the cloudscript?

handlers.Example = function (args) {

    if (args && args.i && args.s) {

    } else {
        //  QUESTION: How can I return constant int value and string value (0 and "WrongParameter") ?
        // return 0 and "WrongParameter" ?
        
        return 0; // or (return "WrongParameter";)

    }

    // QUESTION: Is this correct way to read parameters?
    // It's correct.
    var i = args.i;
    ++i;

    var s = args.s;
    if (s == "How are you?") {
        s = "I'm fine";
    } else {
        s = "What do you mean?";
    }

    // QUESTION: How can I return i and s ?
    // return i and s ?
    return {
        i: i,
        s: s
    }
};


0 Likes 0 ·
Sarah Zhang avatar image Sarah Zhang Sarah Zhang commented ·

@Ozan Yilmaz>>After returning values from the cloudscript, how can I read them in Unity? (FunctionResult variable is an object, and I am trying to return 2 values from the cloudscript.)

 public int i = 5;
    public string s = "How are you?";
    public void APICallExample()
    {
        PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest
        {
            FunctionName = "Example",
            FunctionParameter = new
            {
                i = i,
                s = s
            }
        },
        result =>
        {
            string resultStr = PlayFabSimpleJson.SerializeObject(result.FunctionResult);
            Debug.Log(resultStr);
            var objs = result;
            Dictionary<string, object> dic = PlayFabSimpleJson.DeserializeObject<Dictionary<string, object>>(result.FunctionResult.ToString());
            if (dic.TryGetValue("i", out object a))
                i = Convert.ToInt32(a);
            if (dic.TryGetValue("s", out object b))
                s = Convert.ToString(b);
        },
        error =>
        {
        });
    }
0 Likes 0 ·

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.