question

joon avatar image
joon asked

call cloudscript function from Unity Editor

is it possible to call a cloudscript version as an admin, to use it in editor scripting?

In our case it would be for testing and admin.

the code I have is giving me this error:

PlayFabException: Must be logged in to call this method
PlayFab.PlayFabCloudScriptAPI.ExecuteFunction (PlayFab.CloudScriptModels.ExecuteFunctionRequest request, System.Action`1[T] resultCallback, System.Action`1[T] errorCallback, System.Object customData, System.Collections.Generic.Dictionary`2[TKey,TValue] extraHeaders) (at Assets/PlayFabSDK/CloudScript/PlayFabCloudScriptAPI.cs:69)
LeaderboardManager+<SubmitGhostStatic>d__22.MoveNext () (at Assets/Scripts/Managers/LeaderboardManager.cs:282)
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () (at <695d1cc93cca45069c528c15c9fdd749>:0)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_0 (System.Object state) (at <695d1cc93cca45069c528c15c9fdd749>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:153)
UnityEngine.UnitySynchronizationContext:ExecuteTasks() (at /Users/bokken/buildslave/unity/build/Runtime/Export/Scripting/UnitySynchronizationContext.cs:107)
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.

joon avatar image
joon answered

I figured it out thanks to an answer on a different question.

Here's my (simplified) code to call ExecuteFunction from Unity editor (not playmode/runtime).

public static async void CallCloudScriptEditorTime(string somePlayfabID)
    {
        var isDone = false;
        var entityToken = "";
        PlayFab.AuthenticationModels.EntityKey entity = null;

        PlayFabAuthenticationAPI.GetEntityToken(
            new PlayFab.AuthenticationModels.GetEntityTokenRequest(){}, 
            (result)=>{
                isDone = true;
                Debug.Log(result.EntityToken);
                entityToken = result.EntityToken;
                entity = result.Entity;
            },
            (error)=>{
                isDone = true;
                Debug.Log(error.GenerateErrorReport());
            }
        );

        while (!isDone)
        {
            await Task.Delay(1000); 
        }

        PlayFabCloudScriptAPI.ExecuteFunction(new ExecuteFunctionRequest() {
            AuthenticationContext = new PlayFabAuthenticationContext("",entityToken,somePlayfabID,entity.Id, entity.Type),
            Entity = new PlayFab.CloudScriptModels.EntityKey() { Id = entity.Id, Type = entity.Type },
            FunctionName = "MyFunction",
        }, (result) => {
            // Do something with result
        }, (error) => {
            // Error
        });
    }

link to the other question: https://community.playfab.com/questions/56881/calling-playfabcloudscriptapiexecutefunction-with.html

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

If you call the client API of executing CloudScript, you will need to login as a client and provide the access token. You can call the server API of executing CloudScript, which requires a secret key of your title. In Unity you will need to enable the server API first before calling any server API. You can enable it in the [PlayFab Editor Extension]->[Settings]->[API]->[Enable Server API].

2 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.

joon avatar image joon commented ·

The function I'm using goes through PlayFabCloudScriptAPI

Can you direct me to the other function? I have the server and admin API's enabled.

Looking through the repo:

https://github.com/PlayFab/UnitySDK/search?q=executefunction

I can only see one way (and an instanced way?) to call executefunction. Is there some other syntax I could be using?

0 Likes 0 ·
joon avatar image joon commented ·

The function I'm using goes through PlayFabCloudScriptAPI

Can you direct me to the other function? I have the server and admin API's enabled.

Looking through the repo:

https://github.com/PlayFab/UnitySDK/search?q=executefunction

I can only see one way (and an instanced way?) to call executefunction. Is there some other syntax I could be using?

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.