How to access variables from PApplet (2nd window) in default window?

Hi!
I recently saw someone’s code which allowed for 2 windows. I tried to adapt one of my programs to it but it didn’t work too well. I know I can just declare them at the beginning and be done with it, but is there a way to do it differently?

MultiWindows sketch2; 

void settings() {
  size(400, 400);
}

void setup() {
  surface.setLocation(0, 0);
  surface.setTitle("Input window");
  sketch2 = new MultiWindows();
  
  
}

void draw() {
  background(255);
  stroke(255, 0, 0);
  println(txt);
}


class MultiWindows extends PApplet {
  String txt = "";
  MultiWindows() {
    super();
    PApplet.runSketch(new String[] {this.getClass().getSimpleName()}, this);
  }

  public void settings() {
    size(400, 400);
  }

  public void setup() {
    surface.setLocation(400, 0);
    surface.setTitle("Display window");
  }

  public void draw() {
    background(0);
    fill(255);
    text(txt,width/2,height/2);
  }
  public void keyPressed() {
    txt += key;
  }
}

If you change “println(txt)” to “println(sketch2.txt)”, is that the behavior you were hoping to get?

1 Like

yes thank you. But can you do something like sketch2.txt = "aaa"?