# How do I pass a value from one sketch to another sketch?

**URL:** <https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176>\
**Category:** Coding Questions\
**Created:** [May 22, 2020, 8:05pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176 "2020-05-22T20:05:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [May 22, 2020, 8:05pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/1 "2020-05-22T20:05:57Z")

</div>

I tried to do it by using saveStrings() in one sketch and loadStrings() in the other.

writer.pde

```auto
String[] Pipe = new String[2]; // Declare, create

void setup() {
  size(400, 400);
  noStroke();
}

void draw() {
  background(126);
  ellipse(mouseX, mouseY, 30, 30);

  Pipe[0] = nf(mouseX, 3);
  Pipe[1] = nf(mouseY, 3);

  saveStrings("../fifo.txt", Pipe);
}

```

reader.pde

```auto
String[] vals = loadStrings("../fifo.txt");

//void draw() {
  println("There are " + vals.length + " lines");
  for (int i = 0; i < vals.length; i++) {
    println(vals[i]);
  }
//}

```

This almost works. loadStrings() works if it is reading the values once but if I put the same code into draw() the error is java.lang.reflect.InvocationTargetException

Help please!

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [May 22, 2020, 10:55pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/2 "2020-05-22T22:55:53Z")

</div>

Hi,

A quick note on your `writer.pde` file :

- If you call `saveStrings()` in the draw function, then it’s going to replace and write a new file called `fifo.txt` with the new `mouseX` and `mouseY` at the same location on every frame (roughly 60 times per second). I assume it’s not what you want, you need to save all the mouse position for a certain time and at the end save the strings to a text file.

For your `reader.pde` code, are you sure that your text file is in the right folder? You shouldn’t save it outside the sketch folder (with “…/”) because [`saveStrings()`](https://processing.org/reference/saveStrings_.html) does it already.

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [May 23, 2020, 7:26am UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/3 "2020-05-23T07:26:11Z")

</div>

> [@josephh](#):
>
> For your `reader.pde` code, are you sure that your text file is in the right folder? You shouldn’t save it outside the sketch folder (with “…/”) because [`saveStrings()`](https://processing.org/reference/saveStrings_.html) does it already.

Yes, I am sure the text file is in the right location. reader.pde does not send any information nor does it save a text file. It only reads and uses the values it reads.

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [May 23, 2020, 7:26am UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/4 "2020-05-23T07:26:51Z")

</div>

> [@josephh](#):
>
> I assume it’s not what you want, you need to save all the mouse position for a certain time and at the end save the strings to a text file.

Thanks @josephh. What you suggest would work but it is not what I want. I am using the mouse locations to represent variables that continuously change value (non static?). The ellipse in the minimal example (reader.pde) should mirror the movements of the ellipse in writer.pde.

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [May 23, 2020, 10:28am UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/5 "2020-05-23T10:28:13Z")

</div>

Oh ok, you need to use `ChildApplet` to communicate between different windows : check the example by going here in the Processing IDE : `File -> Examples -> Demos -> Tests -> MultipleWindows`

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [May 23, 2020, 12:13pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/6 "2020-05-23T12:13:06Z")

</div>

Before I look at `ChildApplet` I have found a working solution!

I split this line: `String[] vals = loadStrings("../fifo.txt");`into two parts (see below). The changing values are loaded in draw() with `vals = loadStrings("../fifo.txt");`

```auto
String[] vals;

void setup() {

}

void draw() {
  vals = loadStrings("../fifo.txt");
  println("There are " + vals.length + " lines");
  for (int i = 0; i < vals.length; i++) {
      println(vals[i]);
  }
}

```

---

<div class="post-metadata">

**Author:** ![RichardDL](https://avatars.discourse-cdn.com/v4/letter/r/f475e1/32.png) [@RichardDL](https://discourse.processing.org/u/RichardDL)\
**Post date:** [May 23, 2020, 4:13pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/7 "2020-05-23T16:13:46Z")

</div>

I don’t know what kind of disk your PC has, but writing a new file at 60 Hz looks like a severe test, risk of destroying the disk after a while, and I wouldn’t do it. Suggest you install the UDP library (Sketch, Add Library, UDP… Install) then try the UDP examples. I’ve used these examples as teaching/entertainment where the young student is moving the mouse on one window, and it’s drawing on another (on same or other PC).

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [May 24, 2020, 12:39pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/8 "2020-05-24T12:39:25Z")

</div>

Ah ha, good point. I might have misled you with the mouse positions. I used `mouseX` and `mouseY` in my example because it is reproducible. The information I am actually going to pass from sketch to sketch is some Brightness levels from an image and I can do this at frameRate(1).

To represent this, I have written slowWrite.pde and slowRead.pde (below). The one remaining problem is sometimes there is nothing in fifo.txt and so I get the error report ArrayIndexOutOfBoundsException: 0

Is there a way to check that the file is present and correct before trying to read it?

pseudocode:

```auto
  if (something !=null) { // what is the something?
    println(Val[0]);
  } else {
    //return null; // what is Processing code here?
  }

```

slowWrite.pde

```auto
String[] Pipe = new String[1]; // Declare, create

void setup() {
  frameRate(1);
}

void draw() {
  Pipe[0] = str(minute());

  saveStrings("../fifo.txt", Pipe);
}

```

and slowRead.pde

```auto
String[] Val = new String[1]; // Declare, create

void setup() {
  frameRate(1);
}

void draw() {
  Val = loadStrings("../fifo.txt");

  println(Val[0]);
}

```

---

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [May 24, 2020, 1:05pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/9 "2020-05-24T13:05:52Z")

</div>

Hi,

Your issue is a famous one in computer science : [https://en.wikipedia.org/wiki/Readers–writers\_problem](https://en.wikipedia.org/wiki/Readers%E2%80%93writers_problem)

And using semaphores can help : [https://www.geeksforgeeks.org/semaphore-in-java/](https://www.geeksforgeeks.org/semaphore-in-java/)

But in this case, why are you using two running sketches instead of one? It would be much simpler

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [May 24, 2020, 1:45pm UTC](https://discourse.processing.org/t/how-do-i-pass-a-value-from-one-sketch-to-another-sketch/21176/10 "2020-05-24T13:45:50Z")

</div>

Interesting read! Thank you.

Why? One sketch is all about image analysis and the colorMode is HSB, the other sketch uses some results from the first sketch and is all about creating new images and is in colorMode LCH. So, one sketch makes it simpler for the variable yes, but more difficult in every other way. Also, over and above that, I want to investigate, learn and understand.

I like the semaphore very much. But what causes the flag to go up (or down)?

```auto
  if (something !=null) { // what is the something? // flag down, 1
    println(Val[0]);
  } else { // flag up, 0
    //return null; // what is Processing code here?
  }

```
