How do I Highest score

Get help using Construct 2

Post » Tue Apr 18, 2017 10:18 am

Good morning,

I'm trying to keep the highest score, the game is saved alone.
I'm able to make you save the punctuation you do at that time, but if you then repeat and take out less saves you instead of leaving the highest.
some help?
Thank you
B
6
S
2
Posts: 33
Reputation: 528

Post » Tue Apr 18, 2017 10:45 am

Create two global variables: g_score, g_hiscore.
Add the Local Storage object to your project.

Add this code to the start of your main event sheet:

1. System|On start of layout: LocalStorage|Check item "savehiscore" exists
2. LocalStorage|On item "savehiscore" exists: System|Set g_hiscore to LocalStorage.ItemValue
3. LocalStorage|On item "savehiscore" missing: System|Set item "savehiscore" to 0

This code checks whether you've already saved a hiscore to Local Storage - if you have it loads that value into your g_hiscore global variable, if you haven't it creates the item in Local Storage and then assigns it the value of 0.

During your game session add whatever points you are scoring to g_score.

At the end of the game session and before you start the next session add the following code into your event sheet:
Edit: I forgot to include a trigger once:

1Trigger Once:
>>System| if g_score>g_hiscore: System|Set g_hiscore to g_score; LocalStorage| set item "savehiscore" to g_hiscore

This code checks whether g_score is greater than g_hiscore and if so updates "savehiscore" in LocalStorage.

If you've accessed other items in LocalStorage between retrieving "savehiscore" at the start and updating it at the end you may need to retrieve it again before updating (I think? Maybe someone can confirm)

Hope that helps! :)
Last edited by mekonbekon on Tue Apr 18, 2017 12:02 pm, edited 2 times in total.
B
20
S
8
G
3
Posts: 456
Reputation: 4,051

Post » Tue Apr 18, 2017 11:08 am

I've tried it and it has not worked for me, because what I want to keep is seconds.
I can always keep the last score, but not the highest.

thank you
B
6
S
2
Posts: 33
Reputation: 528

Post » Tue Apr 18, 2017 12:04 pm

I've updated the code above as I'd missed out a "trigger once" in the second section.
This should work for saving your highest score, not your most recent - it's what I'm currently using in my projects without any problems.

Do you have a capx you can share?
B
20
S
8
G
3
Posts: 456
Reputation: 4,051

Post » Wed Apr 19, 2017 10:21 am

You got it, thank you very much.
B
6
S
2
Posts: 33
Reputation: 528


Return to How do I....?

Who is online

Users browsing this forum: adventurist, Blendi1223, PROxMTswatTR, Yahoo [Bot] and 35 guests

cron