Sign In Register

How can we help you today?

Start a new topic
Answered

Unique Display Name

Hi there ! 


I've been reading a few things here and there about how to create events, inserting values into the NoSQL database and I was wondering what would be the best way to do a unique Display Name ? A cloud code example would be so useful for me to understand the basic concepts. 


Basically the way authentication would work is that the username of the player would be its email address (is that a good idea ?) and then let them enter a name of their choosing through the display name but that would be unique.


 I found this below but a bit unsure how to write the event (and test it inside test harness) so it returns an error if it already exists. Is there some kind of cloud code beginners guide somewhere ?

var playerDetails = Spark.runtimeCollection("playerList").findOne({ "userName" : "someUserName" });

 I'm new to this so I'm very open for suggestion.


Thanks ! 


Best Answer

Hi Antoine,


What you could do here is set the "userName" and "displayName" to be the exact same. As the userName has to be unique on Registration, the RegistrationResponse will return an error saying "USERNAME": "TAKEN" if it is already in use, this will ensure that both your userName and displayNames are all unique without you having to add any extra code. Do you plan on using the players email addresses for anything in game ? It would be possible to store it in a players scriptData on Registration, I can show you how to do this if you'd like. If you have any more questions just let me know.


Thanks,

Liam


Answer

Hi Antoine,


What you could do here is set the "userName" and "displayName" to be the exact same. As the userName has to be unique on Registration, the RegistrationResponse will return an error saying "USERNAME": "TAKEN" if it is already in use, this will ensure that both your userName and displayNames are all unique without you having to add any extra code. Do you plan on using the players email addresses for anything in game ? It would be possible to store it in a players scriptData on Registration, I can show you how to do this if you'd like. If you have any more questions just let me know.


Thanks,

Liam

Hey Liam ! 


The way I pictured the login would be email and password. I feel that people tend to forget their username more often then their email address (I feel it's crucial for good UX in a login system). In this case I wouldn't want to display to other people the email address of the player. I also wanted to prevent users from having too many different accounts. Creating another email is just another step that might stop people from creating multiple accounts too easily. Is there a way I could facilitate that with Gamesparks while having a unique Display Name. 

Hi Antoine, 


You could check for unique displayNames manually in the RegistrationRequest and Response if you'd like. To do it you would require a custom runtime collection, for the purposes of this example I've just called it "displayNames"


So in your RegistrationRequest Cloud Code add the following.


 

//load the displayNames collection
var displayNameCol = Spark.runtimeCollection("displayNames")

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

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

//if it's not the collection, proceed as normal
if(displayNameCheck === null){
    Spark.setScriptData("Success", displayName + " is Unique")
}

//if the displayName is already in our collection, throw an error to stop the request
else{
    throw "This displayName Is Not Unique"
}

 


Next in the RegistrationResponse Cloud Code you could add the follwoing.


   

//load our collection
var displayNameCol = Spark.runtimeCollection("displayNames")

//get the displayName 
var displayName = Spark.getPlayer().getDisplayName(); 

//insert the Unique displayName into our custom collection
var success = displayNameCol.insert({"_id" : displayName})

//response scriptata success message
Spark.setScriptData("Success", displayName + " was added to the displayNames collection")

   

That should do it, each Registration with a unique displayName will be successful and the custom collection will look like this. 


 

Hope this helps. If you have any other questions just let me know.


Thanks,

Liam

That's awesome exactly what I was looking ! That'll help me understand a bunch of things too.


Have a good one :).  

 this does not work, I use unity3d and it report error

"Runtime collections disabled - use Spark.getGameDataService() instead"


I have read through the articles on Game Data Service, but the documentation is unclear, please help, I am new :(

Hi Nguyen,


To achieve the same behaviour using the Game Data Service you could do the following:


//RegistrationRequest

var existingItem = Spark.getGameDataService().getItem("displayNames", Spark.getData().displayName);

if(existingItem.document()){

    Spark.setScriptError("error", "Display name taken");

    Spark.exit();

}



//Registration Response

var data = Spark.getData();

if(data.error) Spark.exit();

var item = Spark.getGameDataService().createItem("displayNames", Spark.getData().displayName);

item.persistor.persist();


Regards,

Vinnie

Login to post a comment