question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

Returning Value From Cloud Script?

Hello everyone,

I am using Unity and I am trying to return some data from the cloud script but I don't know what to do with the result. When I print the result, it is correct but how can I convert that data in Unity?

Here's the cloud script code:

handlers.TestAPI = function (args, context) {
    //... the code
    return { "Result": 0 };
}

Here's the Unity C# code, which doesn't work:

        Debug.Log(data.FunctionResult.ToString()); // It prints: {"Result":0}
        Dictionary<string, string> result = JsonUtility.FromJson<Dictionary<string, string>>(data.FunctionResult.ToString());
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

·
Sarah Zhang avatar image
Sarah Zhang answered

You can refer to the following code.

…
using PlayFab.Json;
…

public int resultNumber;
…

JsonObject jsonResult = (JsonObject)result.FunctionResult;
object value;
if(jsonResult.TryGetValue("Result", out value))
	Debug.Log(Convert.ToString(value));
	resultNumber=Convert.ToInt32(value)

// Or you can use PlayFabSimpleJson.DeserializeObject insteads of JsonUtility.FromJson

//Dictionary<string, object> dic = PlayFabSimpleJson.DeserializeObject<Dictionary<string, object>>(result.FunctionResult.ToString());


            //if (dic.TryGetValue("Result", out object value))
            //    resultNumber = Convert.ToInt32(value);
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.

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

That works perfectly. Thank you

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.