How to save Level Progress?

So I’m trying to make a small adventure mode for my game, and I need to be able to store the information when a level is completed. I am trying to use a PrintWriter, and it actually prints the right result into a text file, but when the game restarts the line gets deleted which is against the whole purpose of what I’m doing. Also, how can I tell the reader to read a specific line?

1 Like

Okay I just realised that it’s probably smarter to use the saveString function, since it saves the lines forever, but they are still being overwritten. (Oh yeah and I haven’t even gotten to the load Function, but maybe I’ll understand that one.)

Usually you would keep the whole content of the file in memory. When you want to save it, overwrite the whole file with a new file with saveStrings(). When you want to load it, load the whole file with loadStrings() and then access individual lines.

2 Likes

I think saving works now, but I have trouble loading the files. When I type in:

String[] levelProgress = loadStrings("C:\Users\name\Documents\Processing\Space_Invaders\levelProgress.txt");

it gives me the error message:

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

I’m actually not quite sure about the correctnes of the path, since I have no experience with that and don’t know how to add the file name at the end properly. In short, the whole path is correct, but the end maybe not.

You should escape your backslashes. So you need to write \\ instead of \ when inside a String.

You should be fine if you just use the filename without full path. It will be saved in the sketch folder or data folder.

3 Likes

Found it :sweat_smile: Reading the stuff in the console actually helps sometimes…

1 Like
1 Like

Thanks @GoToLoop, but I was rather looking for a solution that I can also understand at my current level :sweat_smile: Looks like I have it now anyways :stuck_out_tongue:

Okay so I have another question… What does the loadStrings function do exactly? I ask this because I’m loading the text file into the levelProgress String[], but it doesn’t really get the value. But on the other hand, it looks like it does…? :sweat_smile: When I print out the value of the first variable in the array after it got loaded, it prints the right value, but once I try to use it in an if statement Processing doesn’t recognize it or something like that. Here’s the if statement:

if(levelProgress[0] == "level0Finished"){
   println("Works!");
  }else{
   println("Ummm...");
  }

Idk wether seing this helps… Here the part where I load the file into the array again:

void setup(){
levelProgress = loadStrings("C:\\Users\\name\\Documents\\Processing\\Space_Invaders\\levelProgress.txt");
}
1 Like

if ("level0Finished".equals(levelProgress[0])) {

2 Likes

Thanks, I didn’t know about that :smile:


I have no idea what’s going on here. It even prints the right String in void setup… I going to try to make a version of the sketch with only the parts about this and post here.

(Look at the console too)

So, that’s the code:

String[] levelProgress;

void setup() {
  levelProgress = new String[5];
  saveStrings("file.txt", levelProgress);
  loadStrings("file.txt");
}

void draw() {
  if (mouseY < 200 && mousePressed) {
    levelProgress[0] = "level0Finished";
    saveStrings("file.txt", levelProgress);
  }
  println(levelProgress[0]);
  if ("level0Finished".equals(levelProgress[0])) {
    println("Works!");
  } else {
    println("Ummm...");
  }
}

This should work once you created a sketch folder. And I know that it does say “Works!” after you clicked the first time, but after the first time it should print the right value automaticly, since it is now saved. Right?

Don’t ask me how but I got it to work now. Thanks to everybody!