question

nigelbess avatar image
nigelbess asked

How to Check if Username is Available- Unity C#

I made a script with the PlayFab API that checks if a username is available. My approach seems really dirty to me, So I'm wondering if there is a better way.

And how can I set this up so that CheckUser() returns a bool?

public static void CheckUser(string username)

{

string password = GameFunctions.RandomString (16); //generates a 16 digit random string

LoginWithPlayFabRequest request = new LoginWithPlayFabRequest ()

{

Username = username,

Password = password,

};

PlayFabClientAPI.LoginWithPlayFab (request, OnCheckUserStub, OnCheckUser);

}

static void OnCheckUserStub(LoginResult result)

{

//this method is a placeholder and should never be called

}

static void OnCheckUser(PlayFabError error)

{

if (error.ToString().Contains ("AccountNotFound"))

{

Debug.Log ("available");

} else

{

Debug.Log ("taken");

}

}

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

·
brendan avatar image
brendan answered

What's the intended use case? If the idea is that you want to display an "available" next to the input field and check every time the player types, that's not going to work, as you be generating a huge number of login calls per user - most of which would be rejected due to exceeding the limit on calls to that API endpoint.

If you're just trying to give the user feedback for their selected username when they're done typing, just use the RegisterPlayFabUser call - that'll also return the info you need.

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

nigelbess avatar image nigelbess commented ·

Thank you for the fast reply!

This function gets called when the user clicks a button.

RegisterPlayFabUser isnt working for me.

I either give it all the required requests (username, password, etc.) and it successfully creates a new account. I just want to check the username, not create an account.

Or I only give it a username and it returns the "InvalidParams" error, which doesnt give me information I need.

Also, RegisterPlayFabUser is still a void, meaning I cant return a bool from it right?

0 Likes 0 ·
brendan avatar image brendan nigelbess commented ·

I don't follow - what specifically is not working when you call RegisterPlayFabUser. In the case where that call succeeds, the return contains the Session Ticket for the user.

0 Likes 0 ·
nigelbess avatar image nigelbess brendan commented ·

I dont want the user to have to input their password, email, etc when checking for username availability.

So if the username is available, I still dont want to register an account.

0 Likes 0 ·
Show more comments

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.