Sign In Register

How can we help you today?

Start a new topic
Answered

Forcing a unique displayName

Hi,


I need the displayName field in the player collection to be unique, meaning no 2 players will have the same displayName.


What I thought about doing is to have this cloud code in the ChangeUserDetails script:


   

 Spark.getData().displayName;
if (displayName) {
   var query = {"\"displayName\"" : "\"" + displayName + "\""};
    var result = Spark.find("player", query);
    var playerNameFound = (result.length() > 0);

    if (playerNameFound)
        throw "ERROR_NAME_TAKEN";
}

 However no matter what I tried, result was always empty, even if the queried displayName exists in the player collection.



https://support.gamesparks.net/support/discussions/topics/1000074284This post says that the 'player' collection is "unqueriable". Is that correct? Where can I find this in the documentation.


If it is indeed unqueriable, what is an alternative to verifying no 2 player will have the same displayName?


Thanks

Nitay


Best Answer
Hey Nitay,

I thought that might be the case, just wanted to check :)

So, in order to make sure of unique displayNames, you will need to keep a record of your own players as they register the displayName.
To achieve this, you can add some code into the ChangeUserDetailsRequest which will check for the displayName in a custom collection.
If the name exists, then you can call Spark.exit() to force the request to stop.
If the name is not in the collection you can add it.

This is handy because you can also use this collection to store additional player info when you need it.

Let me know if that makes sense or if you need any further help.

Thanks,
Sean

 


Hey Nitay,


The player collection is a system-collection so we dont give access to this collection as editing it could cause critical-errors in your game config.

However, the userName field (used when you register a user) is unique so you could use that unless there is some reason you cannot.
Please let us know.

Thanks,
Sean

Hi Sean,


Thanks for the info.

If I understand correctly in order to use the userName field the player must authenticate with a password. I'm currently using device authentication to lower registration friction for my players.

That's why I've wanted to use displayName, which is chosen by the user, as a unique name for each player.


Is there a way to do so?


Thanks

Nitay

Answer
Hey Nitay,

I thought that might be the case, just wanted to check :)

So, in order to make sure of unique displayNames, you will need to keep a record of your own players as they register the displayName.
To achieve this, you can add some code into the ChangeUserDetailsRequest which will check for the displayName in a custom collection.
If the name exists, then you can call Spark.exit() to force the request to stop.
If the name is not in the collection you can add it.

This is handy because you can also use this collection to store additional player info when you need it.

Let me know if that makes sense or if you need any further help.

Thanks,
Sean

 

Okay, will try that - Thanks!

Login to post a comment