I have implemented a system where a player can send a friend request to another player by writing to their read-only data (using a cloud script) and waiting for that player to accept, and returning an "accept" message - as described in some of the answers on the forums. Works great!
This is fine in a game when I know the playfabID of the friend I want to add.
But if I only know the player's display name (and not their playfabID), it's hard to write to that player's read-only data. Here's a cloud-script solution that I came up with, but it seems really messy. Is there a better way to do this?
handlers.AddFriendByName = function (args, context) { var myID = currentPlayerId; var friendName = args.friendName; var myName = args.myName; //Try to add friend for player ID var result = server.AddFriend({ "PlayFabId": myID, "FriendTitleDisplayName": friendName }); //If fail, return {} meaning friend name was not found if (result.apiError != null) { return {}; } //Hack to find friend ID: //List all friends and search for the one we just added var friendsresult = server.GetFriendsList({ "PlayFabId": myID }); var friendID = ""; if ("Friends" in friendsresult) { var friends = friendsresult.Friends; for (var i = 0; i < friends.length ; i++) { if (friends[i].TitleDisplayName == friendName) { friendID = friends[i].FriendPlayFabId; break; } } } if (friendID == "") return {}; //Remove friend from playfab (so that we can send a request instead) server.RemoveFriend({ "PlayFabId": myID, "FriendPlayFabId": friendID }); //Call handler function to create a new friend request using the friend ID var requestArgs = { "friendID": friendID, "myName": myName }; handlers.addFriendRequest(requestArgs, null); //return {ID} return {ID : friendID} }