How would one go about....

Doing an undo feature? I may need to have the ability to undo the last move I just made in game. I would require it to simply allow only one step back at first but later I may want to let the player have unlimited steps.

Any ideas?

Fanks

Assuming these moves are simple operations just store your moves in an array and step back through them reversing the operations.

The more complex the game / moves the more you will have to store in each array entry so you might have to create a struct for move data.

Okay, cheers =]

You could also consider using a Stack. If you’re not familiar, you can think of a Stack like a stack of plates, each time you add (or “push”) a plate it goes on top of the stack, and each time you remove (or “pop”) a plate you take the one from the top of the stack. So if you push a red plate, a green plate, then a blue plate, then when you pop them, you get them in reverse order: first the blue plate, then the green plate, then the red plate. The same way an Undo function works.

var undoHistory = new Stack<GameMoves>();

// each time the player does a new move
undoHistory.Push(myMove);

// each time the player clicks undo
currentMove = undoHistory.Pop()

The only downside is after you pop things they’re not in the stack anymore, so if you later want to add Redo, you might want to use an array or List instead and keep track of all the states.