# Undo Function for my Tool

**URL:** <https://discourse.processing.org/t/undo-function-for-my-tool/1618>\
**Category:** Coding Questions\
**Created:** [July 9, 2018, 12:38pm UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618 "2018-07-09T12:38:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 12:38pm UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618/1 "2018-07-09T12:38:16Z")

</div>

Hi,

at the moment I’m working on a tool to visualize data. The tool is able to save and restore the data when you click the “Save” button. But that function only saves the then prepared and calculated data, not all the settings in various menus or temporary variables.  
Now I want to create a function that I can call whenever the user made a significant change. My dream would be to dump the complete RAM at that moment and later, when the user decides to undo his previous (5 or less) actions, load that data.  
Is something like that possible?

Max

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 9, 2018, 12:48pm UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618/2 "2018-07-09T12:48:38Z")

</div>

Ovbiously I mean is there a clean way to do it?

If not I could create a class where all variables are stored and save an instance of that class in an ArrayList…  
I want to die.

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 9, 2018, 6:22pm UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618/3 "2018-07-09T18:22:01Z")

</div>

There is not a clean way to do it.

You should create a class where all the variables are stored and save an instance of that class in an ArrayList.

* * *

Actually, depending on your data, you might be able to record not a snapshot of the entire data set, but rather just what the last 5 changes made to it were. Example: Let’s say the user does the following six actions:

1. Set redness from 180 to 90.
2. Adjust size from 30 to 160.
3. Turn up volume by 154%.
4. Recombobulate the entire data set.
5. Pull the latest weather info from an external site.
6. Scribble a doodle over the UI.

To undo 6, you’d just need to redraw the UI without it.  
To roll back 5, you’d need that full copy of the previous data. Yikes.  
To undo 4, you’d need a backup of that data. Everyone knows that you can’t de-recombobulate something (it’s a one-way operation).

Those are the cases where you’d be backing things with a class, probably.

But for the first three, they are just value changes. You need not record the entire data set, just the fact that “volume”, “size”, and “redness” were what were changed, and the amounts they were previously.

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 11, 2018, 9:13am UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618/4 "2018-07-11T09:13:25Z")

</div>

> [@TfGuy44](#):
>
> You should create a class where all the variables are stored and save an instance of that class in an ArrayList.

I tried this now but it only stores a reference so when I change a value also the stored value is changed. Do you know how to fix that?  
It’s similar to the arrayCopy problem…

```auto
Variables var = new Variables();
ArrayList<Variables> = new ArrayList<Variables>();

void setup() {

}

void draw() {
  println(var.value);
}

class Variables {
  int value = 0;
}

void mousePressed() {
  var.value++;
}

void keyPressed() {
  if (key == 's') {
    var_list.add(var);
    println("stored @ " + (var_list.size()-1));
  } else if (key == 'u') {
    if (var_list.size() != 0) {
      var = new Variables();
      var = var_list.get((var_list.size()-1));
      println("undone to " + (var_list.size()-1));
    }
  }
}

```

You can use the debugger to see the value in the ArrayList changeing aswell…

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [July 11, 2018, 3:45pm UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618/5 "2018-07-11T15:45:26Z")

</div>

Yeah, I see your problem. Add a deepCopy method to you class that creates a clone of the class, not a reference to it. Working example:

```auto
Variables var = new Variables();
ArrayList<Variables> var_list= new ArrayList<Variables>();

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

void draw() {
  background(0);
  stroke(255);
  text(var.value, 20, 20);
}

class Variables {
  int value = 0;
  Variables deepCopy() {
    Variables clone = new Variables();
    clone.value = value;
    return clone;
  }
}

void mousePressed() {
  var.value++;
}

void keyPressed() {
  if (key == 's') {
    var_list.add(var.deepCopy());
    println("stored value " + var.value + " at position " + (var_list.size()-1) );
  } else if (key == 'u') {
    if (var_list.size() > 0) {
      var = var_list.get((var_list.size()-1));
      var_list.remove((var_list.size()-1));
      println( "" );
      println("restored the value of " + var.value + " from stored position " + (var_list.size()) );
      println( "new stored size is " + (var_list.size()) );
    } else {
      println( "" );
      println( "can't undo - no more stored values" );
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![MxFxM](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@MxFxM](https://discourse.processing.org/u/MxFxM)\
**Post date:** [July 12, 2018, 7:55am UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618/6 "2018-07-12T07:55:22Z")

</div>

Yeah that’s what I figured out too.

Here are some parts for different variable types in case someone wants to do something similar:

```auto
// an int / float / single variable
this.X_MIN = v.X_MIN;

// a float array
this.supports_xparallel = new float[v.supports_xparallel.length];
arrayCopy(v.supports_xparallel, this.supports_xparallel);

// a multi dimensional array (this is not perfect but it works)
this.iso_line_points = new float[v.iso_line_points.length][v.iso_line_points[0].length][v.iso_line_points[0][0].length];
for (int a = 0; a < v.iso_line_points.length; a++) {
  for (int b = 0; b < v.iso_line_points[a].length; b++) {
    for (int c = 0; c < v.iso_line_points[a][b].length; c++) {
      this.iso_line_points[a][b][c] = v.iso_line_points[a][b][c];
    }
  }
}

// an arrayList
this.data.clear();
for (int i = 0; i < v.data.size(); i++) {
  this.data.add(v.data.get(i));
}

```

---

<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:** [July 12, 2018, 12:42pm UTC](https://discourse.processing.org/t/undo-function-for-my-tool/1618/7 "2018-07-12T12:42:37Z")

</div>

> **[Final array is being changed](https://Discourse.Processing.org/t/final-array-is-being-changed/1447/4)**
>
> Some of my clone() examples I did a long time ago at the link below may help ya out: 😸 https://Forum.Processing.org/two/discussion/12044/how-do-i-copy-out-one-row-of-a-2d-array-into-a-1d-array final int\[\]\[\] stuff = { {1, 2, 3}, {4, 5, 6}, {7, 8,...
