WebStorage Score 'NAN' Error - Please Help!

Get help using Construct 2

Post » Wed Apr 23, 2014 12:26 pm

I have spent a couple of days now trying to integrate the webstorage option into my game to save a players best previous score.

I've followed several tutorials and also read a lot of stuff on the forum which has helped me greatly but I have hit a wall now and could really do with a little bit of help please.

Basically I have the actual score working correctly and the 'Best Score' adds up correctly too but when you exit and reload the game the best score just says NAN. I gather that means 'Not A Number' so it might just be something silly but I'm not sure what I need to change!

Here is the relevant code from Construct:

01.jpg


And here is the error message in game:

2.jpg


If anyone can help me get this working it would be greatly appreciated. This is my first try at a Construct game so it's all a learning experience but I'm getting there! :)

Many thanks.
You do not have the required permissions to view the files attached to this post.
B
5
S
1
Posts: 13
Reputation: 501

Post » Wed Apr 23, 2014 12:31 pm

Webstorage only stores strings.

First I would suggest to have a read on this: https://www.scirra.com/manual/120/webstorage

After that, convert your number to string before saving it and on load, turn it back into an integer :)
B
44
S
10
G
8
Posts: 437
Reputation: 6,463

Post » Wed Apr 23, 2014 1:45 pm

Hi 2retro, got your pm, I am taking a break from helping in forums due to some members who pushed too far.

I see you got a reply. So just to add to it int(x) changes to whole number and str(x) changes to string. X being whatever you want to change.
Recommend reading this as it is the most important page in the manual - lol.

Good luck. And you are welcome, glad the example capx helped you a bit.
Remember that highscore will only be for that player, meaning each player will have their own highscore. Web Storage saves on players pc, not on the web.

You can also change your variables to text instead of numbers then they will store as a string too. But remember if you want to treat them as whole numbers you must convert them to numbers (for calculations etc they will need to be numbers, else if for display purposes, then text (string) is fine.
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,708

Post » Wed Apr 23, 2014 2:43 pm

Thanks, guys. I'll see if I can put some of this great info into practice and report back! :)
B
5
S
1
Posts: 13
Reputation: 501

Post » Wed Apr 23, 2014 5:00 pm

OK, I've read through all of the links that were kindly provided to me now an have made some progress:

new_01.jpg


I changed the bit with the arrow and that has resolved the NAN error and now retains the best score between sessions which is great.

But now the best score continually increases along with each point scored. For example, your best score is 10. You play again and it tells you your best score was 10 but then, as you play and score more points, the best score increases by that amount of points:

new_02.jpg


Now, I know I need to convert something to 'string' in the area marked with a question mark but I'm getting rather confused! :?

If someone could perhaps tell me what I need to change (and to what) in the Scoring area to make it work correctly that would be really great.

Thanks again.
You do not have the required permissions to view the files attached to this post.
B
5
S
1
Posts: 13
Reputation: 501

Post » Wed Apr 23, 2014 5:27 pm

@2retro
You must only change best score if current score is greater than best score.

I would create a function called savescore.
Savescore does this...
BestScore = CurrentScore
webstorage set local key "bestscore" to bestscore
Highscore text = Set "Best:"&BestScore
(something like that)

then
You can compare the two and if score > BestScore then
Function: savescore

Well something like this...

Reason for issue: You are adding 1 to both score and bestscore where you should only be adding 1 to score and when score is better than bestscore, store score as new best score.

Sorry, I wrote this quickly, its late here, hope its not to confusing - lol
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,708

Post » Wed Apr 23, 2014 6:35 pm

Thank you once more for helping me. I *think* I get what you mean. I'll give it a shot and let you know... :)
B
5
S
1
Posts: 13
Reputation: 501

Post » Wed Apr 23, 2014 7:13 pm

Hmmm, still a bit confused I'm afraid. I see where I'm going wrong and the reason behind it but can't seem to get it right. :( How do I create the SaveScore Function for example? Is there a tutorial or capx file to compare against anywhere?

Maybe you can help me out a bit more tomorrow if you have time.

Thank you again for everything so far anyway. :)
B
5
S
1
Posts: 13
Reputation: 501

Post » Wed Apr 23, 2014 8:58 pm

It doesn't have to be a function it can simply be an event like this (a function would be cleaner though):

if Score > BestScore
Set BestScore to Score

Then the rest should work fine...
B
49
S
12
G
10
Posts: 1,833
Reputation: 14,583

Post » Wed Apr 23, 2014 9:56 pm

What bluephaze said.
You can compare the two and if score > BestScore then
BestScore = CurrentScore
webstorage set local key "bestscore" to bestscore
Highscore text = Set "Best:"&BestScore
(something like that)

And remove the bestscore stuff from ? mark piece.
I use functions as I can reuse the code over and over and over. Maybe a little too advanced for now.
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,708

Next

Return to How do I....?

Who is online

Users browsing this forum: evillair, gamecorpstudio, marc254, rbailey83 and 20 guests