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


High Score Saving

billybobjoe1223 billybobjoe1223


Hey, I'm making a game, and I'm wondering how to make a file and then edit/read it so I can store high scores. Any help on this will be appreciated.
webmessia webmessia


I Had a similar issue with a game I've made in Greenfoot. What I've been working on is a library that connects to a website to store information. So games using the library ask the user for a username and password then your game can pull information like high scores, game progress ect. That is however far from finished. In the mean time I suggest you try the Preferences API. It easier for storing simple data than writing to files. However It will not work on the Greenfoot Gallery only on exported jar app's run outside of the browser. Sorry if this code doesn't work it was written in the browser very quickly :P. I will try to write an example for saving to files for you another time (unless someone else beats me to it). But in the mean time this will let you do some cool stuff.
import java.util.Preferences;

public class HighScores extends Actor {
    private Preferences prefs;
    private int highestScore = 0;
    private boolean prefsWorking = false;
    public void addHighScore(int score){
        /*If in an applet we don't have permision to use preferences and 
         * a SecurityException will be thrown. If this happens we set
         * prefsWorking to false
        try {
            prefs = Preferences.userNodeForPackage(HighScores.class);
            prefsWorking = true;
        } catch (Exception e){
            prefsWorking = false; 
            //We now get the value from preferences
            highestScore = prefs.getInt("yourgamename_highestscore",0);
            if(score > highestScore){
                 //If the score is higher than the highest score we put it's value as the new highest score
                prefs.putInt("yourgamename_highestscore", score);
The only extra thing i'd add is that this stores information in the default area as all actors and worlds in greenfoot are in the default package. If you like a bit more organisation where we put HighScores.class you could create a folder in your greenfoot project folder and name all lower case the name of your game then put a new class in there and reference that for example
prefs = Preferences.userNodeForPackage(billygame.SomeClass.class);
. Not necessary, but good practise. Keep an eye out on the discuss section as I might release some prototype of my online data store idea if I ever get around to it. Hope I've been of help
PiRocks PiRocks


To read/write to a file, you need to use the package or one of the java.nio.* packages. There is a section about this in the java tutorials here. One important thing to remember about io is that it won't work on the gallery.
mik mik


Both replies here have pointed out that you cannot write to a file (or connect to another website) when running in the Gallery. That is true. The reason is security: If any applet running on a web page was able to write to your file system, then just opening a web page could raise hell on your computer. The web page might contain an applet that erases all your files and replaces them with cats on skateboards. To prevent this, applets (and Greenfoot scenarios in the Gallery are applets) are run in a "sandbox" -- i.e. they are forbidden to do anything that could be dangerous. And that includes accessing your local file system. If you run the same scenario locally on your computer (by downloading the source and running it in Greenfoot, or by running a jar file) it could do anything it wants. Including replacing all your MP3s with copies of Friday. So be careful with downloading and running code you don't know. We (the Greenfoot Team) are discussing adding functionality to Greenfoot for storing highscores. This would involve a server running on our own machine (which hosts the Greenfoot Gallery) and providing methods for scenarios to store data there. Accessing the server the scenario came from is the one network access that applets are actually allowed to do. (Since, if you don't trust the host sever, then you're in trouble anyway.) So that would allow high scores from the Gallery. This is currently under discussion, and may come in a future Greenfoot update. Michael
You need to login to post a reply.