question

Sebastian Kranzinger avatar image
Sebastian Kranzinger asked

Is it possible to combine multiple requests in a single one while still using the API-wrapper for Unity?

When loading the game, we currently send multiple requests to PlayFab (GetCatalogItems, GetTime, GetPhotonAuthenticationToken, etc.). This means however, that we have to wait for a response to all requests until we can finish loading.

It is possible to send a single ExecuteCloudScriptRequest, get all data synchronous is cloud-script and send a single response containing the all responses. The problem is, that we then would have to extract the data with a custom JSON-wrapper, which would require a lot of work, whereas now we just use the handy PlayFab wrappers that basically do everything for us.

So here's my question:

Is it possible to get the data using an ExecuteCloudScriptRequest, split it into parts (lets say one for the response to GetTime, one for GetCatalogItems, and one for GetPhotonAuthenticationToken) and feed the parts to the PlayFab wrapper to get a GetTimeResult, a GetCatalogItemsResult, and a GetPhotonAuthenticationTokenResult?

apissdks
10 |1200

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

Sebastian Kranzinger avatar image
Sebastian Kranzinger answered

Hi Brandon,

Thanks for your answer! Your example would be possible, however, we would have to parse the result ourselves, since ExecuteCloudScriptResult.FunctionResult is just a JSON.

Is there anyway to extract a GetCatalogItemsResult, a GetTimeResult and a GetPhotonAuthenticationToken from the result in the client (using the UnitySDK)?

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.

Sarah Zhang avatar image Sarah Zhang commented ·

The result of CloudScript is JSON, we need to deserialize it on our own. Please navigate to this thread for the reference.

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

For a couple of them, you could consider using GetPlayerCombinedInfo with CloudScript or with the Client API.

Another alternative (that I haven't done, so I can't confirm it would work) would be to maybe add your results to a list or array in CloudScript, then return the list and have the client parse the results. For example:

//CloudScript
handlers.exampleFunction = function(args, context) {
    //example requests:

    var catalog = GetCatalogItems();
    var time = GetTime();
    var token = GetPhotonAuthenticationToken();

    return [catalog, time, token];
}



//Client
private void GetData() {
    PlayFabClientAPI.ExecuteCloudScript(new ExecuteCloudScriptRequest {
        FunctionName = "exampleFunction"
    }, result => {
        //do something with the result
    }, error => {
        //do something with the error
    }
}
10 |1200

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

kamyker avatar image
kamyker answered

I'm using async wrapper and then simply call Task.WhenAll to send multiple requests.

public static class PFAsync
{
	public delegate void PlayFabMethod<TIn, TOut>( TIn r, Action<TOut> o , Action<PlayFabError> e, object c, Dictionary<string, string> h);

	public static Task<TOut> Run<TIn, TOut>( PlayFabMethod<TIn,TOut> playfabMethod,
		TIn request, object customData = null, Dictionary<string, string> extraHeaders = null )
	{
		var tcs = new TaskCompletionSource<TOut>();

		Action<TOut> resultCallback = result => tcs.SetResult( result );
		Action<PlayFabError> errorCallback = error => tcs.SetException( new Exception( error.GenerateErrorReport() ) );

		playfabMethod.Invoke( request, resultCallback, errorCallback, customData, extraHeaders );

		return tcs.Task;
	}
}
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.