This site requires JavaScript, please enable it in your browser!
Greenfoot back
bernhard
bernhard wrote ...

2014/1/21

Save values permanently

bernhard bernhard

2014/1/21

#
I am making a game and I added an offline highscore list. It just saves all scores and puts them in the right order. Everything works fine but of course when I close greenfoot and open it up again, all values are reset to 0. I guess I'd just have to admit that, however my teacher told me, it would be possible to save and load the values, so that they are still there after closing abd opening greenfoot. I just wonder wether that is right. Is there any way? PS: I know one can make online highscore lists using the User Info, anyway I want to make it offline.
bernhard bernhard

2014/1/26

#
Nobody knowing an answer?
danpost danpost

2014/1/26

#
Show what code you are trying to use to save the highscores permanently.
bernhard bernhard

2014/1/26

#
I did not try yet as I thought it wouldn't be possible. The scores are static fields of an Actor "HighscoreList".
danpost danpost

2014/1/26

#
'static' fields only retain their values between compilations of the project. In other words, you can reset the project as many times as you like and the highscores will be maintained; but, once you re-compile the project, those fields are reset. BTW, any project loaded when greenfoot loads is compiled at that time; so, exiting and returning to greenfoot resets the values of those 'static' fields. The only way, without the use of the UserInfo storage, to save highscores like you want is to write a file to disk with the data and read them back in when needed (programmatically, of course).
bernhard bernhard

2014/1/26

#
Could you please tell me I can find an understandable tutorial to do that?
danpost danpost

2014/1/26

#
This Java tutorial should have what you need. If you wish to just jump into trying to code it without a fair understanding of what you are doing, go to the page called 'Data Streams' in the 'I/O Streams' section.
Super_Hippo Super_Hippo

2014/1/26

#
You can use the UserInfo when you are offline, too. On the website, it is not possible, but in the program, it is. It creates an Excel file "storage.csv" where the scores are saved and can be loaded again. This is probably much (!) easier than creating a file yourself and so on.
bernhard bernhard

2014/1/26

#
Ok I will take a look at both possibilities. Thank you guys.
You need to login to post a reply.