How to save Level Progress?


#1

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?


#2

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.)


#3

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.


#4

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.


#5

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.


#6

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


#7

#8

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:


#9

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");
}

#10

Processing.org/reference/String_equals_.html

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


#11

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


#12


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)


#13

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?


#14

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