Implementing undo/redo in LevelEditor

Implementing undo/redo in LevelEditor

Postby Edward_Lii » 28 Apr 2012, 15:12

Hello all,

I was planning on implementing undo and redo support for the leveleditor.
This needs some careful planning and that's why I created this topic.

I've already tried some stuff with an vector containing the preformed actions.
These actions can be reverted, the problem with this approach is that the editor does a lot of unpredictable stuff.

If the user places a block left of the level (say at -100,50) an action will be added with those coordinates.
When reverting the block will be removed, but the level bounds have changed, meaning that other undos will probably fail.
And off course a redo of the action will place a block at -100,50 which is again outside the bounds and will stretch the level further.

What I tried was instead of storing the actions done by the user (placing block, removing block) store the actions the editor did.
This worked a lot better, except for the fact that there need to be more action types, and more code to redo and undo these.
Also the methods for placing, moving and removing objects need to store their action(s) only when they are called from a user action, not from redoing.

This is a possible implementation, it is just a lot of work, not only making but also maintaining.

An easier, but memory intensifier, approach would be to store the state of the level before each action.
The advantage of this is that it's relatively easy to make and maintain and if implemented correctly error prove.
Instead of storing the whole state we can also store a diff of the state, kind of like a patch.

Perhaps there are other methods?

Any suggestions/feedback are welcome. ;)
From,
Edward_Lii
User avatar
Edward_Lii
MnMS Moderator
 
Posts: 774
Joined: 20 Dec 2010, 16:46

Re: Implementing undo/redo in LevelEditor

Postby ctdabomb » 28 Apr 2012, 15:16

Edward_Lii {l Wrote}:Instead of storing the whole state we can also store a diff of the state, kind of like a patch.

to me, this sounds like the best approach.
Some people are like slinkies... not really good for anything, but you still can't help smiling when you shove them down the stairs.
ctdabomb
SuperTux Moderator
 
Posts: 1074
Joined: 13 Dec 2011, 21:21
Location: halfway there

Re: Implementing undo/redo in LevelEditor

Postby Knitter » 28 Apr 2012, 19:54

Most of the times I had to implement such a feature, storing the game/editor state was the easiest and less error prone, I would first go with that approach. Naturally, the amount of undo/redo actions need to be limited, but you can easily store the editor contents in an in-memory file/buffer. So, you can basically have an array of memory streams to which you push the editor state at each action the user makes, since the array can be made circular, new actions will replace older ones keeping.
Knitter
 
Posts: 238
Joined: 03 Jul 2011, 22:52
Location: Portugal

Re: Implementing undo/redo in LevelEditor

Postby MCMic » 28 Apr 2012, 21:06

It should be as easy to code as the replay function and the mimic of the shadow ^^
User avatar
MCMic
 
Posts: 689
Joined: 05 Jan 2010, 17:40

Who is online

Users browsing this forum: No registered users and 1 guest