Sign In Register

How can we help you today?

Start a new topic

Check if player exist by displayname or username

 Hi all,

I wonder what's the best / easiest way to check if a player exists.

- I like to check inside unity if a certain displayname is used allready.

I don't want to do this on registration request. I need to do this check before registering/authentication

- I like to do the same with username. Check if a certain username exists in the game.

I'm new to Gamesparks so any code snippets to get me on my way would be highly appreciated.

Best Answer

 Hi again,

This is related to the same subject so i gues i just post it here and not start a new thread...

I have a simple cloudcode script attached to DeviceAuthenticationRequest.

It makes a runtimeCollection with displaynames so i can check later for duplicates.

This works fine.

But i would like to add the username or id from the player to this list but it keeps returning null for id or username, so i'm retrieving it wrong.

Can anyone please help me how to retrieve username or id in DeviceAuthenticationRequest ?


// ====================================================================================================
// Cloud Code for DeviceAuthenticationRequest, write your code here to customise the GameSparks platform.
// For details of the GameSparks Cloud Code API see			
// ====================================================================================================
//load the displayNames collection
var displayNameCol = Spark.runtimeCollection("playerList")

//get the displayName passed in from the request
var displayName = Spark.getData().displayName;

//get the user id
var userId = Spark.getData().userId;

//check the custom collection to see if the current displayName is already in use
var displayNameCheck = displayNameCol.findOne({"displayName":displayName})

if(displayNameCheck === null){ // displayname doesnt exist so we make a new list
    Spark.setScriptData("Success", displayName + " is Unique")
    // add the displayname to the list    
        "displayName" : displayName,
        "userId" : userId,



It is AuthenticationResponse, it is the response for both Device and normal Authentication Request. What I did was set a script Data in the Device Request telling the AR that it was coming from a Device Request and not a normal Auth Request.


Is there even something like DeviceAuthenticationResponse ?

Request is for verification, Response is for post-processing. You are inserting the player in the Request, so even if the request fails, they will get inserted. In the Response section, you'd do Spark.getPlayer().getUserName() or getDisplayName();


That's what i thought.

Thanks for clarifying that.


To answer your questions imply: you have to be authenticated.
I explained wrong...

- On app start it authenticates with the login account.
- The game gets on it's 'Hi there, whats your name' screen
- The game checks if that name exists (through the login account), if it doesnt exist it makes a device account. If it exists it asks for a different name.
- The player can make a real account in the game later if he desires so, then the device account gets upgraded to a regular account.


You just said you were using DeviceAuthentication on app start, so in what case would you not be authenticated ? Sorry, I'm not understanding :(


I managed to have that part working, Thanks again for putting me on the right path..

I just wondered if it was possible to do this without being logged in/authenticated.
It's not a big thing because i got it working as it is. I was just wondering if it was bad practice to do this with a 'login account' to do the check.

I think i should most likely leave it alone since it's working fine :)
Don't fix it if it's not broken, right ?


Well, the code I provided above will search username, displayname, and - copy or remove whatever query part you don't need.
I don't want them to be equal because some reason i saw on another post in this forum...

- If a player tries to login a few times with the wrong password the account gets locked.
This is for safety reasons so other players have more trouble hacking or guessing in another players account. But this allows trolls to screw other players over.
- a player wants to troll another player and just uses that others player's displany/username  and fails a few times logging in. That other player's account gets locked for multiple failed logins.
I don't say this will happen but it could happen once players know how easy it is to lock other players account.


If the Display Name needs to be unique (which, by the way, usernames already are), then set the username and displayname to be equal, and on Cloudcode just do a case-insensitive check if username == displayname. If the username is already taken, you'll get a script error.
I am doing that, but i want to have my display names unique.
Even if the player is authenticated as device.

That's why i need to check if the displayname is available.


It's better to do DeviceAuthentication as startup regardless, then convert DeviceAuth to a login via ChangeAccountDetailsRequest when the user wants to register an account.


I was wondering if there is a way to check for a username or displayname (as described above) without being authenticated as a player.

Ii need to check if a display name is taken before authenticating as device, later if the player wants to make a account it gets upgraded to one.

Right now i created a 'login account' that users first login to so they can check if a name exists. Then if it doesn't, i create a new account and authenticate with that one.

This login account gets used by all new players. If this bad practice to do it this way ?


@ward, its fine. The approval process on this forum is pretty dumb. Luckily you can see unapproved posts as they come in via email. (ie, your post technically isn't even visible to me right now, only saw the message via email.


Login to post a comment