question

dario-1 avatar image
dario-1 asked

CloudScript not working

I have the following code in my cloud script

handlers.testScript = function (args , context){


    var request = {
            PlayFabId: currentPlayerId,
            VirtualCurrency: "GO",
            Amount: 100
    };
    var result = server.AddUserVirtualCurrency(request);
}

and this code in unity

    public void CloudScript(){

        PlayFabClientAPI.ExecuteCloudScript(


            new ExecuteCloudScriptRequest()
            {

                GeneratePlayStreamEvent = false,
                FunctionName = "testScript",
                SpecificRevision = 2

            }, exito =>
            {
            Debug.Log("Exito: "+exito.ToString());

            }, error =>
            {
            Debug.Log("Error: "+error.ErrorMessage);

            }
        );
        
    }

I ran it but nothing happens. Virtual currency is not added to the user. What am I doing wrong?

CloudScript
10 |1200

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

Seth Du avatar image
Seth Du answered

We have implemented your code and it works fine in my title, the Cloud Script is triggered successfully and VC is added to player.

It is good that we have seen that you have written a print function to output error log in the ExecuteCloudScript onFailCallback. Then, to debug Cloud Script, after confirming the cloud script is triggered by the client, please set

GeneratePlayStreamEvent = true 

in the ExecuteCloudScriptRequest, So that detailed result messages (including errors) can be observed in your PlayStream.

Additionally, there is only ONE Cloud Script can be set as the LIVE one. ExecuteCloudScript will trigger the LIVE one by default if you didn’t set SpecificRevision. We noticed that you set

SpecificRevision = 2

please make sure your custom function is existed in your Revision 2. Last but not least, please check if the VC “GO” is existed in your current title.

10 |1200

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

dario-1 avatar image
dario-1 answered

Hello, yes its working now.I dont know why it didnt work last time. However im facing another issue with the cloudscript.

Im sending parameters but i got the invalid input parameters error all the time.

Tried the following parameters in the cloudscript test page

{'input': '10000coins'}

{"input"': "10000coins"}

{input: '10000coins'}

{input: "10000coins"}

"{'input': '10000coins'}"

this is the result

{
 "code": 200,
 "status": "OK",
 "data": {
  "FunctionName": "BuyVC",
  "Revision": 4,
  "Logs": [
   {
    "Level": "Error",
    "Message": "PlayFab API request error",
    "Data": {
     "api": "/Server/GrantItemsToUser",
     "request": {
      "PlayFabId": "27F0A8344780E863",
      "Annotation": "Granted undefined to user 27F0A8344780E863"
     },
     "result": null,
     "apiError": {
      "code": 400,
      "status": "BadRequest",
      "error": "InvalidParams",
      "errorCode": 1000,
      "errorMessage": "Invalid input parameters",
      "errorHash": null,
      "errorDetails": {
       "ItemIds": [
        "The ItemIds field is required."
       ]
      }
     }
    }
   }
  ],
  "ExecutionTimeSeconds": 0.0168026,
  "ProcessorTimeSeconds": 0,
  "MemoryConsumedBytes": 54424,
  "APIRequestsIssued": 1,
  "HttpRequestsIssued": 0,
  "Error": {
   "Error": "CloudScriptAPIRequestError",
   "Message": "The script called a PlayFab API, which returned an error. See the Error logs for details.",
   "StackTrace": "Error\n    at handlers.BuyVC (C089-main.js:45:41)"
  }
 },
 "CallBackTimeMS": 133

this is the cloudscript

handlers.BuyVC = function (args,context){


    var GrantItemsToUserRequest = {
        PlayFabId: currentPlayerId,
        ItemIds: args.inputValue,
        Annotation: "Granted " + args.inputValue + " to user " + currentPlayerId
    };


    var GrantItemsToUserResult = server.GrantItemsToUser(GrantItemsToUserRequest);
    return JSON.stringify(GrantItemsToUserResult.ItemGrantResults);


}
4 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.

dario-1 avatar image dario-1 commented ·

Actually i was doing it a newbie mistake (funny part im programming for at least 20 years). Will comment this for someone with this issue.

In Unity the parameter name must be the same you request in CloudScript.

For example:

This is the ClodScript code. I´m looking for args.inputValue

var GrantItemsToUserRequest = {        

PlayFabId: currentPlayerId,        

ItemIds: args.inputValue,        

Annotation: "Granted " + args.inputValue + " to user " + currentPlayerId    

};

In Unity the code should look like

        ExecuteCloudScriptRequest request = new ExecuteCloudScriptRequest()
        {
            FunctionName = "BuyDron",
            FunctionParameter = new { inputValue = itemId, price = _price  }
        };
2 Likes 2 ·
MGGD avatar image MGGD dario-1 commented ·

@SethDu I was using:

public struct PFJAddCurrencyParameter
    {
        public string CurrencyName;
        public int CurrencyAmount;
    }

...<br>PFJAddCurrencyParameter param;
        param.CurrencyName = "CZ";
        param.CurrencyAmount = 100;
        string parameter = JsonUtility.ToJson(param);  

ExecuteCloudScriptRequest req = new ExecuteCloudScriptRequest()
        {
            FunctionName = "YEAddCurrency",
            FunctionParameter = parameter 
        };

But the CloudScript was returning errors. I logged "args" before calling the function, and in Unity result.Logs, it was something like:

{"CurrencyName":"CZ","CurrencyAmount":100}

Using args.CurrencyName caused an error when calling server.AddVirtualCurrency, because args.CurrencyName was actually = "undefined"

Then I replaced the last line in the first code with your new {}:

FunctionParameter = new { CurrencyName = "CZ", CurrencyAmount = 100 }

Now it is all working! I logged args again, and it has:

[object Object]

I expected it work with JSON, but I have no idea what is going on!

0 Likes 0 ·
Seth Du avatar image Seth Du ♦ MGGD commented ·

For the different API calls, you need verify the type of property in the request, for example in the ExecuteCloudScriptRequest, the FunctionParameter is an Object variable, not a (JSON) string, so convert a struct to JSON string is not necessary.

You should always check the type of a property, because C# is strongly typed language and you will get error if you are using the wrong type.

1 Like 1 ·
Seth Du avatar image Seth Du ♦ commented ·

if you are trying to send json date which is list<string>, the following is an example:

using Newtonsoft.Json;

var json = JsonConvert.SerializeObject(aList);

There are many ways to convert a value or object into JSON data, DataContractJsonSerializer is another option for you. Please avoid manually hard-coding your json data unless it is necessary.

1 Like 1 ·

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.