Microsoft Azure PlayFab logo
    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Add-ons
    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA
  • Runs on PlayFab
  • Pricing
    • Blog
    • Forums
    • Contact us
  • Sign up
  • Sign in
  • Ask a question
  • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges
  • Home /
  • API and SDK Questions /
avatar image
Question by Arjuna · Apr 14, 2017 at 06:58 AM · CloudScriptFriends

Get player ID by name

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}
}
Comment

People who like this

0 Show 1
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arjuna · Apr 14, 2017 at 07:00 AM 0
Share

Note: I haven't tested this code, so I'm sure there's sytax errors.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Brendan · Apr 14, 2017 at 07:49 AM

Actually, I would advise against this design pattern, as it has a few problems. Most critically, this design will fail at scale - take, for example, the scenario where you have someone playing in your game who is well-known, such as a popular YouTube streamer. For any game with a non-trivial player population, you'll likely have hundreds or even thousands of people hitting that person with a friend request. If you're writing all requests to the same key, you'll encounter throttling on writes to the key, since only one player can write to it at a time. But also, since multiple players will be reading and then writing it, they'll be stomping each others' data constantly. Alternately, if you have each player write to a distinct key using their PlayFab ID, the player's data space will rapidly run out of keys.

For a way to implement a two-way confirmed friends system that will work using existing components, have a look at this thread, where we describe an implementation that makes use of the friend tag system: https://community.playfab.com/questions/370/206712537-Two-way-friend-confirmation-with-Cloud-Script.html

Comment

People who like this

0 Show 5 · Share
10 |1200 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arjuna · Apr 14, 2017 at 03:39 PM 0
Share

Thanks Brendan! That does sound like a better solution, and cleaner too! As someone on the other link said, it would be really useful to add this to the recipes/tutorials section as there were many solutions on the answer forums, and I wasn't sure which was the right one.

avatar image Brendan ♦♦ Arjuna · Apr 14, 2017 at 03:41 PM 0
Share

Yes, we're in the process of a fairly significant documentation update. Once we have that complete, we'll be looking at our samples repo, to see about getting more examples in there.

avatar image Arjuna · Apr 14, 2017 at 04:56 PM 0
Share
For these steps, I would still need the friend's playfab ID:
  • Use AddFriend on both players, setting each in the other player's friend list
  • Use SetFriendTags on both players, to tag the players uniquely ("requester"/"requestee", or similar)

Would I still need to call server.GetFriendsList and parse through the results to find this ID (assuming I only knew the name to begin with)

avatar image Brendan ♦♦ Arjuna · Apr 14, 2017 at 07:27 PM 1
Share

1) Yes, but you can get a PlayFab ID from any of the other unique identifiers (https://api.playfab.com/documentation/client/method/GetAccountInfo).

2) You would get the friends list to check if you have any outstanding friend requests and process them.

avatar image Arjuna Brendan ♦♦ · Apr 14, 2017 at 08:10 PM 0
Share

Ah I didn't know I could use GetAccountInfo with the other identifiers, thanks! That'll work perfect.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Navigation

Spaces
  • General Discussion
  • API and SDK Questions
  • Feature Requests
  • PlayStream
  • Bugs
  • Add-on Marketplace
  • LiveOps
  • Follow this Question

    Answers Answers and Comments

    2 People are following this question.

    avatar image avatar image

    Related Questions

    C#/UWP: AddFriend per Cloud Script fails 5 Answers

    Cloud script Friends GetPlayerStatistics vs GetFriendLeaderboard considering request limit 1 Answer

    "Internal Server Error" in GetFriendLeaderboard API 0 Answers

    How to manage Friend list manually without errors and API request limit 1 Answer

    Playfab action-event server system 1 Answer

    PlayFab

    • Multiplayer
    • LiveOps
    • Data & Analytics
    • Runs on PlayFab
    • Pricing

    Solutions

    • For Any Role

      • Engineer
      • Designer
      • Executive
      • Marketer
    • For Any Stage

      • Build
      • Improve
      • Grow
    • For Any Size

      • Solo
      • Indie
      • AAA

    Engineers

    • Documentation
    • Quickstarts
    • API Reference
    • SDKs
    • Usage Limits

    Resources

    • Forums
    • Contact us
    • Blog
    • Service Health
    • Terms of Service
    • Attribution

    Follow us

    • Facebook
    • Twitter
    • LinkedIn
    • YouTube
    • Sitemap
    • Contact Microsoft
    • Privacy & cookies
    • Terms of use
    • Trademarks
    • Safety & eco
    • About our ads
    • © Microsoft 2020
    • Anonymous
    • Sign in
    • Create
    • Ask a question
    • Create an article
    • Post an idea
    • Spaces
    • PlayStream
    • Feature Requests
    • Add-on Marketplace
    • Bugs
    • API and SDK Questions
    • General Discussion
    • LiveOps
    • Explore
    • Topics
    • Questions
    • Articles
    • Ideas
    • Users
    • Badges