Highscores with nicknames - Natural Sort?

Get help using Construct 2

Post » Wed Jan 01, 2014 12:33 am

Hey, I want to save the highscores with corresponding nicknames.
At the moment I have an array (1d) with 10 elements filled with strings like:
"520 | Nickname1" ,
"40 | Nickname2",
"270 | Nickname3" ,
"25 | Nickname4",
"120 | Nickname5", etc.

The built-in sort function sorts it like this. And it should be like this:

"520 | Nickname1" ,
"270 | Nickname3" ,
"120 | Nickname5",
"40 | Nickname2",
"25 | Nickname4", etc.

To sort it properly, i must convert all the elements to int(), but then i lost the nicknames. I've found something like Natural Sort [argh, i can't post links yet... "array natural sort" in Google -> first link], but is there any way to implement this in Construct 2?
Or maybe is there a tutorial how to save the highscores WITH nicknames?
Thanks in advance.
B
5
Posts: 9
Reputation: 243

Post » Wed Jan 01, 2014 7:33 am

When you are collecting your score/names why not bulk out your score so it becomes "000123" or "008765" rather than "123" and "8765" then ,I think, the sort will work.

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Wed Jan 01, 2014 8:13 am

ok after a quick test I am a bit more confident that it would work.

I thought you would have to write a routine to pad out the numbers but there is a text option/ expression called zeropad

"zeropad(number, digits)
Pad number out to a certain number of digits by adding zeroes in front of the number, then returning the result as a string. For example, zeropad(45, 5) returns the string "00045"."

and a quick capx (using beta 155)

https://dl.dropboxusercontent.com/u/143636437/examples%20for%20web/array%20sort%20test%20with%20zeropad.capx
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Wed Jan 01, 2014 2:58 pm

Thanks, good idea! It's working, but now i can't figure out how to 'un-zeropad' these scores just before putting them in textbox (to make it look nice, so "00070" will be "70", etc.)
I've tried setting text to "tokenat(Array.CurValue, 2, "0") & newline", but the number of left-side-zeros varies + the scores have zeros too = not working...

(Maybe some loop checking the nth character in the string, starting from the left and IF nth-character == "0" change it to "", ELSE end the loop + trim() it at the end? But i don't know how to check n-th character of the text in C2)Slavq2014-01-01 15:07:55
B
5
Posts: 9
Reputation: 243

Post » Wed Jan 01, 2014 5:41 pm

here is a slight edit

https://dl.dropboxusercontent.com/u/143636437/examples%20for%20web/array%20sort%20test%20with%20zeropad%20and%20backagain.capx

just take the array value into a variable and it will autmatically remve the zeros.

edit - and it looks better with the leading 0s. (in my (not very humble) opinion) RamPackWobble2014-01-01 17:45:08
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Thu Jan 02, 2014 12:06 am

Yeah but when the array contains elements with nicknames (like "00750 | BobTheDarkLord", putting it to variable deletes the nickname :(
Hmm I'll consider leaving it with the leading 0's, but it just fits better to my game's visual style without them.
B
5
Posts: 9
Reputation: 243

Post » Thu Jan 02, 2014 3:48 am

I have high scores in my demo project which should be done in a month or so. If you're saving them on a server and accessing them via an Ajax / PHP interface sorting them server-side is trivial (e.g. SELECT * FROM highscores ORDER BY score)

If you're doing it in Construct 2 I'd try using a selection sort; it's really good on platforms where your ability to directly manipulate memory elements is limited since it sorts the list in-place.nusbaumc2014-01-02 03:51:55
B
9
S
3
Posts: 37
Reputation: 749

Post » Sun Jan 05, 2014 9:47 pm

sorry - I missed the replies.

you don't need to loose the names - I will try to give you an example but it won't be for 24 hours at best.
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Mon Jan 06, 2014 8:28 am

More time than I thought !

sort array mk 2
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Mon Jan 06, 2014 11:08 am

Why not use a 2d array?
If you can only save a string for each highscore, convert the array to such a string when saving and convert the string to the array when loading.

Not a bad solution with the padded zeroes @above.mindfaQ2014-01-06 11:12:18
Visual Novel 'Engine' in 100 Events
if you ever have to choose between buying Construct 2 on scirra.com or on Steam, read this: Review
B
22
S
9
G
1
Posts: 788
Reputation: 3,788

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 6 guests