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

2012/4/9

Writing a file

Busch2207 Busch2207

2012/4/9

#
Hey guys... I try to save the best score of a Level... getting the best score for a Level works... But writing just works on my computer and not in the gallery. I tried different codes:
    public static void SaveLevelScore(int i_ReachedScore,int i_Level)
        try
        {
            int[] i_LastScore = ScoreReader.getScores();
            if(i_ReachedScore<i_LastScore[i_Level])
                return;
            i_LastScore[i_Level] = i_ReachedScore;
            OutputStream OS = new FileOutputStream("Scores.txt");
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(OS));
            for(int i=0; i<i_LastScore.length;i++)
                        writer.write(i+"#"+i_LastScore[i]+"\n");
            writer.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
I although tried:
    public static void SaveLevelScore(int i_ReachedScore,int i_Level)
        try
        {
            int[] i_LastScore = ScoreReader.getScores();
            if(i_ReachedScore<i_LastScore[i_Level])
                return;
            i_LastScore[i_Level] = i_ReachedScore;
            FileWriter fw = new FileWriter("Scores.txt");
            BufferedWriter writer = new BufferedWriter(fw);
            for(int i=0; i<i_LastScore.length;i++)
                        writer.write(i+"#"+i_LastScore[i]+"\n");
            writer.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
Where is my mistake?
kiarocks kiarocks

2012/4/9

#
The Gallery does not support file writing, if you are using 2.2.0 beta you could try using the strings, ints, or score to accomplish the same thing.
Busch2207 Busch2207

2012/4/9

#
Ah kk thank you kiarocks! Well... I although thought about using PlayerData, but there you just can save it for a player and not general for all players...
davmac davmac

2012/4/9

#
The Gallery does not support file writing
I don't understand why everyone wants to blame the gallery for this. It's a Java restriction on applets, it's nothing to do with the gallery. Busch2207: Imagine if this did work, that is, if you could read and write files on the user's computer. See the potential for evil? You could write a scenario which deleted someone's files, or installed a virus on their computer, etc - that's why you can't do it. But: you probably could make it work with PlayerData. You have 10 ints, which is enough for 10 levels - and you also have 5 Strings (with a maximum length of 50 characters per String). If you're clever, you can store numerical data in the Strings.
Busch2207 Busch2207

2012/4/9

#
I know, that u can save the Levels although in the strings. (I already use it in my scenario:http://www.greenfoot.org/scenarios/4578) I just meant, to save the best score of all users for each level, so that you can show, what was the best score a user has reached for a specific level...
davmac davmac

2012/4/9

#
so that you can show, what was the best score a user has reached for a specific level
I don't understand - if you save the score for each level into the PlayerData structure, then you can do this. Do you mean something else? Eg. you want to be able to show the highest scores for a particular level?
Busch2207 Busch2207

2012/4/9

#
Yes! That's what I mean.
K_O_P K_O_P

2012/4/9

#
@ GreenfootTeam Maybe you can add a PlayerData especially for this case? That you can call e.g. by:
PlayerData.getGeneralUser();
That always will return the same PlayerData (not depending on which user is playing)
davmac davmac

2012/4/9

#
Well, we're not going to change the API before we release Greenfoot 2.2.0. After that we can consider it. (It doesn't necessarily mean we will do it! Only that we can talk about it - and if we did, I don't think we'd call it something very different - it's not data for a user at all; it's scenario-specific data).
K_O_P K_O_P

2012/4/10

#
@ Busch Maybe you can store more ints, when u use Symbols instead of numbers if you take 100 different symbols, you can write the numers 0 to 99 in just one space! you just need a method that converts a number in this code and backwards.
You need to login to post a reply.