We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
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.
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?
Like that?
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?
Second, how would you store it in the text file?
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! ;)