We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello, I have a question. I want to save the highscorelist in my game. Could you help me save and load the highscores? I have not found anything here that would solve my problem. Here is my code.
int[] scores = new int[5];
int score;
void setup() {
size(1260, 720);
for (int i=0; i<scores.length; i++){
scores[i] = 0;
}
}
void draw() {
background(0);
textSize(80);
fill(255, 0, 0, 255);
text("Highscores ", 300, 160);
for (int i=0; i<scores.length; i++) {
textSize(60);
text(scores[i], 570, 300+80*i);
}
}
void keyPressed() {
score = int(random(100));
addNewScore(score);
}
void addNewScore(int score) {
for (int i=0; i<scores.length; i++) {
if (score>=scores[i]) {
for (int j=scores.length-1; j>=max(i,1); j--) {
scores[j] = scores[j-1];
}
scores[i] = score;
break;
}
}
}
Answers
I would be very happy about help. :)
Look at loadStrings and saveStrings in the reference please
Previous relevant posts:
https://forum.processing.org/two/search?Search=Highscores
Kf
@Chrisir I tried to use save and loadStrings, but it doesn't work. If you know how it works, please show me how to implement this to my code. I'm new in processing and I tried for 2 weeks to save and load the Strings. Sorry, but your answer is crap. My code is about 20 lines. if you know how, then please show me ;)
@kfrajer Do you really think, that your post help? There is nothing that can solve my problem in that post. Absolutely nothing. If you are able to do that, why you did not show me? My code is about 20 lines, please show me Einstein...
I think that you both are not able to do that and try to advise me to other topics. You want to answer my question with crap posts but you wouldn't help me. I postet my code to show you my problem but you don't look at it or tried to understand my problem. load and saveStrings doesn't work for me. If you really want to help, show me how to implement this into my code and please don't answer if you don't know how...
@MigiModo
You get feedback based on the information you provide in your post. The information you provided is vague as you **didn't describe your problem. Since we don't know what your problem is, then you get redirected to the reference so you can read it and learn how to properly use the functions provided by Processing.
I check the post I provided to you. It is the second or third link that provides a working example in java storing highscores: https://forum.processing.org/two/discussion/17222/highscores-after-exiting/p1
The search engine in the forum is not optimized but it works if you ask the right question.
Please, be polite to the people in the forum. Most people (all people?) here are volunteers.
Kf
@kfrajer I told you, that this example doesn't work for me. If you really want to help, show me how to implement this in my code and not redirecting me in examples they didn't work for me. You see in my code, that I didn't use any Strings and I don't know how to implement this to save. I asked for help and you send me to other links were are no solutions for my problem. That ist no help. I ask 2 times to told me how to implement this. If you are unable to do this, then please let other people help me. Sorry for my bad english. how can I save my int arrays in my highscorelist and how can I load them. That is the question. I'm not able to do this and ask for help. Is that too heavy to understand? save and loadStrings doesn't work in my code yet. I don't know how to implement this. I'm 14 years of age and I'm new in processing. Redirecting to links were are no solutions is not help in my opinion. please show me how to use save and loadStrings in my code...
I give all informations you need to help me. But you redirecting me to other therms and topics they don't help me. You see my code. Can you help me to save and load this or not? Which information do you need???
@kfrajer I did not use Android... I tried to save my own highscorelist in my code and load this in java. You understand me or not? I don't know how to use save and loadStrings in my code. I read all the preferences in processing but I don't find any working examples. And in this whole forum there is no solution for my problem. Then please don't redirecting me to crap that doesn't work for me. I only need a little help to save the scores in my code. I did not know that this is too heavy for this forum...
https://Forum.Processing.org/two/discussions/tagged/highscore
@GoToLoop very funny, very funny... How old are you guys? I'm 14 years of age and I think I'm smarter than you guys. I think nobody here is able to help me with my code and you want to kidding me with your crap links they doesn't help me... I'm here in questions about code and not in questions about links.. I answered a question and I was very precisely in my opinion. very funny, to make fun about an 14 year old guy he's tryin' to figure out a thing. Wow. Is it to heavy to answer for my code? nobody have to or is forced to help me, but kidding is not funny. I say it 3 times and every time I was satisfied about an answer... It is a crap link. thanks for nothing... I search for a german forum with smart guys...
@MigiModo as much as you would like to, you cannot force someone to help you by yelling at them or being rude. I know it can be frustrating to receive short answers, but the truth is, that ALOT of the users that post questions haven't even looked at the reference or tried to solve the question themselves, which means that the same questions get postet time after time, and when you've been on this forum for a while, you get quite tired of the same old questions.
Now, I will try to help you with your problem if you can manage to not yell me out. If you still want, that is.
Personally, I would use a table rather than loadString(), mostly because I am more familiar with that, but also because you might want to save more details along with the highscore later. And using a table, you can easily add that at a later stage.
Here is example I found in the reference on Table:
Line 7-9 names the columns of the table, which can be quite useful for later use. You can see that in line 12-14, we use these names to decide which column we want to add data to. The first argument is the name of the column, the second argument is data we want to store in that column. But how do we know which row we are storing this in? Well, we know, that we are storing it in a new row below the previous because of line 11, where we add a row.
So, how do you implement this into your sketch?
You are already pretty much done with the first step, because you already have an array with your highscores. step 2 and 3 is mostly copy and paste and you seem to be quite known with for-loops, so I think you will figure this out.
If you have any difficulties, just @mention me
I forgot to say that when you save a table, you have to specify what kind of table you want it to be. Most commonly used are csv, which stands for comma separated value, and tsv, which is short for tab separated value. As you can imagine, csv seperates the values with a comma, and tsv separates the values with a tab.
This means that you can crate your own table by opening up textEditor write your table (a new row in the editor is a new row in the table) separate you values with either commas or tabs. When you are done with your table, you can save it as a csv og tsv, depending on how you separated the values. And then you can load that into your sketch with loadTable();
@Eeyorelife first of all, thanks for your answer. I don't yelling at anybody. I asked a clear question about code and got links to other topics where are no solutions. I said 3 times that I don't want to get links because they don't help me and they thought it was funny to spam me full with links. I tried to implement your idea in my sketch, but I'm not able to do that. I got no plan which value of which array is beeing used. By the way I got trouble to understand all the things in English. thanks for your help and I will try it now a few times more. :) Maybe I can do it by my own.
Ok, I think, nobody in this Forum ist able to implement this to my code. Everybody posted links or examples but nobody tries to help me to implement this to my code. How should I learn something, when nobody teaches me the right way. No teacher in the world gives you examples without an explaination. If anyone is able to help me, then I would be glad if you show me how to implement this in my code. Thank you...
If I were the best programmer in the world and can implement every example without asking other people, then I don't have to ask for help. Think about it...
The support of this program is very bad and I think that this program is to complicated to start learning.
I am sorry, I am on holiday away from home and only on my iPhone. Therefore I cannot really help you.
Looking at your code in your original post: does the code in itself work?
Is the addNewScore adding a new score so that the list stays sorted?
Did you try saveStrings (scores, "score.txt");
or so? What happened?
Most people here help by giving hints instead of providing full code solutions so people can find out on their own. I can understand this is frustrating at times.
Best, Chrisir ;-)
You would be surprised to know how many people ask for solutions without ever trying to search for an example before asking. Besides, you don't have to be the best programmer in the world to be able to implement an example.
Ok, so they key to being able to implement any exapmle into your code is to understand the example. So for a moment, you should forget your own code and just play around with the example. Try to modify it until you are sure you understand how it works. Because then you can pull it apart and put it back together again, which is useful when you want to implement it into your code.
Here is what we start with (the example):
And we want to be able to modify it so that we can use it for our purpose. I want to be able to add new rows to the table more dynamically. Because as the code is now, I am only adding the data I have predetermined. Which is quite boring. So I am going to make a function addData(); Which will create a new row and then add some data to that row. But if we want to be able to store this information for longer than the duration of the sketch, we have to save this table. So we want another function saveT(); which will just save the table. But kind of data do we want to add? For now, let's just add the location of the mouse when I press the leftt-button and save the table when I press right-button. Here is a pseudocode:
So now, we can just take all the code from the example and place paste it into this structure. I don't want to change anything other than the structure just yet, I want to be sure we don't have any errors. Now it looks like this:
Remember: you have to save this sketch in order to check if it works. So if you save the file, then run it and press the mousebutton a couple of times, then go to open up the .csv file in the data folder, you will see something like this:
So we know it works, now we just have to modify it a little bit so we it does what we want it to do. In setup() we change the names of the columns to id, x, y. We want addData() to be able to receive the x and y location of the mouse so we do this:
Note that I changed line 5 and 6 from newRow.setString() to newRow.setInt(), because we want numbers, not words. And lastly, in mouseClicked we pass the mouse positions to addData() Here is the result:
Now, I feel like I really understand the code. I know what it does and I know why it acts like it does. I have really gotten to know the Table class and I feel like I am ready to implement it into your code. If you feel the same way you should not look at what comes next. If you feel like you understand the code you should try to do this last step for yourself. Maybe even if you don't feel like you fully undersand. Because you will learn a whole lot more if you actually fo it yourself, than if you just copy my result. My result might not even be the best one. You know your own code best, so you know how to should behave. But yeah, here is my solution:
Ok, first thank you for your continued efforts. My game works completely. My only problem is that I want to save my highscores so that they are loaded automatically at startup. I tried everything, but it doesn't want to succeed. I would like to show you my whole game, so you can see that I have given myself efforts and being not a fool. @Eeyorelife your code do not work. I tried it in 3 different ways, but it doesn't work. Excuse me for being so rude, but it is very frustrating when you get such difficulties in the end. Thanks for your help.
Here can you download my game and can see what is my problem. My highscore system works very fine, but it doesn't save or load the highscores after I close the program. Maybe you have to play it before you understand my problem and can help me better. :)
https://www.dropbox.com/sh/99c7ksmltcgfq5d/AABTYMKApMoJJdXZTSiGjMrfa?dl=0
you have to download the complete application folder on the top right and then direct download.
How so? Does the sketch not run? Do you get an error? I don't think you are a fool. If you explain what the problem is, then we might be able to fix it.
This doesn't help me in helping you. I don't know what you tried or what went wrong. If you post the code that didn't work for you, someone will likely be able to point out why it isn't working and how you can improve it.
If you look at my final sketch you may notice that I am saving the table, but not loading it. Which means that the sketch do not receive any information from the table, it only writes to it. Is that your problem? If so, you can either create a .csv file manually and load it with your sketch. Or you can try to load a .csv file and if it doesn't excist, then you create a new one.
Your sketch run, but the scores are not loading when I start the sketch. It saves an .csv file into the data folder but it doesn't work to save the scores correctly and load them by starting the sketch. For today I got a blackout and will not try it again, before I destroy my PC. Why is such a simple function so complicated? I think it should be easier to save and load values. I'm 14 years of age, live in Germany, my English is limited and I'm no college student or something. I started with 0% of knowledge. I gave my very best into my game and all ends with saving and loading the scores... It is ridiculous :) I made this for my internship in August. I want to become a programmer in the future, but this program make me cry. I really have to think about my future plans. :)
Originally, I wanted to enter a name for the highscores, but that was too complicated for me. There is too little help or support for this program on the Internet. But I understand, that this is for college students and not for school boys. I can not describe my problem better than I do it before. I just want to save and load the scores. Very simple, but a very hard challenge in Processing.
The next thing that will be posted here is "use loadTable"...... if anyone want to post this, please show me in my code, because I don't know how to use this function and I only can learn from running systems and not broken parts. Thanks for your help and for your time. I appreciate this.
Anyone can learn how to code, but a small mistake can be a huge pain in the ass. I can help you tomorrow as I am busy today. Sometimes it is good to take a breake and come bakc to the code later.
Hello,
back from holidays.
Here is my version with
loadStrings
based on your very first post above.Best, Chrisir ;-)
New Version with Name Input - unfortunately more complicate than I thought.
Better stick with the previous version without names...
Chrisir
Or use table() ?
Yes, I think
table
would be more elegant; the code gets complicated though because of entering a name....Here is my suggestion without names:
And here is my suggestion with names:
My code is maybe a bit messy, so I apolagize for that, but if something is unclear, just ask and I will reply.
@Chrisir I hope you enjoyed your holidays. both of your new codes to save my scores do work perfectly and are very good explained. Thanks @Chrisir and thanks @Eeyorelife for your efforts. The methods for the name input are not that what I imagined. I try to make a code and show you guys what I mean. I'm so thankful.
in my first version there are no names, in my 2nd version the player can enter his or her name and end the input with return
of course you can change the design of the input box or place it on the screen
glad you like it!
Chrisir
For everyone who had the same problems like me. Here is the code to save scores without names. :) thanks to Chrisir and Eeyorelife!!
closing this one, continued here:
https://forum.processing.org/two/discussion/23605/enter-a-name-to-a-highscore-list-with-4-letters