Software "history" storing methods.

So I’m making this 2d bone modeler.

The question I would like to ask is what is the “way” to do “history”. The thingy with CTRL+Z, Y. Do I store copies of my data every time I change something? But that seems to me that doing that would destroy the memory.

Do I just record what actions were done and reverse them? I tried this, and it seems not to work very properly.

It should, if you really store EVERY kind of change…

Look at every text editor, for example. They save every single letter you changed (or they should), but generally they group the saved letters by words. I would do this for your program. Instead of saving every tiny change, group together a few small changes. But you have to be careful because you need to only group small changes, as larger changes are far more significant in some cases.

However, my way is still flawed because some smaller changes might also be important. Maybe instead of saving them to memory, you should save them to file. Open a file when the program is started, assign every joint an id and then use that id and save the new modified joint angles/positions/whatever else to the file. When the user presses the back button, simply loop backwards through the text file, getting every id and then assigning the old attributes to that specific joint. When the user exits the program, close the file.

Optional: Save the file to disk when the program exits, or at regular intervals so that if the user experiences a power outage most of their data will still be there.

Similarly to what Opiop said, though I’m not an expert, I’d store every state of the program on a stack and pop the stack when the user presses ctrl-Z.

And maybe it is the best to add every undone step to a redo stack that is cleared as soon as a new action is performed…

I came across this article recently on the Command Pattern. I haven’t used it yet, but one of the things the pattern apparently enables is a method of undo/redo. The site that article comes from, Game Programming Patterns, has a number of great articles on general game programming and architecture.