question

Kim Strasser avatar image
Kim Strasser asked

Problem with updating player display name with cloud script

I want to update the display name but my cloud script code always fails. I want that a player can only update his display name once(when the display name is not configured), therefore I wrote this code: if (DisplayName != null).

Why is my cloud script code not working?

Raw event JSON
{
    "EventName": "player_executed_cloudscript",
    "Source": "CloudScript",
    "FunctionName": "UpdateDisplayname",
    "CloudScriptExecutionResult": {
        "FunctionName": "UpdateDisplayname",
        "Revision": 16,
        "FunctionResult": null,
        "FunctionResultTooLarge": null,
        "Logs": [],
        "LogsTooLarge": null,
        "ExecutionTimeSeconds": 0.00042869999999999996,
        "ProcessorTimeSeconds": 0,
        "MemoryConsumedBytes": 24552,
        "APIRequestsIssued": 0,
        "HttpRequestsIssued": 0,
        "Error": {
            "Error": "JavascriptException",
            "Message": "JavascriptException",
            "StackTrace": "ReferenceError: DisplayName is not defined\n    at handlers.UpdateDisplayname (BFD0A-main.js:4:5)"
        }
    },
    "EventNamespace": "com.playfab",
    "EntityType": "player",
    "TitleId": "BFD0A",
    "EntityId": "2948776B356793A8",
    "EventId": "1b31881167554232b591fc39187d67eb",
    "SourceType": "BackEnd",
    "Timestamp": "2019-09-16T15:23:27.1164935Z",
    "History": null,
    "CustomTags": null,
    "Reserved": null,
    "PlayFabEnvironment": {
        "Vertical": "master",
        "Cloud": "main",
        "Application": "logicserver",
        "Commit": "91b7bbc"
    }
}

My cloud script code:

handlers.UpdateDisplayname = function (args, context)
{
    var NewDisplayname = args.DesiredDisplayname;
    if (DisplayName != null)
      server.UpdateUserTitleDisplayName({PlayFabID: currentPlayerId, DisplayName: NewDisplayname});
}

Client code:

private async Task UpdateDisplayname()
{
    var result = await PlayFabClientAPI.ExecuteCloudScriptAsync(new ExecuteCloudScriptRequest()
    {
        FunctionName = "UpdateDisplayname",
        FunctionParameter = new { DesiredDisplayname = "Mynewdisplayname" },
        GeneratePlayStreamEvent = true
    });

    if (result.Error != null)
        Console.WriteLine(result.Error.Error.ToString());
    else
        Console.WriteLine("Your new displayname: " + "Mynewdisplayname");
}
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.

1 Answer

·
Sarah Zhang avatar image
Sarah Zhang answered

UpdateUserTitleDisplayName isn’t included in Server API set, but in Client and Admin API sets. CloudScript methods have full access to PlayFab's Server API set and currently can’t access to Admin API set. So, you can call the Client API UpdateUserTitleDisplayName in the Clients directly.

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.