saving a highscore

Hey guys, Can someone help me out with saving my score to a .txt file? I do somehow know that it's wrong the way a programmed it but I don't know how to code it on the right way. Can someone help me out what i suppose to do? I've yet looked up all the processing reference pages of working with saveString and loadString, but I don't know why I don't succeed at saving it to a .txt file when the score is higher than the highScore from the .txt file.

It only works when I uncomment line 27 but than it doesn't check if score is bigger than the highScore

String highScore="";

String highScore="";

int score;

void setup()
{

}

void draw()
{
  String lines[] = loadStrings("highScore.txt");
  println("there are " + lines.length + " lines");
  for (int i = 0 ; i < lines.length; i++)
  {
    println(lines[i]);
  }

  println(score);
}

void keyPressed()
{
  if(keyCode==UP)
  {
    String[] scoreList= split(highScore, ' ');
    if(parseInt(scoreList[0])>score)
    {
      highScore= str(score);

      saveStrings("highScore.txt", scoreList);
    }
  }
}

void mousePressed()
{
  if(mouseButton==LEFT)
  {
    score+=100;
  }
}

Answers

  • Whoa there, slow down. First, let's make sure we can load the file properly, ONCE (and not inside draw() (DO NOT LOAD THINGS IN DRAW)), and display the scores.

    String[] lines;
    int score;
    
    void setup()
    {
      size(400, 400);
      lines = loadStrings("highScore.txt");
    }
    
    void draw()
    {
      background(0);
      fill(255);
      text("there are " + lines.length + " lines", 20, 20);
      text("Current score: " + score, 20, 40);
      for (int i = 0; i < lines.length; i++)
      {
        text(lines[i], 20, 60 + 35*i);
      }
    }
    
    void keyPressed()
    {
      if (keyCode==UP)
    
      }
    }
    
    void mousePressed()
    {
      if (mouseButton==LEFT)
      {
        score+=25;
      }
    }
    
  • edited December 2016

    Get that working for yourself first. Hint: You may need to create a starting highScore.txt file in the right place.

    DO NOT LOAD THINGS IN DRAW. Did I say that already? It bears repeating. DO NOT LOAD THINGS IN DRAW.

    Next, think about loading things in draw. NO. NO. DO NOT LOAD THINGS IN DRAW.

    Next, think about what you would do to update your high score list. Do you want to add a new score to the list? No, probably not - you only want to remember the top 5 (or 10, or whatever) scores - not all the scores that have ever happened.

    So when you add the new score, you will then have to remove a score too. And you will want to remove the score that is the lowest. How do you know which score will be the lowest? You will need to sort the scores.

    So (DO NOT LOAD THINGS IN DRAW!) the plan is this:

    1) DO NOT LOAD THINGS IN DRAW.

    2) Add the current score to the list of scores.

    3) Sort the scores.

    4) Remove the lowest score from the list.

    5) Save the remaining list.

    Try this process yourself. It might help to create a new array of ints that is one larger that list.length. Then you can put the current high scores in (converting from strings to ints), and the last space is for the current score. Sort the array (Hint: This is such a common operation that there must be a simple function that does it, right? Check the reference!) Remove the last score (Is there a function to shorten() an array? Reference!). Then update your array of strings, and save those strings.

    Er. Yeah. Try writing this yourself and post the code of your attempt for more help.

    Also: DO. NOT. LOAD. THINGS. IN. DRAW.

  • edited December 2016

    Haha, you are making it funny thanks for answering though I will take a try by the hand of your plan, Thanks, I will come back with an update of my code but I don't know why it's so necessary to shorten my array? can't it just override?

  • edited December 2016

    Like that?

    String[] lines;
    int     score;
    String scoreList;
    
    void setup()
    {
      size(400, 400);
      lines = loadStrings("highScore.txt");
    }
    
    void draw()
    {
      background(0);
      fill(255);
      text("there are " + lines.length + " lines", 20, 20);
      text("Current score: " + score, 20, 40);
      for (int i = 0; i < lines.length; i++)
      {
        text(lines[i], 20, 60 + 35*i);
      }
    }
    
    void keyPressed()
    {
      if (keyCode==UP)
      {
        if(lines.length<5)
        {
          lines=append(lines,str(score));
        }
        else
        {
          if(parseInt(lines[lines.length-1])<score)
          {
            lines[lines.length-1]=str(score);
            for(int i=lines.length-1; i>0; i--)
            {
              if(parseInt(lines[i])>parseInt(lines[i-1]))
              {
                String setScoreLower=lines[i-1];
                lines[i-1]=lines[i];
                lines[i]=setScoreLower;
              }
            }
          }
          scoreList="";
          for (int i = 0; i < lines.length; i++)
          {
            scoreList+=lines[i]+" ";
          }
          saveStrings("highScore.txt",lines);
        }
    
        sort(lines);
      }
    
      println(lines);
      println("scoreList  "+scoreList);
    }
    
    void mousePressed()
    {
      if (mouseButton==LEFT)
      {
        score+=25;
      }
      if (mouseButton==RIGHT)
      {
        score-=25;
      }
    }
    
  • What about adding an indentity of the player's name? Your plans do work :)

  • So how do you go about that?

    • First, how do you intend to get the name of the player?

      • You can make your own way of getting text from the user (certainly not recommended).
      • You can use some existing library like G4P (recommended for your final project).
      • You can just use JOptionPane.showInputDialog() (recommended for fast testing/prototyping).
    • Second, how would you store it in the text file?

    • Third, how would you parse it at the start of your sketch?
    • Lastly, how will you display it to the screen?

    Think of all this a bit, and try finding a solution. If you don't, ask here for more help.

  • well I'm building a game (based on Minestorm II) and in the setup menu I would like to be able to write down an username before starting the game (that's no problem), I don't know whether it's better to place it in the same .txt file or in another, I will have to start splitting my Strings, right? It might not be to hard to code it, just a small upgrade. But I'll leave that for tomorrow. I will first try it on my way by the hand of your explanation. Thanks for answering.

  • Ok, ask here for any more help. Don't start a new thread if your question is related to high scores in any way at all.

  • It's okay I figured out! ;)

Sign In or Register to comment.