question

agniesportsyt avatar image
agniesportsyt asked

How to access the variables in the cloud script ?

Now from the above mentioned cloud script, how can I access the variables - keystring, datapayload or messageGroupId in my Unity's C# Script when I call the ExecuteCloudScript. I am confused about the Json concepts and I don't know anything about Java. So please help me to get through this problem. Thank you in advance.

CloudScriptShared Group DataFriends
10 |1200

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

Rick Chen avatar image
Rick Chen answered

The variables of your Unity Script can be passed to CloudScript through FunctionParameter in Unity, for example:

private static void StartCloudHelloWorld(){  PlayFabClientAPI.ExecuteCloudScript(new
ExecuteCloudScriptRequest()  {  FunctionName =
"helloWorld", // Arbitrary function name (must exist in your uploaded
cloud.js file)  FunctionParameter
= new { name = "YOUR NAME" }, // The parameter provided to your
function 
GeneratePlayStreamEvent = true, // Optional - Shows this event in PlayStream  }, OnCloudHelloWorld, OnErrorShared);}

Then, in CloudScript, you could use the args to access the variables, for example:

handlers.helloWorld = function (args) {
                
// ALWAYS validate args parameter passed in from clients (Better than we do here)
var message = "Hello " + args.name + "!"; // Utilize the name parameter sent from client
log.info(message);
return { messageValue: message };
}

Please refer to: Writing custom CloudScript

If you have any further questions, please feel free to ask.

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.

agniesportsyt avatar image agniesportsyt commented ·

Yeah I tried, but I am getting this error : 'ExecuteCloudScriptResult' does not contain a definition for 'message' and no accessible extension method 'message' accepting a first argument of type 'ExecuteCloudScriptResult' could be found (are you missing a using directive or an assembly reference?)

1 Like 1 ·
Rick Chen avatar image Rick Chen ♦ agniesportsyt commented ·

In Unity, the ‘ExecuteCloudScriptResult’ does not contain message property. Please refer to ExecuteCloudScriptResult or the SDK document to check its structure. To get the function result, you could use ‘result.FunctionResult’ in the successful callback, which is an object. There are plenty ways to parse the object in C# and get its property, an example would be:

  void
OnSuccess(PlayFab.ClientModels.ExecuteCloudScriptResult result)
{
Debug.Log(JsonUtility.FromJson<Hello>(result.FunctionResult.ToString()).messageValue);
}

Where the Hello Class is defined as:

public class Hello
{
public string messageValue;
}
0 Likes 0 ·
agniesportsyt avatar image agniesportsyt commented ·

Please I want the answer. I am getting this type of errors for the rest of the variables too

0 Likes 0 ·
teella avatar image
teella answered

@agniesportsyt I can into the same problem. Seems the example that comes with Playfab Cloud Script and the documentation are just plan wrong.

I had to do this:

var json = JSON.parse(JSON.stringify(args));

args.inputValue just doesn't work.. but if you call JSON.stringify(args) you can see it does have values.. but doesn't seem to know it's a json object.

handlers.helloWorldZ = function (args, context) {
    var json = JSON.parse(JSON.stringify(args));
	var message = "Hello ID: " + currentPlayerId + "!";
	
	if (json && json.inputValue)
	{
	    message = message + " inputValue is: " + json.inputValue;
	}
	log.debug("helloWorldZ:", { input: JSON.stringify(args) });
	
	return { messageValue: message };
}

Maybe there is a tick box people check that isn't documented somewhere that auto converts args into a json object. But as it sits today, the example is broken and documentation a time waster figure stuff out like that.

,

@agniesportsyt I ran into the same problem. Seems like the examples don't work as document. I had to use: var json = JSON.parse(JSON.stringify(args));

args.inputValue doesn't work .. just comes up undefined.

This is from the stock example that comes with Cloud Script, all I did was do the .parse and .stringify

handlers.helloWorldZ = function (args, context) {
    var json = JSON.parse(JSON.stringify(args));
	var message = "Hello ID: " + currentPlayerId + "!";
	
	if (json && json.inputValue)
	{
	    message = message + " inputValue is: " + json.inputValue;
	}
	log.debug("helloWorldZ:", { input: JSON.stringify(args) });
	
	return { messageValue: message };
}

Maybe there is a check box somewhere in playfab that auto converts args to json or something.. but the above got me going.

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.