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
C
Customer Support
said
almost 7 years ago
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.
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 :(
Customer Support
said
almost 7 years ago
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
K
Keeves Technologies
said
about 4 years ago
The code mentioned above does not work successfully. After attaching this code if I try to sign up using a username which is already used no error message is shown and another account is created with the same username.
I have done following steps. But I have got same result.
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.
Customer Support
said
almost 7 years ago
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
Customer Support
said
almost 5 years ago
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
A
Antoine TheGentle
said
almost 7 years ago
That's awesome exactly what I was looking ! That'll help me understand a bunch of things too.
Antoine TheGentle
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 ?
I'm new to this so I'm very open for suggestion.
Thanks !
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
- Oldest First
- Popular
- Newest First
Sorted by PopularNguyen An Phuc
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 :(
Customer Support
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
Keeves Technologies
The code mentioned above does not work successfully. After attaching this code if I try to sign up using a username which is already used no error message is shown and another account is created with the same username.
I have done following steps. But I have got same result.
Antoine TheGentle
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.
Customer Support
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.
Next in the RegistrationResponse Cloud Code you could add the follwoing.
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
Customer Support
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
Antoine TheGentle
That's awesome exactly what I was looking ! That'll help me understand a bunch of things too.
Have a good one :).
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics