How do I generate a random character string

Get help using Construct 2

Post » Tue Jan 19, 2016 2:24 pm

Hi everyone,

I'm currently designing a game for which I need to put an online database with highscores. Thanks to @ArcadEd 's excellent tutorial, everything went smoothly, but now I face another problem.

The game exports scores to the database at the end of every level. This means that if you scored 600 points in level 1 and 700 in level 2, your total score will be 1300. However, as the score is send at the end of every level, this single session will export scores of 600 and 1300 for this player. This is problematic because of course I want only one score to be send per game session, not one per level!

Is there a way to avoid this?
One way I thought about doing this is to create a random (long) string of characters and add this to the database under "sessionID" so that each session correspond to a single ID and the score is sent to the same place of the database, thus avoiding multiple scores to be send from the same game.

Is there a better way to do this that you can think of?
If not... well I have troubles creating this random string of characters (it should use most ascii characters). I should probably make a loop and append a random character at the end of a variable, but is there any way easier to select any random character than having a random number between 33 and 126 and extracting the character corresponding to this ascii code?

Not exactly sure how to translate this into Construct 2 either... Or maybe is there any string generator out there in the plug ins?
Thanks for any help! :)
B
23
S
5
G
1
Posts: 335
Reputation: 2,406

Post » Tue Jan 19, 2016 4:43 pm

What you want is called a "hash", it is a random string generated using a seed input. You can use the CB Hash plugin to do this easily in C2.

You can just use random(1000000) as a seed if you need a randomized hash.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822

Post » Wed Jan 20, 2016 10:50 am

@gumshoe2029
Thanks for the reply. The CB Hash plugin is pretty close from what I am looking for. But if I understand this plugin correctly (which I may not) it doesn't seem to me that it generates random strings. As you state yourself, it uses a seed and generate a character string based on it, right?

So if I use random (1000000) as a seed, well... It does the same job as just using random(1000000) as the unique code in my case. I don't necesarilly need to have that code scripted.

But anyway, thanks for pointing out the CBHash plugin to me. I installed it and it will be useful someday for sure. ;)

EDIT :
If anyone else need to do this, here is what I did :

On start of layout
> repeat 30 times --> text > append keyboard.stringfromkeycode(floor(random(42)+48))

set sessionID to text.text


And now I've got my text variable sessionID with a unique 30 character string composed of any random character chosen from numbers from 0 to 9, letters from A to Z and a bunch of special characters.

If you need more information about keyboard.stringfromkeycode and stuff, you should look at this excellent tutorial by @Waltan :
https://www.scirra.com/tutorials/739/your-own-textbox
B
23
S
5
G
1
Posts: 335
Reputation: 2,406

Post » Fri Jan 22, 2016 6:02 pm

You are exactly correct in that it doesn't generate random strings, which is why I said you have to seed it with the random number. It just takes the text value of that number and turns it into a more random string.

Uniform random number generation is not cryptographically secure it you are wondering, so do not use it for things like hashing passwords (and even session ids is probably not a good idea). But this is the 'easy mode' answer.

Even your example is not cryptographically secure and could probably be guessed by a good cryptanalysis program.

If you want to put a little work into it, I can give you more options.

If you want to search yourself, the key phrase is "cryptographically secure" for any library that you are looking at.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822

Post » Fri Jan 22, 2016 9:30 pm

@gumshoe2029
Thanks a lot for your concern, but I don't really need something 100% secure. I use this session ID just to figure out that the score of this game has already been injected in the database sooner. It isn't related to password or anything that need high security.

But it is very possible that one day I'll need something cryptographically secure, so I'll refer to this post if it happens.

Thanks again!
B
23
S
5
G
1
Posts: 335
Reputation: 2,406

Post » Sat Jan 30, 2016 5:04 pm

Sessions can be hijacked if the session ID is not cryptographically secure. Just FYI.
https://en.wikipedia.org/wiki/Session_hijacking
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822

Post » Wed Feb 03, 2016 1:19 pm

Thanks for the link. I'll check with the technical guys if this is required.
B
23
S
5
G
1
Posts: 335
Reputation: 2,406

Post » Wed Feb 10, 2016 4:01 pm

It is only required if you want your application to be secure, and not have clever folks hacking your users.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822

Post » Thu Feb 11, 2016 6:34 pm

This code snippet provide a quick way to generate UUID.
http://jcward.com/UUID.js

You could use browser ExecJS to use it or wrap the snippet in a simple plugin with a single expression.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Sun Apr 09, 2017 7:16 pm

I had the need to generated v4 UUIDs for a project I was working on so I created a c2addon. Link below.

This addon contains a global plugin which allows you to insert version 1 or version 4 UUIDs via an expression.

http://zerglabs.github.io/c2-addons/uuid.c2addon

Image
B
33
S
7
G
6
Posts: 22
Reputation: 5,594


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests