# How to save Level Progress?

**URL:** https://discourse.processing.org/t/how-to-save-level-progress/5156
**Category:** Beginners
**Created:** [November 3, 2018, 1:05pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156 "2018-11-03T13:05:54Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 1:05pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/1 "2018-11-03T13:05:54Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 1:30pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/2 "2018-11-03T13:30:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 3, 2018, 2:14pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/3 "2018-11-03T14:14:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 3:04pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/4 "2018-11-03T15:04:28Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [November 3, 2018, 3:05pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/5 "2018-11-03T15:05:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 3:07pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/6 "2018-11-03T15:07:32Z")

</div>

Found it 😅 Reading the stuff in the console actually helps sometimes…

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 3, 2018, 3:09pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/7 "2018-11-03T15:09:18Z")

</div>

> [@Save variable in a loop](https://discourse.processing.org/t/save-variable-in-a-loop/4654/4):
>
> /\*\* \* Load & Save Variable (v1.0.1) \* GoToLoop (2018/Oct/20) \* https://Discourse.Processing.org/t/save-variable-in-a-loop/4654/4 \*/ int a; void setup() { size(300, 200); noLoop(); fill(-1); textAlign(CENTER, CENTER); textSize(0100); final String[] aFile = loadStrings("a.txt"); if (aFile != null) a = int(aFile[0]); } void draw() { background((color) random(#000000)); text(a, width\>\>1, height\>\>1); } void mousePressed() { ++a; redraw = true; } @Override void exit()…

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 3:13pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/8 "2018-11-03T15:13:03Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 3:39pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/9 "2018-11-03T15:39:39Z")

</div>

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…? 😅 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:

```auto
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:

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

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [November 3, 2018, 3:50pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/10 "2018-11-03T15:50:26Z")

</div>

> [@Schred](#):
>
> `if (levelProgress[0] == "level0Finished") {`

> **[equals() / Reference](https://processing.org/reference/String_equals_.html)**
>
> Compares two strings to see if they are the same. This method is necessary because it's not possible to compare strings using the equality operator (==). Returns true if the strings are the sam…

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

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 3:51pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/11 "2018-11-03T15:51:21Z")

</div>

Thanks, I didn’t know about that 😄

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 4:02pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/12 "2018-11-03T16:02:16Z")

</div>

![Screenshot%20(120)](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/6d455d5deea9b20725e6dba43dc77d911256338a.png)  
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)

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 4:26pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/13 "2018-11-03T16:26:28Z")

</div>

So, that’s the code:

```auto
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?

---

<div class="post-metadata">

### Author: ![Schred](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/schred/32/13146_2.png) [@Schred](https://discourse.processing.org/u/Schred)
#### Post date: [November 3, 2018, 4:34pm UTC](https://discourse.processing.org/t/how-to-save-level-progress/5156/14 "2018-11-03T16:34:39Z")

</div>

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