I get no error message in my CloudScript JSON file and in the client code when I use this CloudScript to create/update the player's display name. The problem is that I get no error message when the display name already exists. I found out that the display name is still null after running the CloudScript because another player already has exactly the same display name.
I want to show an error message in the client code if a player wants to use a display name that another player already uses. If no other player uses the "DesiredDisplayname", then I want to show the newly created display name to the player. How can I do that?
Raw event JSON { "EventName": "player_executed_cloudscript", "Source": "CloudScript", "FunctionName": "UpdateDisplayname", "CloudScriptExecutionResult": { "FunctionName": "UpdateDisplayname", "Revision": 25, "FunctionResult": null, "FunctionResultTooLarge": null, "Logs": [], "LogsTooLarge": null, "ExecutionTimeSeconds": 0.0702372, "ProcessorTimeSeconds": 0.016, "MemoryConsumedBytes": 37424, "APIRequestsIssued": 1, "HttpRequestsIssued": 1, "Error": null }, "EventNamespace": "com.playfab", "EntityType": "player", "TitleId": "BFD0A", "EntityId": "761EA8EC34182A11", "EventId": "620f0e43deae4ea8aba6d980016e5c74", "SourceType": "BackEnd", "Timestamp": "2019-09-20T12:05:11.1056068Z", "History": null, "CustomTags": null, "Reserved": null, "PlayFabEnvironment": { "Vertical": "master", "Cloud": "main", "Application": "logicserver", "Commit": "529e09e" } }
My CloudScript:
handlers.UpdateDisplayname = function (args, context) { var NewDisplayname = args.DesiredDisplayname; var CurrentDisplayName = ""; var resultprofile = server.GetPlayerProfile( { PlayFabID: currentPlayerId, ProfileConstraints : { ShowDisplayName: true } }); if (resultprofile.Error == null) { if (resultprofile.PlayerProfile != null) { CurrentDisplayName = resultprofile.PlayerProfile.DisplayName; if ((CurrentDisplayName == null) || (CurrentDisplayName == "")) UpdateUserTitleDisplayNameFromCloudScript(NewDisplayname, currentPlayerId); else log.info("You already have a display name. It's not possible to change it:" + CurrentDisplayName.toString()); } } } function UpdateUserTitleDisplayNameFromCloudScript(DesiredDisplayname, PlayFabId) { var contentBodyTemp = { "DisplayName": DesiredDisplayname, "PlayFabId": PlayFabId }; let url = "https://BFD0A.playfabapi.com/Admin/UpdateUserTitleDisplayName"; let method = "POST"; let contentBody = `{"PlayFabId": "${PlayFabId}", "DisplayName": "${DesiredDisplayname}"}`; let contentType = "application/json"; let headers = { "X-SecretKey": "..." }; let responseString = http.request(url, method, contentBody, contentType, headers); let responseJSONObj = JSON.parse(responseString); return (responseJSONObj.data); }
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 { if (result.Result.Logs.Count() > 0) Console.WriteLine(result.Result.Logs[0].Message); else Console.WriteLine("Your new displayname: " + "Mynewdisplayname"); } }