Sign In Register

How can we help you today?

Start a new topic
Answered

Generate Unique Alpha-Numeric Code

Hey,


Does GameSparks provide functionality to create a unique code per player, ideally alpha-numeric? This would be used for player's to add each other as friends, so it has to be user-friendly which the _id isn't due to it's length.


I'm thinking a combination of base64 and a counter might be the best approach for this, if there is no built in functionality. Although it looks like btoa doesn't work in cloud code, so might have to write a function for this instead.


Thanks!


Best Answer

Hi David,


We don't have a built in function to do this. There are plenty of methods available to do this online which could be used in Cloud Code. I've posted one below.

 

var myUuid = generateUUID();

function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxx-xxxx-4xxx-yxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return uuid;
};
Spark.getPlayer().setScriptData("uuid", myUuid );

Spark.setScriptData("uuid", myUuid)

 

Thanks,

Liam


Answer

Hi David,


We don't have a built in function to do this. There are plenty of methods available to do this online which could be used in Cloud Code. I've posted one below.

 

var myUuid = generateUUID();

function generateUUID() {
    var d = new Date().getTime();
    var uuid = 'xxxx-xxxx-4xxx-yxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return uuid;
};
Spark.getPlayer().setScriptData("uuid", myUuid );

Spark.setScriptData("uuid", myUuid)

 

Thanks,

Liam

Hi Liam, thanks for the quick response. Yeah I've seen the examples like that, although my understanding is that there still can be collisions, although the chances are very slim.


Hi, where can we put the code?. 
I'm new to Gamesparks.

Hey, will you going to reply or not?

Hi,


This code can go into the Cloud Code for any of your events on the platform.


Regards,

Liam

Login to post a comment