Save variable in a loop

/**
 * 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() {
  saveStrings(dataPath("a.txt"), new String[] { str(a) });
  super.exit();
}
3 Likes